Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: Possible useless use of map

by almut (Canon)
on Mar 18, 2010 at 10:39 UTC ( [id://829378]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Possible useless use of map
in thread Possible useless use of map

No, $hr->{stuff} alone doesn't autovivify the element. It's the dereferencing operation

$hr->{stuff}->{foo} ^^

that autovivifies, not merely accessing the hash element.

As for side effects with tied hashes, I mean that arbitrary code could be associated with the tied read operation.

Replies are listed 'Best First'.
Re^5: Possible useless use of map
by Anonymous Monk on Mar 18, 2010 at 10:57 UTC
    :D You're not paying attention
    $ perl -MDDS -w -le " my $hr; $hr->{stuff}; Dump($hr)" $HASH1 = {}; $ perl -MDDS -Mwarnings -le " my $hr; $hr->{stuff}; Dump($hr)" Useless use of hash element in void context at -e line 1. $HASH1 = {};
    $hr used to be undef, after $hr->{stuff} it is a hashref.

    Curiously, my little test reveals Data::Dump::Streamer breaks the -w switch .

      I see what you mean.  So, one could conclude that getting no warning in the OP's case (with map) is in fact the appropriate behavior (as the $hr->{$_} is not useless), while getting the warning with for seems more questionable  (in which case I wouldn't consider it a bug to silence a useless "Useless use of..." warning :)

        One could if you ignore intent, but I wouldn't support that conclusion. The warning deals with return value and where it ends up. perldiag says:
        ... Very often this points not to stupidity on your part, but a failure of Perl to parse your program the way you thought it would...
        so if you were to adapt perldiag example with andreas' example you get
        #~ perl -MDDS -Mstrict -Mwarnings -le " my $hr; map { @{$hr->{$_}}{qw' + a b c '} = $_,$_,$_ ; } 1; Dump($hr) " #~ perl -MDDS -Mstrict -Mwarnings -le " my $hr; map { @{$hr->{$_}}{qw' + a b c '} = ( $_,$_,$_ ); } 1; Dump($hr) " #~ perl -MDDS -Mstrict -Mwarnings -le " my $hr; $_ = 1; @{$hr->{$_}}{q +w' a b c '} = $_,$_,$_ ; Dump($hr) " #~ perl -MDDS -Mstrict -Mwarnings -le " my $hr; $_ = 1; @{$hr->{$_}}{q +w' a b c '} = ( $_,$_,$_ ); Dump($hr) " $ perl -MDDS -Mstrict -Mwarnings -le " my $hr; map { @{$hr->{$_}}{qw' +a b c '} = $_,$_,$_ ; } 1; Dump($hr) " $HASH1 = { 1 => { a => 1, b => undef, c => undef } }; $ perl -MDDS -Mstrict -Mwarnings -le " my $hr; map { @{$hr->{$_}}{qw' +a b c '} = ( $_,$_,$_ ); } 1; Dump($hr) " $HASH1 = { 1 => { a => 1, b => 1, c => 1 } }; $ perl -MDDS -Mstrict -Mwarnings -le " my $hr; $_ = 1; @{$hr->{$_}}{qw +' a b c '} = $_,$_,$_ ; Dump($hr) " Useless use of a variable in void context at -e line 1. Useless use of a variable in void context at -e line 1. $HASH1 = { 1 => { a => 1, b => undef, c => undef } }; $ perl -MDDS -Mstrict -Mwarnings -le " my $hr; $_ = 1; @{$hr->{$_}}{qw +' a b c '} = ( $_,$_,$_ ); Dump($hr) " $HASH1 = { 1 => { a => 1, b => 1, c => 1 } }; $ $

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found