Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: RFC: New style for argument check in subs

by Jenda (Abbot)
on Sep 05, 2012 at 11:30 UTC ( #991812=note: print w/replies, xml ) Need Help??

in reply to RFC: New style for argument check in subs

"missing" ne "undefined"!

An undef may very well be the value you want to specify! There is a big difference between foo({bar => 15, baz => undef}) and foo({bar => 15}) and I would definitely not expect to get the default value of baz in the first case.

Enoch was right!
Enjoy the last years of Rome.

Replies are listed 'Best First'.
Re^2: RFC: New style for argument check in subs
by LanX (Bishop) on Sep 05, 2012 at 11:41 UTC
    As I already said, accepting undef as valid argument is such a special and rare case that it should be covered by another approach. (see footnotes in OP)

    Here using ternaries is far more explicit and shows that a special argument undef is accepted.

    my $a = exists $arg{a} ? $arg{a} : 'else case';

    But I agree that it's better to name the error die('Undefined argument!') instead of 'Missing argument' to avoid such misunderstandings.

    Cheers Rolf

      Not rare at all! Both as unnamed and as named parameter used in DBI + DBD::CSV:

      use DBI; my $dbh = DBI->connect ( "dbi:CSV", # Unnamed, driver undef, # Unnamed, username undef, # Unnamed, password { f_ext => ".csv/r", # Named and defined f_schema => undef, # Named and undefined });

      FWIW all NULL values in a database are refered to through undef, so you'll see a lot of those in both named and unnamed arguments to functions and methods.

      Enjoy, Have FUN! H.Merijn
        Good point.

        But in those cases checks for existence should be explicit.

        UPDATE: You convinced me that

        *** Unpack Named First

        is a danegrous option. Thanks :)

        Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991812]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-04-27 03:50 GMT
Find Nodes?
    Voting Booth?