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