#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Use of uninitialized value - Grrr...


    Getting this error: "Use of uninitialized value in alarm at ./MyScript.pl line 59."

    Anyone have any idea why? I clearly define this variable with a default value of 0 and it's in scope when its used:

    Code:
    # Defaults 
    my $target      = 'NULL'; 
    my $em_result   = 0; 
    my $cur_alarm   = 0; 
    my $act_alarm   = 0; 
    my $timeout     = 600; 
    
    
    if (!defined $clopts{t}) {     
      printf(STDERR "Error: You must specify a target.\n");     
      usage();     
      exit(1); 
    } else {     
      $target = $clopts{t};
    }    
    
    eval {     
      local $SIG{ALRM} = sub { die "cmdtimeout"; };     
      alarm($timeout);     
      $data = `$ssh -l $fuser $target $test_cmd`;     
      $retcode = $? >> 8;     
      $cur_alarm = sprintf("%d", alarm());     # Line 59
      alarm(0);
    };
    I've also tried:

    Code:
    $cur_alarm = alarm();
    Both methods have worked in other places but first time I'm doing this inside an eval block.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Doing an alarm() will not work. Take a look at the below code snippet (with use warnings)

    Code:
    [user@host ~]$ cat test.pl
    #! /usr/bin/perl
    use warnings;
    use strict;
    
    my $x = alarm();
    print "$x\n";
    [user@host ~]$ ./test.pl
    Use of uninitialized value $_ in alarm at ./test.pl line 5.
    0
    [user@host ~]$
    If no arguments are supplied to alarm(), then the value in $_ is considered. In other places, it might have worked for you, because co-incidentally you might have had some numeric value in $_ (or you did not use warnings)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    But alarm() with no arguments is supposed to return the current counter and this has worked in other places.

    If I actually print alarm() instead of trying to save it in a var for later it works (but also complains):

    Code:
    eval {     
      local $SIG{ALRM} = sub { die "cmdtimeout"; };     
      alarm($timeout);     
      $data = `$ssh -l $fuser $target $test_cmd`;     
      $retcode = $? >> 8;     
      print STDOUT "alarm=" . alarm() . "\n";  
      $cur_alarm = sprintf("%d", alarm());     # Line 59
      alarm(0);
    };
    
    Use of uninitialized value in alarm at ./MyScript.pl line 61.
    alarm=598
    Use of uninitialized value in alarm at ./MyScript.pl line 62.
    em_result=0
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    Instead of shelling out the ssh call in an eval block, IMO it would be better to use Net::SSH2. That would give you more control over the process.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by FishMonger
    Instead of shelling out the ssh call in an eval block, IMO it would be better to use . That would give you more control over the process.
    Unfortunately these are solaris 11 storage appliances and Net::SSH2 is horrible buggy with the version of ssh (and I'm not allowed to change it)

    I use Net::SSH pretty much everywhere else though... besides this is a very simple query I'm running thru ssh returning only a single integer.

    I've actually got motivated to tackle this with python just for giggles... the rest of my team has switched to python and keeps riding me to standardize.

IMN logo majestic logo threadwatch logo seochat tools logo