dimar has asked for the wisdom of the Perl Monks concerning the following question:
Consider the following code:
### sample1 $_ = 'lrep esu'; $str = reverse lcfirst uc; print $str."\n"; ### sample2 $_ = 'lrep esu'; $str = lcfirst reverse uc; print $str."\n";
Each sample produces a different result, but neither one generates an error. This is true regardless of the order in which I specify the built-in functions. Moreover, I do not have to specify parenthesis or an input argument. The functions implicitly work on $_ by default.
Question: suppose I have a custom subroutine "spaces_to_und" that converts spaces to underscores. How do I declare the subroutine so that it works *just like* the built-in lcfirst, reverse and uc functions?
The goal is to operate on $_, just like built-ins, as well as obviate the need to use parens, as well as the ability to call the functions in any relative order in the source code, without generating an error.
To pull this off, prototyping is surely required, but no proof-of-concept has yet been found. Is this even possible?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: subroutine bewilderment: how to mimic builtins
by ikegami (Patriarch) on Feb 07, 2006 at 23:22 UTC | |
by japhy (Canon) on Feb 07, 2006 at 23:59 UTC | |
by ysth (Canon) on Feb 08, 2006 at 02:43 UTC | |
by ikegami (Patriarch) on Feb 08, 2006 at 03:55 UTC | |
by ysth (Canon) on Feb 08, 2006 at 04:39 UTC | |
Re: subroutine bewilderment: how to mimic builtins
by diotalevi (Canon) on Feb 07, 2006 at 23:29 UTC | |
Re: subroutine bewilderment: how to mimic builtins
by revdiablo (Prior) on Feb 07, 2006 at 23:21 UTC | |
by ikegami (Patriarch) on Feb 07, 2006 at 23:47 UTC |