Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2017-01-20 11:24 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (174 votes). Check out past polls.