We should all be familiar with the problems related to prototypes in Perl. Here are the two biggies:
The second item is the one I am curious about at the moment.
I know of two ways to subvert/work around/ignore prototype enforcement when calling a subroutine:
Foo->subroutine_name();
&
sigil. &subroutine_name();
Are there any other interesting cases I've missed?
Udpate:
@brian d foy, I don't particularly want to evade prototypes, but I wondered "how many ways are there to do it?" I ask this question out of curiosity.
@jrockway, I agree with you, and I believe that you have more explicitly and more concisely described my first point regarding the problems with prototypes, that people misunderstand them. Perhaps the problem lies in programmer expectations and not in the feature. But that is really a philosophical question of the sort I don't want to have.
A Perl function prototype is zero or more spaces, backslashes, or type characters enclosed in parentheses after the subroutine definition or name. A backslashed type symbol means that the argument is passed by reference, and the argument in that position must start with that type character.
To declare a subroutine, use one of these forms: sub NAME ; # A "forward" declaration. sub NAME ( PROTO ); # Ditto, but with prototype.
Call it via a subroutine reference.
sub foo($) { print "arg is $_[0]\n" }
my $sub = \&foo;
$sub->();
Call it before Perl has seen the prototype (important because perl doesn't make you declare subs before use):
foo();
sub foo($) { print "arg is $_[0]\n" }
Using the goto &name
syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With