Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: effects of ampersand and non ampersand when referencing a sub name

by Jenda (Abbot)
on Dec 20, 2012 at 10:00 UTC ( #1009712=note: print w/replies, xml ) Need Help??

in reply to effects of ampersand and non ampersand when referencing a sub name

As others wrote already there is no way to make this work. The important thing to remember is that prototypes are NOT subroutine signatures! Prototypes do not exist to allow you to specify the number and type of arguments! They exist to allow you to create (very occasionally) a subroutine that works as a builtin. Take push() for example. If you pass (@arr, 1,2,3) to an ordinary subroutine, then the subroutine receives a list containing all elements in @arr followed by 1, 2 and 3 and has no way to modify the @arr array. push() on the other hand receives the array and then the 1, 2 and 3 and can modify the array.

If for whatever reason you feel the need to create a subroutine that is used the same as push(), you can use a prototype to tell Perl to change the way its parameters are handled, but you should not attempt to use it to validate that a subroutine gets the expected number of parameters!

The only prototype an ordinary programmer should ever use is the empty one for constant subroutines (and even then  use constant ... is better) and MAYBE ($) for subroutines that are to be used in mathematical expressions. All other prototypes are best left to a few module authors.

Enoch was right!
Enjoy the last years of Rome.

Replies are listed 'Best First'.
Re^2: effects of ampersand and non ampersand when referencing a sub name
by tobyink (Abbot) on Dec 20, 2012 at 17:55 UTC

    The (&) prototype is useful if you're writing subs that expect to be passed coderefs/callbacks. And (_) can be useful too.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009712]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-03-30 23:10 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (364 votes). Check out past polls.