Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Why is "undef" after passing not readonly anymore?

by LanX (Canon)
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/perl5db.pl:640] line 2. DB<183> tst undef => 42 # really?

Comment on Why is "undef" after passing not readonly anymore?
Select or Download Code
Re: Why is "undef" after passing not readonly anymore?
by Tux (Monsignor) 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);

    Produces:

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

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-09-17 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls