This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Start refactoring cmd_L.
[perl5.git] / lib / CORE.pod
1 =head1 NAME
2
3 CORE - Namespace for Perl's core routines
4
5 =head1 SYNOPSIS
6
7     BEGIN {
8         *CORE::GLOBAL::hex = sub { 1; };
9     }
10
11     print hex("0x50"),"\n";                     # prints 1
12     print CORE::hex("0x50"),"\n";               # prints 80
13     CORE::say "yes";                            # prints yes
14
15     BEGIN { *shove = \&CORE::push; }
16     shove @array, 1,2,3;                        # pushes on to @array
17
18 =head1 DESCRIPTION
19
20 The C<CORE> namespace gives access to the original built-in functions of
21 Perl.  The C<CORE> package is built into
22 Perl, and therefore you do not need to use or
23 require a hypothetical "CORE" module prior to accessing routines in this
24 namespace.
25
26 A list of the built-in functions in Perl can be found in L<perlfunc>.
27
28 For all Perl keywords, a C<CORE::> prefix will force the built-in function
29 to be used, even if it has been overridden or would normally require the
30 L<feature> pragma.  Despite appearances, this has nothing to do with the
31 CORE package, but is part of Perl's syntax.
32
33 For many Perl functions, the CORE package contains real subroutines.  This
34 feature is new in Perl 5.16.  You can take references to these and make
35 aliases.  However, some can only be called as barewords; i.e., you cannot
36 use ampersand syntax (C<&foo>) or call them through references.  See the
37 C<shove> example above.  These subroutines exist for all keywords except the following:
38
39 C<__DATA__>, C<__END__>, C<and>, C<cmp>, C<default>, C<do>, C<dump>,
40 C<else>, C<elsif>, C<eq>, C<eval>, C<for>, C<foreach>, C<format>, C<ge>,
41 C<given>, C<goto>, C<grep>, C<gt>, C<if>, C<last>, C<le>, C<local>, C<lt>,
42 C<m>, C<map>, C<my>, C<ne>, C<next>, C<no>, C<or>, C<our>, C<package>,
43 C<print>, C<printf>, C<q>, C<qq>, C<qr>, C<qw>, C<qx>, C<redo>, C<require>,
44 C<return>, C<s>, C<say>, C<sort>, C<state>, C<sub>, C<tr>, C<unless>,
45 C<until>, C<use>, C<when>, C<while>, C<x>, C<xor>, C<y>
46
47 Calling with
48 ampersand syntax and through references does not work for the following
49 functions, as they have special syntax that cannot always be translated
50 into a simple list (e.g., C<eof> vs C<eof()>):
51
52 C<chdir>, C<chomp>, C<chop>, C<defined>, C<delete>, C<each>,
53 C<eof>, C<exec>, C<exists>, C<keys>, C<lstat>, C<pop>, C<push>,
54 C<shift>, C<splice>, C<split>, C<stat>, C<system>, C<truncate>,
55 C<unlink>, C<unshift>, C<values>
56
57 =head1 OVERRIDING CORE FUNCTIONS
58
59 To override a Perl built-in routine with your own version, you need to
60 import it at compile-time.  This can be conveniently achieved with the
61 C<subs> pragma.  This will affect only the package in which you've imported
62 the said subroutine:
63
64     use subs 'chdir';
65     sub chdir { ... }
66     chdir $somewhere;
67
68 To override a built-in globally (that is, in all namespaces), you need to
69 import your function into the C<CORE::GLOBAL> pseudo-namespace at compile
70 time:
71
72     BEGIN {
73         *CORE::GLOBAL::hex = sub {
74             # ... your code here
75         };
76     }
77
78 The new routine will be called whenever a built-in function is called
79 without a qualifying package:
80
81     print hex("0x50"),"\n";                     # prints 1
82
83 In both cases, if you want access to the original, unaltered routine, use
84 the C<CORE::> prefix:
85
86     print CORE::hex("0x50"),"\n";               # prints 80
87
88 =head1 AUTHOR
89
90 This documentation provided by Tels <nospam-abuse@bloodgate.com> 2007.
91
92 =head1 SEE ALSO
93
94 L<perlsub>, L<perlfunc>.
95
96 =cut