Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Spotting an empty array as argument

by GrandFather (Saint)
on Mar 25, 2021 at 20:15 UTC ( [id://11130337]=note: print w/replies, xml ) Need Help??


in reply to Spotting an empty array as argument

Perl "flattens" arrays and hashes passed as parameters to a sub into a list of scalar values (see the perlsub Description section, paragraph two. In your case it is not possible for the sub to tell the difference between a call with no parameters and a call containing one or more empty array and empty hash parameters.

Perhaps a solution is to use a "sayArray" sub that does the multi-line thing and use Perl's "say" in other cases? That has the advantage that it clearly signals to the user that something special is happening in the array case.

Perl's built in "say" doesn't need any magic for handling a "no argument" case. It just does what print does (in that case it prints nothing) then prints a newline, which is what it always does.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
  • Comment on Re: Spotting an empty array as argument

Replies are listed 'Best First'.
Re^2: Spotting an empty array as argument
by Chuma (Scribe) on Mar 25, 2021 at 21:01 UTC

    Right, I suspected there might not be any nice way of doing it. I mean, "say" does seem to do some magic in the "no argument" case, just like "print", since it treats it differently from the "empty array" case. But maybe that's just a special rule for built-in functions.

    It's weird though, because there are so many built-in functions that have the same behaviour, defaulting to $_ if there are no arguments, and it's clearly useful, so it's surprising if there's no way of recreating that for your own functions.

      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]

        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

        > (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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130337]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found