Plankton has asked for the wisdom of the Perl Monks concerning the following question:
I cannot seem to figure out why my use of a hash of hash doesn't work when I integrate my test script into a Object Orientated Perl module.
Here's an abbreviated version of my test script:
The output of this is what one would expect:#!/usr/bin/perl -w use strict; my %TZMap = ( sjc => { TZ1 => 'PST', TZ2 => 'PDT', TZFILE => '/usr/share/zoneinfo/America/Los_Angeles', }, blr => { TZ1 => 'IST', TZ2 => 'IST', TZFILE => '/usr/share/zoneinfo/Asia/Calcutta', }, ); sub testmap { my $tz = `/bin/date +%Z`; chomp($tz); my $site = `/bin/hostname | cut -d- -f2 | cut -c1-3`; chomp($site); my $tzfile = $TZMap{$site}->{TZFILE}; print "$site:" . $TZMap{$site}->{TZ1} . ":" . $TZMap{$site}->{ +TZ2} . ":$tz\n"; } testmap();
Now I moved this same declaration of the TZMap into a Perl module where I hope to check systems' timezone info like so ...sjc:PST:PDT:PST
When I run the code that uses this Perl module the output I get is ...package MAN::ER::SRole::Base; use strict; use base qw(MAN::ER::SRole); use MAN::ER::SRole; ... code omitted ... my %TZMap = { ... code omitted ... }; ... code omitted ... sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = $class->SUPER::new(); bless($self, $class); return $self; } ... code omitted ... sub check_timezone { my ($self) = @_; my $site = `/bin/hostname | /bin/cut -d- -f2 | /bin/cut + -c1-3`; chomp($site); my $tz = `/bin/date +%Z`; chomp($tz); my $tzfile = $TZMap{$site}->{'TZFILE'}; print Dumper(%TZMap); print "$site:" . $TZMap{"$site"}{TZ1} . ":" . $TZMap{$site}->{ +TZ2} . ":$tz\n"; if ( $TZMap{$site}->{TZ1} eq $tz or $TZMap{$site}->{TZ2} eq $t +z ) { $self->log_okay_message( "yada yada\n"); } else { $self->log_error_message("blah blah\n" ); } } sub check { my ($self) = @_; $self->clear_result(); $self->_common('check'); $self->ensureprocess($_) foreach @processes; $self->check_timezone(); }
... there are no values in the has. What am I doing wrong here? and BTW we are using a vert old version of Perl (v5.6.1) maybe that's the issue but I hope not.$VAR1 = { 'HASH(0x88eb640)' => undef, 'sjc' => {} }; sjc:::PST
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What happened to my globally declared hash when moved to OO Perl code?
by friedo (Prior) on Mar 04, 2008 at 18:25 UTC | |
by Plankton (Vicar) on Mar 04, 2008 at 18:41 UTC | |
|
Re: What happened to my globally declared hash when moved to OO Perl code?
by j1n3l0 (Friar) on Mar 04, 2008 at 23:31 UTC |