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.
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:f {print(4711);} 1000, 1111;
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...sub f(\[$&];@)
--
Ronald Fischer <ynnor@mm.st>
Ronald Fischer <ynnor@mm.st>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Prototype question
by CountZero (Bishop) on Sep 05, 2012 at 12:41 UTC | |
by rovf (Priest) on Sep 05, 2012 at 12:54 UTC | |
by CountZero (Bishop) on Sep 05, 2012 at 13:18 UTC |
Back to
Seekers of Perl Wisdom