1CORE(3pm) Perl Programmers Reference Guide CORE(3pm)
2
3
4
6 CORE - Namespace for Perl's core routines
7
9 BEGIN {
10 *CORE::GLOBAL::hex = sub { 1; };
11 }
12
13 print hex("0x50"),"\n"; # prints 1
14 print CORE::hex("0x50"),"\n"; # prints 80
15 CORE::say "yes"; # prints yes
16
17 BEGIN { *shove = \&CORE::push; }
18 shove @array, 1,2,3; # pushes on to @array
19
21 The "CORE" namespace gives access to the original built-in functions of
22 Perl. The "CORE" package is built into Perl, and therefore you do not
23 need to use or require a hypothetical "CORE" module prior to accessing
24 routines in this namespace.
25
26 A list of the built-in functions in Perl can be found in perlfunc.
27
28 For all Perl keywords, a "CORE::" prefix will force the built-in
29 function to be used, even if it has been overridden or would normally
30 require the feature pragma. Despite appearances, this has nothing to
31 do with the CORE package, but is part of Perl's syntax.
32
33 For many Perl functions, the CORE package contains real subroutines.
34 This feature is new in Perl 5.16. You can take references to these and
35 make aliases. However, some can only be called as barewords; i.e., you
36 cannot use ampersand syntax (&foo) or call them through references.
37 See the "shove" example above. These subroutines exist for all
38 overridable keywords, except for "dump" and the infix operators.
39 Calling with ampersand syntax and through references does not work for
40 the following functions, as they have special syntax that cannot always
41 be translated into a simple list (e.g., "eof" vs "eof()"):
42
43 "chdir", "chomp", "chop", "each", "eof", "exec", "keys", "lstat",
44 "pop", "push", "shift", "splice", "stat", "system", "truncate",
45 "unlink", "unshift", "values"
46
48 To override a Perl built-in routine with your own version, you need to
49 import it at compile-time. This can be conveniently achieved with the
50 "subs" pragma. This will affect only the package in which you've
51 imported the said subroutine:
52
53 use subs 'chdir';
54 sub chdir { ... }
55 chdir $somewhere;
56
57 To override a built-in globally (that is, in all namespaces), you need
58 to import your function into the "CORE::GLOBAL" pseudo-namespace at
59 compile time:
60
61 BEGIN {
62 *CORE::GLOBAL::hex = sub {
63 # ... your code here
64 };
65 }
66
67 The new routine will be called whenever a built-in function is called
68 without a qualifying package:
69
70 print hex("0x50"),"\n"; # prints 1
71
72 In both cases, if you want access to the original, unaltered routine,
73 use the "CORE::" prefix:
74
75 print CORE::hex("0x50"),"\n"; # prints 80
76
78 This documentation provided by Tels <nospam-abuse@bloodgate.com> 2007.
79
81 perlsub, perlfunc.
82
83
84
85perl v5.16.3 2013-03-04 CORE(3pm)