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

Re: Distinguish between missing and undefined arguments with subroutine signatures

by LanX (Sage)
on Dec 27, 2020 at 18:52 UTC ( #11125808=note: print w/replies, xml ) Need Help??


in reply to Distinguish between missing and undefined arguments with subroutine signatures

> Is there a common idiom how to handle such case with signatures?

The rule of thumb is to use a default which is defined.

Using undef as default should be redundant and will force you to check @_ - if still possible (untested)

Sorry, your code is longish (aka TLDR), if I didn't get it right, please consider condensing it to the relevant part.

This question could be actually very relevant if there is a real use case! P5P is considering to drop the population of @_ when signatures are used.

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

update

) not

# Standard signature with optional second arg. sub sig_std ($what, $arg=undef) {

Replies are listed 'Best First'.
Re^2: Distinguish between missing and undefined arguments with subroutine signatures
by dave_the_m (Monsignor) on Dec 27, 2020 at 19:27 UTC
    P5P is considering to drop the population of @_ when signatures are used
    But only after alternative facilities for argument instrospection have been provided. See the proposal on the p5p mailing list.

    In particular, in

    sub foo($x, ??$has_y, $y = 0) { ... }
    $has_y is true if and only if a second arg is passed to the sub.

    Dave.

      sub foo($x, ??$has_y, $y = 0) { ... }

      Great! That's it. (Or better: Will be)

      Greetings,
      -jo

      $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re^2: Distinguish between missing and undefined arguments with subroutine signatures
by jo37 (Hermit) on Dec 27, 2020 at 19:16 UTC

    The use case I have in mind is a combined getter/setter for a perl object. Return the value when called without an additional arg, set the value when called with an arg. A call with an undefined arg clears the attribute - just like in my example.

    Maybe that's not a real world use case - I'm just playing with things.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      > combined getter/setter ... Return the value when called without an additional arg,

      from my experience are mutators written in a way to always return the value.

      > A call with an undefined arg clears the attribute - just like in my example.

      OK ... but in that case why do you have a default value at all?

      Anyway, if you want to allow to set to undef and don't wanna introspect @_ , then you're stuck in a kind of semipredicate problem.

      Such problems can be solved in Perl by setting a new "impossible" object MISSING as default, like

      use constant MISSING => bless {}, "MyPackg::__Missing__"

      and later

      sub mutator ($value = MISSING) { ...BODY... }

      (untested)

      If you see that an arguments equals to that "impossible" value blessed into your own private namespace below "MyPackg", you can be sure that it wasn't used by accident.

      HTH! :)

      update

      clearer rewording, clearer code

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

      ) of course it's possible that such a value is used, but extremely unlikely without intend.

        use constant MISSING => bless {}, "MyPackg::__Missing__"

        That's a very good solution.

        Thx.

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
        OK ... but in that case why do you have a default value at all?

        I don't want a default value. To my understanding it is needed for an optional value argument in a signature.

        Greetings,
        -jo

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-05-20 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (75 votes). Check out past polls.

    Notices?