Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

RE: undefs and functions

by Russ (Deacon)
on Aug 02, 2000 at 05:15 UTC ( #25645=note: print w/replies, xml ) Need Help??

in reply to undefs and functions

Here's what I came up with... For each key in the structure, we provide the value we want for the default (if that key is missing). Then we work through the structure, replacing any undefined values with the desired default.

I'm not at all sure this is the kind of solution you want, (and I'm not entirely certain it even works like I think it does) but it was a lot of fun to tinker with it. :-)

use Data::Dumper; # Trying to guarantee this: #$self->{db}->{offer}->{product_id}->{name} # We start with a fully formed hashref $self->{db}->{offer}->{product_id}->{name} = 1; # and undef somewhere in the middle undef $self->{db}; # Show the structure just to prove it is too short print Dumper $self; my $Last = $self; for (['db',{}], ['offer',{}], ['product_id',&func], ['name',42]){ defined $Last->{$_->[0]} or $Last->{$_->[0]} = $_->[1]; $Last = $Last->{$_->[0]}; } print Dumper $self; # func() is the default for key 'product_id' above sub func{ {} }
$VAR1 = { 'db' => undef }; $VAR1 = { 'db' => { 'offer' => { 'product_id' => { 'name' => 42 } } } };

Brainbench 'Most Valuable Professional' for Perl

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-12-06 07:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found