Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

effects of ampersand and non ampersand when referencing a sub name

by ISAI student (Scribe)
on Dec 20, 2012 at 08:40 UTC ( [id://1009702]=perlquestion: print w/replies, xml ) Need Help??

ISAI student has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I am looking at an old script that I am "renovating", and I saw the following line of code, which will serve as an example:

$thread = threads->create(\&execute, $i, $reg_info_hashRef, $erbose);

I want the threads->create to check that I input the right number of arguments (execute is prototyped). This means that the code should look like this:

$thread = threads->create(\execute, $i, $reg_info_hashRef, $erbose);
I know that using ampersand disables the prototype test. Should removing just enable the prototype test, and/or do anything else? Thanks.

Replies are listed 'Best First'.
Re: effects of ampersand and non ampersand when referencing a sub name
by tobyink (Canon) on Dec 20, 2012 at 08:56 UTC

    Using a coderef at all bypasses prototypes. (As does calling a sub as a method.)

    If you need to check the number of arguments passed to a sub, do it in the sub! Prototypes are just to help the Perl parser parse argument lists more sanely.

    sub execute { @_==3 or croak "execute() needs three arguments!!"; ...; }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: effects of ampersand and non ampersand when referencing a sub name
by Jenda (Abbot) on Dec 20, 2012 at 10:00 UTC

    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.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      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'
Re: effects of ampersand and non ampersand when referencing a sub name
by jwkrahn (Abbot) on Dec 20, 2012 at 08:49 UTC

    \&execute and \execute do two different things.    \&execute passes a code reference to the subroutine threads->create while \execute executes the subroutine execute and passes a reference to the return value.

Re: effects of ampersand and non ampersand when referencing a sub name
by ISAI student (Scribe) on Dec 20, 2012 at 10:52 UTC
    Thank you all! I will do as you suggest. The code will be longer, but safer.
      Hi ISAI,

      On a meta level (and regarding your last posts :)

      I think Damian's Perl Best Practices could be a valuable read for you!

      It helped me a lot to get a much more profound orientation in Perl.

      Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1009702]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found