Dru has asked for the wisdom of the Perl Monks concerning the following question:

Can someone tell me why Perl is not letting me use local here? This is the error I'm getting:
Global symbol "$stout" requires explicit package name at ./temp.pl line 48. Global symbol "$stout" requires explicit package name at ./temp.pl line 49. Global symbol "$stout" requires explicit package name at ./temp.pl line 71. Global symbol "$stout" requires explicit package name at ./temp.pl line 75. Execution of ./temp.pl aborted due to compilation errors.
From everything I read, I thought I could declare $stout as a local variable so that it can also be used in the run subroutine. I tried to make it global declaring it with my outside of the the subs, but I got some strange results. I'm trying to figure out what code ssh returns on a successfull connection.
45 sub connect { 46 47 for(@host){ 48 local $stout = run($_); 49 unless($stout == 256) { 50 $success++; 51 last; 52 } 53 print "$_ not responding\n"; 54 } 55 print "All hosts not responding" unless $success; 56 57 } 58 59 60 # 61 62 sub run { 63 64 my $host = $_[0]; 65 my $user = 'root'; 66 my $cmd = '/usr/local/scripts/temp.sh'; 67 68 ssh("$user\@$host", $cmd); 69 70 71 if($stout == 256) { 72 print "Problem sshing: $!\n" and exit; 73 } 74 else{ 75 print "STDOUT is: $stout\n"; 76 }
Thanks,
Dru

Replies are listed 'Best First'.
Re: Problems Using Local
by davorg (Chancellor) on Jan 27, 2003 at 17:06 UTC

    local doesn't do what you think it does. It doesn't declare a new local variable. It temporarily gives a new value (undef) to a previously existing package variable.

    You can pre-declare variables with use vars, our or my. In this case you almost certainly want my.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Problems Using Local
by Aristotle (Chancellor) on Jan 27, 2003 at 16:53 UTC
    That code doesn't seem to make much sense. $stout is never assigned to, yet tested on several occasions. I doubt you want local anyway - use my.

    Makeshifts last the longest.

Re: Problems Using Local
by Aragorn (Curate) on Jan 27, 2003 at 16:48 UTC
    If you declare $stout as a global I think it should work. Use use vars qw($stout); or our $stout; somewhere at the start of your script should do it.

    What sort of "strange results" did you get when using my $stout?

    Arjen

      Ugh, I should not answer questions on a monday afternoon. See the replies from Aristotle and davorg for correct information.

      Arjen

Re: Problems Using Local
by OM_Zen (Scribe) on Jan 27, 2003 at 22:35 UTC
    Hi,

    The scoping of local only actually undefines a variable , you could use my to make it scope specific in the code ,so that it is global like my $stout ,local actually is not the one that is to be used in this part of the code for what you wanted to do actually