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

Why is "undef" after passing not readonly anymore?

by LanX (Bishop)
on Sep 05, 2012 at 18:14 UTC ( #991907=perlquestion: print w/replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:

DB<174> use Scalar::Util qw(readonly) DB<175> sub tst { print readonly($_[0]) ? "r" : "rw" } DB<176> tst 0 r DB<177> tst undef rw DB<179> print readonly(undef) ? "r" : "rw" r
Bug or feature???

Cheers Rolf

UPDATE: to answer my own question, readonly only tells me if I can modify an alias w/o problems within a sub. The original undef in the outer scope isn't changed anyway.
DB<181> sub tst { $_[0] = 42 } DB<182> tst 5 Modification of a read-only value attempted at (eval 119)[/usr/share/p +erl/5.14/] line 2. DB<183> tst undef => 42 # really?

Replies are listed 'Best First'.
Re: Why is "undef" after passing not readonly anymore?
by Tux (Abbot) on Sep 05, 2012 at 18:25 UTC

    See Juerd's talk "When undef isn't". When it doesn't show, disable CSS.

    Enjoy, Have FUN! H.Merijn
      only sparse slides, no deeper explanation?

      Cheers Rolf

        Sorry, "you had to be there" :)
Re: Why is "undef" after passing not readonly anymore?
by philiprbrenan (Monk) on Sep 06, 2012 at 00:34 UTC

    It's to permit autovivification:

    use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); sub aaa($) {my ($a) = @_; $a->{a} = 1; say STDERR dump($a); } aaa({}); aaa(undef); aaa(0);


    { a => 1 } { a => 1 } Can't use string ("0") as a HASH ref while "strict refs" in use at C:\ +Phil\Perl\PerlMonks\ line 8.

      Good point, but I think it's even simpler!

      There are so many cases where undef is occasionally passed as argument, that inner variables of a sub like $_[0] must be writable.

      Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991907]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-01-24 02:54 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (255 votes). Check out past polls.