Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Prototype question

by rovf (Priest)
on Sep 05, 2012 at 12:02 UTC ( [id://991819]=perlquestion: print w/replies, xml ) Need Help??

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

UPDATE: Forget this node. I just saw that I have misread the docs. NO NEED TO RESPOND.

I have a sub which expects one or more arguments. In particular, the first argument could be (for instance) a number or a reference to an anonymous sub. Here are two valid examples for calling my function:

f(15,103); f(sub { print(4711); }, 1000, 1111);

As syntactic sugar, I would like to have the possibility to specify the first argument as a code block, i.e.

f {print(4711);} 1000, 1111;
I guess there is no alternative for using prototypes in this case.... However, I can not specify the prototype as f(&;@), since in this case the first argument must be a sub ref or code block, but in my case, it might also be a non-reference. Hence I tried to prototype my function like this:
sub f(\[$&];@)
I.e. a simple scalar or a code block, optionally followed by other arguments. Unfortunately this does not work. When invoking f as f(1,2,3,4), I get the error message Type of arg 1 to main::f must be one of [$&] (not constant item).

What am I doing wrong? I must misunderstand the explanations in perlsub about the the "\[]" notation prototype somehow...
-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Prototype question
by CountZero (Bishop) on Sep 05, 2012 at 12:41 UTC
    Just forget about prototypes. Perl does not need them in most cases. All agree that the Perl prototypes are one of the most misunderstood features of Perl. More specifically they are NOT made to give you the general ability to check whether correct paramaters have been provided to your sub. If you want to make sure correct parameters are given to your sub, you must include these tests inside your sub and these tests then work at run-time, not at compile-time (as in many bondage and discipline languages).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      I know; however, if you want to use the "code block" syntax to call a function, you must define a prototype. At least Perl 5.8.8 (which I still have to use) does not provide an alternative.

      My solution to my original problem was to use a cover function. Basically I define my function f without prototypes, and then add a function

      sub g(&@) { &f }
      to provide the ability to write codeblocks.

      -- 
      Ronald Fischer <ynnor@mm.st>
        Indeed you are right. I somehow missed the "code block" reference in your post or misread it as a anonymous sub reference. Sorry for that.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found