Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by kyle (Abbot)
on Sep 19, 2008 at 19:40 UTC ( [id://712597]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: What the heck does "tests->{$stype} ||= {};" do?
by Bloodnok (Vicar) on Sep 20, 2008 at 10:38 UTC
    ... 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.

        Thanx for that ikegami, - as usual you've spotted the deliberate mistake ;-)

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found