in reply to Re^2: Spotting an empty array as argument
in thread Spotting an empty array as argument

The prototype (_) can be used for the last argument. Some builtin functions can't be simulated, though, you can detect them by prototype('CORE::func') returning undef.

For example, you can easily replicate the behaviour of uc, as prototype('CORE::uc') returns _. Similarly, pack has the prototype of $_ (the underscore must be the last one). But print or chomp return undef, so their behaviour is more complex and prototypes can't express it. So there is a special rule, but only for some of the built-in functions.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^4: Spotting an empty array as argument
by ikegami (Patriarch) on Mar 26, 2021 at 19:32 UTC

    While prototype would normally be a useful thing to try, say can't be prototyped because it supports say FH ..., so a definitive answer can't be obtained from prototype here (assuming the lack of file handle support is acceptable).

    Seeking work! You can reach me at ikegami@adaelis.com

      > say can't be prototyped because it supports say FH ...,

      I can't follow, the fact that say is internally calling something like FH->print(@_,"\n") shouldn't have any consequence on the prototype.

      Hmmm ...

      I think what you mean is that this inhibits attempts to override say with another implementation, since it needs to act like an indirect method call, but with prototype magic.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        the fact that say is internally calling something like FH->print(@_,"\n") shouldn't have any consequence on the prototype.

        It doesn't. It's the syntax that can't be recreated with a prototype.

        I think what you mean is that this inhibits attempts to override say with another implementation,

        Not at all. That's what the comment to which I replied said.

        I pointed out that's not necessarily relevant since the OP didn't ask to replicate all of say's syntax, and irrelevant parts of say can't be recreated using prototypes.

        It was a nitpick. It is, of course, entirely correct that the relevant behaviour of say can't be replicated with a prototype. It just can't be ascertained by the means used by the person to which I replied. This is all I said.

        Seeking work! You can reach me at ikegami@adaelis.com

Re^4: Spotting an empty array as argument (prototype _ )
by LanX (Saint) on Mar 26, 2021 at 10:46 UTC
    > (the underscore must be the last one)

    FWIW, the last of the mandatory ones!

    I.e. you can define following prototypes as long as they are marked optional with a semicolon.

    Trouble here is the scalar context!

    DB<237> sub tst (_;@) { dd \@_ } DB<238> $_=666; @a='a'..'c'; $a='A' DB<239> tst [666] DB<240> tst $a,1,2,3 ["A", 1, 2, 3] DB<241> tst $a,@a ["A", "a", "b", "c"] DB<242> tst @a # OOPS [3]

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery