override(3) User Contributed Perl Documentation override(3)
NNNNAAAAMMMMEEEE
ex::override - Perl pragma to override core functions
SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
use ex::override ucfirst => sub {
# make sure only the first
# letter is uppercased
ucfirst( lc( shift ) );
};
ucfirst( 'MAKE THIS RIGHT' );
# Make this right
no ex::override 'ucfirst';
ucfirst( 'MAKE THIS RIGHT' );
# MAKE THIS RIGHT
DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
"ex::override" is an easy way to override core perl func-
tions.
OOOOvvvveeeerrrrrrrriiiiddddiiiinnnngggg aaaa ffffuuuunnnnccccttttiiiioooonnnn
use ex::override
length => \&mylength,
open => \&myopen;
Overriding a core function happens at compile time. Argu-
ments are passed to "ex::override" in a name based, or
hash style. The key is the name of the core function to
override, the value is your subroutine to replace the
core's.
UUUUssssiiiinnnngggg aaaannnn oooovvvveeeerrrrrrrriiiiddddeeeennnn ffffuuuunnnnttttiiiioooonnnn
Nothing changes on the surface. If you override "stat",
then you still use "stat" the same way.
NNNNOOOOTTTTEEEE:::: This is only true if you are keeping the same proto-
type as the function you've overriden. To do this, you
must define your prototype:
use ex::override values => sub (\%) { values %{+shift} };
If you don't use this same prototype or force yourself to
use the function the same, you can extend the functional-
ity of a core function:
# length of all arguments passed to length()
use ex::override length => sub { length join '', @_ };
OOOOvvvveeeerrrrrrrriiiiddddiiiinnnngggg aaaa ffffuuuunnnnccccttttiiiioooonnnn gggglllloooobbbbaaaallllyyyy
DDDDoooonnnn''''tttt ddddoooo tttthhhhiiiissss wwwwiiiitttthhhhoooouuuutttt aaaa vvvveeeerrrryyyy ggggoooooooodddd rrrreeeeaaaassssoooonnnn!!!!
"ex::override" allows you the ability to override core
functions globaly. Any packages that inherit from yours
will use your function override. There are good reasons
for doing this, if you think you need to, make sure you
have a good reason.
use ex::override
GLOBAL_length => sub {
# prevent someone from passing a list
croak "Don't do that!" if @_ > 1;
length shift
};
NNNNOOOOTTTTEEEE:::: If you globaly override a function in a package,
only that package can remove it.
RRRReeeemmmmoooovvvviiiinnnngggg yyyyoooouuuurrrr oooovvvveeeerrrrrrrriiiiddddeeee
This works the same way that "no strict" works.
no ex::override; # remove _all_ overrides
no ex::override 'values';
no ex::override 'GLOABL_length';
TTTTIIIIPPPPSSSS
Get a list of overrideable function
If you have the Perl source laying around, go to it's
root dir and try this:
perl -lne 'print /_(\w+)/ if /return -K/' toke.c
You'll have to weed out which ones are functions ( vs.
operators, etc. ).
Get a functions prototype
perl -lwe 'print prototype "CORE::length"'
This prints the prototype, or "Use of uninitialized
variable..." if there isn't one.
TTTTOOOODDDDOOOO
Find a way to preserve prototypes so the user doesn't have
to know them.
AAAAUUUUTTTTHHHHOOOORRRR
Casey West, <_c_a_s_e_y_@_g_e_e_k_n_e_s_t_._c_o_m>
CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
Copyright (c) 2000 Casey West <casey@geeknest.com>. All
rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as
Perl itself.
2003-03-14 perl v5.6.1 override(3)