Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Keep It Simple, Stupid
 
PerlMonks  

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( { 'log.name' => 'data load', 'rows' => '1', 'directory' => 'smsc$root:[statistics]', 'node' => 'YTSMSC', 'username' => 'smsc', 'passwd' => 'XxXx', 'localpath' => '/usr/local/reporting/ytsmsc', 'machine' => '172.23.2.2' }, '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): ./u.pl --ne=floom --conf=t +est.conf ?remote.passwd? >>directory localpath log.name machine node passwd rows username directory => smsc$root:[statistics] localpath => /usr/local/reporting/ytsmsc log.name => data load machine => 172.23.2.2 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): ./u.pl --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!

Comment on Confusion when about accessing a hash in an object
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-04-20 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls