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

Re^3: Distinguish between missing and undefined arguments with subroutine signatures (semipredicate problem)

by LanX (Saint)
on Dec 27, 2020 at 19:37 UTC ( [id://11125815]=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

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

  • Comment on Re^3: Distinguish between missing and undefined arguments with subroutine signatures (semipredicate problem)
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: Distinguish between missing and undefined arguments with subroutine signatures (semipredicate problem)
by jo37 (Deacon) on Dec 27, 2020 at 20:27 UTC
    use constant MISSING => bless {}, "MyPackg::__Missing__"

    That's a very good solution.

    Thx.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      > That's a very good solution.

      Thanks, that's my favorite solution for the semipredicate problem. :)

      But in this particular case, I'd say checking @_ is better.

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

Re^4: Distinguish between missing and undefined arguments with subroutine signatures (semipredicate problem)
by jo37 (Deacon) on Dec 27, 2020 at 20:47 UTC
    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$
      > To my understanding it is needed for an optional value argument in a signature.

      Ah ... I see. Using a slurpy parameter @settings will solve this and fits much better into your use-case.

      from perlsub

      After positional parameters, additional arguments may be captured in a slurpy parameter. The simplest form of this is just an array variable:
      sub foo ($filter, @inputs) { print $filter->($_) foreach @inputs; }

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

        Using a slurpy parameter @settings will solve this and fits much better into your use-case.

        Sadly, it does not. I already considered this. It does not prevent the sub from being called with more arguments than allowed.

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-18 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found