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>

In reply to Prototype question by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.