Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^5: Distinguish between missing and undefined arguments with subroutine signatures

by jo37 (Deacon)
on Jan 08, 2021 at 22:58 UTC ( [id://11126636]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Distinguish between missing and undefined arguments with subroutine signatures
in thread Distinguish between missing and undefined arguments with subroutine signatures

Trying to implement it according to your remarks leads me to something I don't like at all.

EDIT: See comments in uglier_foo.

my $missing = sub {...}; # private sub sub ugly_foo ($self, $foo=$missing->(), $bar=$missing->()) { say "foo is missing" if $missing->($foo); say "bar is missing" if $missing->($bar); } # compare with eq sub uglier_foo($self, $foo=MISSING, $bar=MISSING) { # This comes from posting insufficiently tested code at midnight # say "foo is missing" if !$foo || $foo eq MISSING; # say "bar is missing" if !$foo || $foo eq MISSING; say "foo is missing" if $foo && $foo eq MISSING; say "bar is missing" if $bar && $bar eq MISSING; } # slurping sub ugliest_foo ($self, @args) { my ($foo, $foo_missing); if (@args) { $foo = shift @args; } else { $foo_missing = 1; } my ($bar, $bar_missing); if (@args) { $bar = shift @args; } else { $bar_missing = 1; } if (@args) { croak "too many arguments"; } say "foo is missing" if $foo_missing; say "bar is missing" if $bar_missing; }

So, yes, I stubbornly stick to my solution.

Greetings,
-jo

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

Replies are listed 'Best First'.
Re^6: Distinguish between missing and undefined arguments with subroutine signatures
by LanX (Saint) on Jan 08, 2021 at 23:02 UTC
    >     say "foo is missing" if !$foo || $foo eq MISSING;

    hard to understand, if you wanna catch False you don't need missing at all. (tho I suppose you rather meant defined which is another beast )

    > So, yes, I stubbornly stick to my solution.

    I'm happy to agree, since I have unfortunately more urgent stuff to deal with now! :)

    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://11126636]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found