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

Reference problem?

by hotshot (Prior)
on Oct 07, 2003 at 16:45 UTC ( [id://297331]=perlquestion: print w/replies, xml ) Need Help??

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

Hello guys!

I have a weired problem I have the folowing function:
sub saveVolumes { my $volInfo = shift; # supposed to be hash reference (hash of ha +shes) print Dumper($volInfo); print "volumes: $volInfo->{volumes}{0}, $volInfo->{volumes}\n"; # more stuff here }
The problem is with the output from the prints, I get to console:
$VAR1 = { 'volumes' => 'none', 'maxVolId' => 'none', 'isInternal' => 'true' }; volumes: 0 1, none value: 0 1 value: 0 1 2 3
A you can see, under $volInfo->{volumes} I get different values in the dump and the regular print after it. How can that be?

Replies are listed 'Best First'.
•Re: Reference problem?
by merlyn (Sage) on Oct 07, 2003 at 16:52 UTC
    $volInfo->{volumes}{0}, $volInfo->{volumes}
    Those both can't be user-defined data. The value at the key of volumes is either your data, or a hashref to more of your data, but not both. I bet if you add use strict, your program will stop working.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      As you said, I indeed expect to get in the volumes key a data or a hash ref (and not both). I had a bug in the program so I added the print lines, and then I got what I got, this is the source of my bug but I can't understand how I can get such a thing.
      The following is the full function (with debugs):
      sub readClusterVolumes { my %volumesInfo; print "checkpoint 1:\n", Dumper(\%volumesInfo); print "vol: ", $volumesInfo{volumes}, ', ', $volumesInfo{volumes}{ +0}, ', ', $volumesInfo{volumes}{1}, "\n"; my @volumesNodeIDs = &getParam($PARAMS_VOLUMES_NODE_LIST); # shou +ld return (0 1) or nothing dependin whether the parameter exists my (%volumes, $tmp, $volumeKey); $volumesInfo{maxVolId} = &getParam($PARAMS_MAX_VOLUME_ID); $volumesInfo{isInternal} = &getParam($PARAMS_INTERNAL_DISKS); for (my $i = 0; $i < @volumesNodeIDs; $i++) { print "in for loop\n"; ($volumeKey = $PARAMS_NODEX_VOLUMES) =~ s/X/$volumesNodeIDs[$i +]/; $tmp = &getCDBParam($volumeKey, $TRUE); if ($tmp && $tmp ne '') { print "in if, tmp :$tmp\n"; $volumes{$volumesNodeIDs[$i]} = $tmp; } } print "checkpoint 2:\n", Dumper(\%volumesInfo); print "vol: ", $volumesInfo{volumes}, ', ', $volumesInfo{volumes}{ +0}, ', ', $volumesInfo{volumes}{1}, "\n"; if (scalar(keys(%volumes)) == 0) { print "no voluems #######\n"; $volumesInfo{volumes} = 'none'; } else { print "voluems exist #######\n"; $volumesInfo{volumes} = \%volumes; } print "checkpoint 3:\n", Dumper(\%volumesInfo); print "vol: ", $volumesInfo{volumes}, ', ', $volumesInfo{volumes}{ +0}, ', ', $volumesInfo{volumes}{1}, "\n"; return \%volumesInfo; };
      I will also add that this happens in the second call to the function. in the first call the output is:
      checkpoint 1: $VAR1 = {}; vol: , , in for loop in if, tmp :0 1 2 3 4 in for loop in if, tmp :0 1 2 3 4 checkpoint 2: $VAR1 = { 'volumes' => {}, 'maxVolId' => '4', 'isInternal' => 'false' }; vol: HASH(0x8479e1c), , voluems exist ####### checkpoint 3: $VAR1 = { 'volumes' => { '1' => '0 1 2 3 4', '0' => '0 1 2 3 4' }, 'maxVolId' => '4', 'isInternal' => 'false' }; vol: HASH(0x88d8144), 0 1 2 3 4, 0 1 2 3 4
      and in the second call I get:
      checkpoint 1: $VAR1 = {}; Use of uninitialized value in print at /vobs/mng/exadmin/var/www/cgi-b +in/lib/clusterEdit.pl line 21, <SESS> line 60. Use of uninitialized value in print at /vobs/mng/exadmin/var/www/cgi-b +in/lib/clusterEdit.pl line 21, <SESS> line 60. Use of uninitialized value in print at /vobs/mng/exadmin/var/www/cgi-b +in/lib/clusterEdit.pl line 21, <SESS> line 60. vol: , , checkpoint 2: $VAR1 = { 'volumes' => {}, 'maxVolId' => 'none', 'isInternal' => 'false' }; vol: HASH(0x847a2c0), , no voluems ####### checkpoint 3: $VAR1 = { 'volumes' => 'none', 'maxVolId' => 'none', 'isInternal' => 'false' }; vol: none, 0 1 2 3 4, 0 1 2 3 4
      I removed some Unintialized value... warnings I got, so it will be more clearer. You can see that it somehow 'remembers' the old values from the first call (the last line of the second output). I can't understand how that can be.
      All I want under the volumes key is none where there are no volumes, and the volumes themselvs when volumes exist.
Re: Reference problem?
by thens (Scribe) on Oct 07, 2003 at 17:29 UTC

    It is the same old problem. Not using strict and warnings. Make use of them whenever you write some meaningful perl scripts. ( can excuse the JAPHs !! ).

    See this for some good practices while coding in perl.

    -T

    use Perl; use strict;

Log In?
Username:
Password:

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

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

    No recent polls found