Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Passing Str or undef ?

by John M. Dlugosz (Monsignor)
on Apr 26, 2011 at 12:43 UTC ( [id://901338]=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Hey, if using MooseX::Method::Signatures, how do you declare a parameter that can be passed a value of undef? I know something like Str $foo? will let me leave if off, and the functions sees it as undef if it wasn't passed. But it still complains if the caller explicitly passes an undef in that spot!

Replies are listed 'Best First'.
Re: Passing Str or undef ?
by Corion (Patriarch) on Apr 26, 2011 at 13:02 UTC

    I would assume that somewhere in MooseX::Types, it is declared how to pass an instance of a type or undef. Other languages use a Maybe(Str) construct for that, but I don't know what the Moose people believe should be used. Moose::Util::TypeConstraints claim that there is Maybe[`a], so maybe that's what you should use:

    Maybe[Str] $foo
Re: Passing Str or undef ?
by FunkyMonk (Chancellor) on Apr 26, 2011 at 13:49 UTC
    Using MooseX::Declare you can...
    use MooseX::Declare; class Foo { method baz(Str|Undef $x) { say $x // '[undef]'; } }; Foo->new->baz($_) for undef, "hi", [], {}; __END__ [undef] hi Validation failed for 'Tuple[Tuple[Object,Str|Undef],Dict[]]' with val +ue [ [ Foo=HASH(0x2645708), ARRAY(0x101e2... [+] Validation failed for 'Str' with value ARRAY(0x2105220) and Vali +dation failed for 'Undef' with value...

    MXD uses MooseX::Method::Signatures so I guess you'll be able to adapt the code above.

Re: Passing Str or undef ?
by stvn (Monsignor) on Apr 26, 2011 at 15:35 UTC

    Corion actually answered the question, but only by guessing, so I will restate his answer but with some more details and certainty.

    You have two choices here really, the first is a type union, which is written as Str | Undef and this basically says that the value can be either a String or Undefined. The second choice is the Maybe[`a] parameterized type, which would be written like this Maybe[Str]. This gives you basically the same thing as the type union but communicates the relationship differently by more explicitly saying "this *might* be undefined" not "this *can also* be undefined". My personal choice is always to do Maybe[`a] instead of the union in cases like these.

    -stvn
      Yes, thanks. I like the apparent semantics better of Maybe.

Log In?
Username:
Password:

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

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

    No recent polls found