Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

What the heck does "tests->{$stype} ||= {};" do?

by Anonymous Monk
on Sep 19, 2008 at 19:28 UTC ( [id://712591]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am having the great joy of figuring out someone else code. And I haven't seen this before ...

$tests->{$stype} ||= {}; my %tests = %{$tests->{$stype}};

... what the heck would that code be trying to do?

The $test is a big ugly global hash ref at the top of the code that is about 100 lines long so I will only post a small version that might have typos in it here:

my $tests = { all => { cost => \&cost_checker }, YEAM => { lipmons => sub { check_lipmons(@_, 'SD' => 3, 'ron' => 2, 'SP' => 3); }, dd => \&check_dd, stype => sub { check_stypes(@_, "EM"); }, heat_monr => sub { check_heatmonr(@_, 1); }, heat_pgm => sub { check_hpgm(@_, 'Solar'); }, }, ...

Thanks

Replies are listed 'Best First'.
Re: What the heck does "tests->{$stype} ||= {};" do?
by kyle (Abbot) on Sep 19, 2008 at 19:40 UTC

    You actually have two different variables, both named "tests". One is the hash, %tests, and the other is a scalar, $tests, which holds a reference to a hash. If you're not familiar with references, swing on over to perlreftut and perlref. After that, References quick reference is correctly named.

    The first line ($tests->{$stype} ||= {};) checks to see if $tests->{$stype} is a true value. If not, it sets it to "{}", which is a new, empty, anonymous hash reference. The intent of this is (probably) to ensure that $tests->{$stype} is a hash ref (in case it's empty), but it will actually do nothing if there's some other (true) value there already. It's equivalent to:

    if ( ! $tests->{$stype} ) { $tests->{$stype} = {}; }

    If you're interested in the "||=" operator, have a look at perlop.

    The second line (my %tests = %{$tests->{$stype}};) dereferences the hash reference in $tests->{$stype} and copies it to %tests. This is probably the reason for the first line. If there's no hash reference in there, the second line will die.

      ... the 2 lines in question could be restated, IMCO without losing (maybe even introducing) clarity, to:
      my %tests = %{$tests->{$stype} || {}};

      A user level that continues to overstate my experience :-))

        That's not equivalent. In your code, $tests->{$stype} is never assigned anything.

        But it's probably better. Assigning to $tests->{$stype} was probably a side-effect.

Re: What the heck does "tests->{$stype} ||= {};" do?
by jwkrahn (Abbot) on Sep 19, 2008 at 19:39 UTC

     $tests->{$stype} ||= {}; is short for  $tests->{$stype} = $tests->{$stype} || {}; which says that if  $tests->{$stype} is true keep the same value but if it is false then assign the value  {} to it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found