Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: If Not Replace

by derby (Abbot)
on Sep 10, 2011 at 14:18 UTC ( #925248=note: print w/ replies, xml ) Need Help??


in reply to If Not Replace

You probably do not need this at all. You can rely on autovivification when setting the value and defined when accessing.

-derby


Comment on Re: If Not Replace
Re^2: If Not Replace
by tchrist (Pilgrim) on Sep 10, 2011 at 14:31 UTC
    Thereís no autoviv involved here. Autovivication is the implicit allocation of references of the proper type when undefined lvalues are dereferenced. There is no dereferencing here.

      Ah ... sorry I was wrongly thinking perl's auto extending of arrays is the same thing as autoviv.

      -derby
        Ah ... sorry I was wrongly thinking perl's auto extending of arrays is the same thing as autoviv.
        Youíre right in that both behaviors derive from a common principle that Perl takes care of memory allocation for you. This is very different from C, Java, or even Python, all of which blow up if you try to overindex an array, and certainly never auto‐allocate new ones to multiple levels as occurs with autoviv.

        Itís just that the way the jargon has developed in Perl culture, the word autoviv always involves implicit allocation of references (into previously undefined lvalues that are getting dereferenced), not merely extending an existing string, array, or hash with non-reference data.

        Non‐autoviv examples:

        my $s = "a"; $s .= "abcdefghijklmop"; my @a = (0 .. 4); $a[55] = "frog"; my %h = (red => 1, blue => 2, yellow => 3); $h{green} = 6.02e23;
        Autoviv examples:
        my $hr = undef; $hr->{radish} = "crunch"; # yes autoviv $hr->{banana} = "squish"; # no autoviv my @a = ("a" .. "z"); $a[0][1] = "new level"; # yes autoviv $a[0][9] = "new element"; # no autoviv my %h = ( ); $h{little}{dog} = "puppy" ; # yes autoviv $h{little}{cat} = "kitten"; # no autoviv $h{male}{cattle} = "bull"; # yes autoviv $h{male}{cat} = "tom"; # no autoviv

        See the difference?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-10-02 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (48 votes), past polls