SPUG: VEXED "Barbarian needs food badly" Scalar inside Hash problem

Peter Darley pdarley at kinesis-cem.com
Thu Dec 19 08:23:17 CST 2002


VEXED "Barbarian needs food badly" Scalar inside Hash problemSouza,
    Just a shot in the dark, but you might have some extra trailing
characters in WhoAmI.txt, like a trailing space or a trailing \r or \n, that
is preventing the name to match what's in the hash, but would still look
correct when you printed it.
Thanks,
Peter Darley
  -----Original Message-----
  From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org]On Behalf Of
Souza Jonathan
  Sent: Wednesday, December 18, 2002 7:49 PM
  To: SPUG
  Subject: SPUG: VEXED "Barbarian needs food badly" Scalar inside Hash
problem


  I have a vexing problemo.  First I have done this before in other
programs, but can't get it to work here.  I want to use a variable inside a
hash.



  Here is my code snippet it is a segment out of a larger package.  My
problem in in SetupEnvVariables.






  package monitor;



  #----------
  #Method Name: SetupEnvVariables
  #Purpose: Modify system environment variables
  #Parameters: None
  #Calls: Win32::AdminMisc::SetEnvVar && Win32::AdminMisc::DelEnvVar
  #Sets: None
  #Returns: 1 if completed || undef if error
  #----------
  sub SetupEnvVariables
  {
          my ($self) = @_;
          my ($machine_name, $env_value);

          #MAKE ANY CHANGES TO THE ENVIRONMENTAL VARIABLES
          $machine_name = $self->{WHOAMI}{MACHINE_NAME};
          print "MN: $machine_name\n";
#prints SP2007 which is correct
  #print Dumper($self->{MACHINES});                                 #dumps
correctly

  print "Hours: $self->{MACHINES}{$machine_name}{HOURS_TO_RUN}\n";  #error
says "Use of uninitialized value in string.."
  print "Hours2: $self->{MACHINES}{SP2007}{HOURS_TO_RUN}\n";        #prints
correctly -some integer value.

          if (exists $self->{MACHINES}{$machine_name}{ENV_VARIABLES})
#always doesn't exist because $machine_name is not being

          {
                  #blah blah
          }



  #etc....
  }




  #----------
  #Method Name: New
  #Purpose: Constructor
  #Parameters: None
  #Calls: Configure
  #Sets: Creates default hash and reference to objects methods
  #Returns: Object reference
  #----------
  sub New
  {
          my ($class) = @_;
          my $self = {
                                  'WHOAMI' => {},
                                  'MACHINES' => {},
                                  };

          bless $self, $class;
  print "In New\n";
          $self->Configure();

          return $self;
  } #End New





  #----------
  #Method Name: Configure
  #Purpose: Read any configuration files
  #Parameters: None
  #Calls: Automation::Read_ConfigXML
  #Sets: $self->{WHOAMI}{MACHINE_NAME} & $self->{WHOAMI}{XML_FILE}
  #Returns: 1 Completed || undef errors
  #----------
  sub Configure
  {
          my ($self) = @_;
          my ($key, $value, $open);

  print "IN CONFIGURE\n";
          #Read WhoAmI.txt
          if (!($open = open (FH, "<", "WhoAmI.txt")))
          {
                  sleep (20);
                  if (!($open = open (FH, "<", "WhoAmI.txt")))
                  {
                          print "Failed to open WhoAmI.txt\n";
                          return undef;
                  }
          }

          if ($open)
          {
                  while (<FH>)
                  {
                          ($key, $value) = split /=/;
                          $self->{WHOAMI}{$key} = $value;
                  }
                  close FH;

                  my $auto = Automation->New();
                  $auto->Read_ConfigXML($self->{WHOAMI}{XML_FILE},
$self->{MACHINES});
  #print Dumper($self->{MACHINES}); #dumps correctly
                  undef $auto;
                  return 1;
          }

          return undef; #should never hit this

  } #End Configure




  #########################
  package main;
  use strict;

  my $monitor = Monitor->New();
  $monitor->SetupEnvVariables();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/archives/spug-list/attachments/20021219/098adb43/attachment.htm


More information about the spug-list mailing list