This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move a test from t/lib/warnings/sv to .../9uninit
[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
d47dcd9c
FC
37C<shove> example above. These subroutines exist for all overridable
38keywords, except for C<dump> and the infix operators. Calling with
2702a50a
FC
39ampersand syntax and through references does not work for the following
40functions, as they have special syntax that cannot always be translated
41into a simple list (e.g., C<eof> vs C<eof()>):
42
43C<chdir>, C<chomp>, C<chop>, C<each>, C<eof>, C<exec>, C<keys>, C<lstat>,
44C<pop>, C<push>, C<shift>, C<splice>, C<stat>, C<system>, C<truncate>,
45C<unlink>, C<unshift>, C<values>
4aaa4757 46
c8472d06
T
47=head1 OVERRIDING CORE FUNCTIONS
48
1694bc13
RGS
49To override a Perl built-in routine with your own version, you need to
50import it at compile-time. This can be conveniently achieved with the
51C<subs> pragma. This will affect only the package in which you've imported
52the said subroutine:
c8472d06 53
1694bc13
RGS
54 use subs 'chdir';
55 sub chdir { ... }
56 chdir $somewhere;
c8472d06 57
1694bc13
RGS
58To override a built-in globally (that is, in all namespaces), you need to
59import your function into the C<CORE::GLOBAL> pseudo-namespace at compile
60time:
61
62 BEGIN {
63 *CORE::GLOBAL::hex = sub {
64 # ... your code here
65 };
66 }
67
68The new routine will be called whenever a built-in function is called
c8472d06
T
69without a qualifying package:
70
1694bc13 71 print hex("0x50"),"\n"; # prints 1
c8472d06 72
1694bc13
RGS
73In both cases, if you want access to the original, unaltered routine, use
74the C<CORE::> prefix:
c8472d06 75
1694bc13 76 print CORE::hex("0x50"),"\n"; # prints 80
c8472d06
T
77
78=head1 AUTHOR
79
1694bc13 80This documentation provided by Tels <nospam-abuse@bloodgate.com> 2007.
c8472d06
T
81
82=head1 SEE ALSO
83
1694bc13 84L<perlsub>, L<perlfunc>.
c8472d06
T
85
86=cut