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

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

by Anonymous Monk
on Sep 19, 2008 at 19:28 UTC ( #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'); }, }, ...


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 (Monsignor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://712591]
Approved by kyle
[LanX]: quick survey ... Module::Build, ExtUtils:: ModuleMaker or Module::Starter ?
[Your Mother]: Module::Install
[Your Mother]: :P
[LanX]: Moma knows best! ;-)
[Your Mother]: I am quite aware that Module::Build is MUCH more in favor with many monks but I had trouble with it every time I tried to use it and trouble with CPAN stuff that used it too.
[Your Mother]: Take my advice with a grain of salt. I haven't done a new CPAN release in years at this point.
[LanX]: I want to author a new module for CPAN w/o complicated build structure
[LanX]: ther is also Dist::Zilla :-/
[LanX]: How to make a CPAN Module Distribution from 2002

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2017-08-18 17:39 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (306 votes). Check out past polls.