Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

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


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

But I suppose you don't want the sub missing() to be available as method the way ->foo() is?

That doesn't harm at all. Calling $obj->missing just returns false, which is correct :-)

However, your objection discovered a flaw in my approach. The missing sub needs to be prototyped to go one step further: multiple optional arguments. And then slurping starts hurting :-)

This leads to:

#!/usr/bin/perl use v5.16; use warnings FATAL => 'all'; package Foo; use experimental 'signatures'; use constant MISSING => bless {}, __PACKAGE__ . '::__missing__'; sub missing :prototype(;$) { @_ ? $_[0] && $_[0] eq MISSING : MISSING; } sub new ($class) { bless {}, $class; } sub foo ($self, $foo=missing, $bar=missing) { say "foo is missing" if missing $foo; say "bar is missing" if missing $bar; } package main; my $foo = Foo->new; say "none:"; $foo->foo; say "one:"; $foo->foo(1); say "two:"; $foo->foo(1, 2); print "\n"; say 'object foo is not missing' unless $foo->missing; __DATA__ none: foo is missing bar is missing one: bar is missing two: object foo is not missing

Greetings,
-jo

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

Replies are listed 'Best First'.
Re^4: Distinguish between missing and undefined arguments with subroutine signatures
by LanX (Saint) on Jan 08, 2021 at 22:03 UTC
    > And then slurping starts hurting :-)

    ++ for linguistic excellency but not for correctness. ;-)

    Some remarks:

    • you don't need a sub missing() just compare with eq
    • if you really need it make it a private sub with my $missing = sub {...}
    • since constants are implemented as subs, they'll be methods too °
    • slurping @vals can still handle both cases by checking the length
    Anyway, you seem to be very convinced, so good luck and enjoy! :)

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

    °) maybe consider namespace::clean

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-18 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found