Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Confusion when about accessing a hash in an object

by jimbus (Friar)
on Nov 30, 2005 at 18:33 UTC ( #513041=perlquestion: print w/replies, xml ) Need Help??
jimbus has asked for the wisdom of the Perl Monks concerning the following question:

I'm declaring an object thusly:
sub getProperties { my $props = {}; . . . if ($name ne "") { $props->{$name} = $value; } . . . bless($props); return $props; }

And I'm trying to access with the following method:

sub get { my $self = shift; my $propName = shift; return $self->{$propName}; }

I'm not getting anything back from the return.

To debug I used Data::Dumper on $self and there's data there:

$VAR1 = bless( { '' => 'data load', 'rows' => '1', 'directory' => 'smsc$root:[statistics]', 'node' => 'YTSMSC', 'username' => 'smsc', 'passwd' => 'XxXx', 'localpath' => '/usr/local/reporting/ytsmsc', 'machine' => '' }, 'Properties' );

I also grabbed part of one of the examples from perlobj as I was researching did the following:

sub get { my $self = shift; my $propName = shift; print "?$propName?\n"; #1 my @keys = @_ ? @_ : sort keys %$self; #2 my @keys = ($propName); print ">>@keys\n"; foreach my $key (@keys) { print "\t$key => $self->{$key}\n"; } # return $self->{'$propName'}; }

With line #1 uncommented I get the array of keys and the whole list of keys and values printed out:

[appadmin@NEOps2]/usr/local/reporting(159): ./ --ne=floom --conf=t +est.conf ?remote.passwd? >>directory localpath machine node passwd rows username directory => smsc$root:[statistics] localpath => /usr/local/reporting/ytsmsc => data load machine => node => YTSMSC passwd => xXxX rows => 1 username => smsc

Uncommenting #2 gives me the expected value in the key array, but prints a blank value in the key and value area:

[appadmin@NEOps2]/usr/local/reporting(160): ./ --ne=floom --conf=t +est.conf ?remote.passwd? >>remote.passwd remote.passwd =>

I'm very confused, can anyone help me see the error of my ways?

UPDATE: Diotalevi pointed out that I wasn't actually asking for anything in the list. Apparently, I'd grabbed the wrong test file and wasn't actually reading the debug information... in other words, it was an ID 10 T error :)

Never moon a werewolf!

Replies are listed 'Best First'.
Re: Confusion when about accessing a hash in an object
by diotalevi (Canon) on Nov 30, 2005 at 18:41 UTC

    There doesn't appear to be a 'remote.passwd' entry in your data. Without one, you won't be able fetch it.

    You're missing the second argument to bless(). Without it, you're preventing subclassing.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://513041]
Approved by friedo
[stevieb]: glad I have an exceptionally high-quality test regimen for the Pi with...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2018-01-16 22:10 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (192 votes). Check out past polls.