Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Using Variable outside of loop with Net::SSH

by Nich240 (Initiate)
on May 08, 2015 at 13:22 UTC ( [id://1126088]=perlquestion: print w/replies, xml ) Need Help??

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

I am using Net::SSH2 to ssh into a NAS box, and run a command. I can get the command results while I am inside the loop. When I am outside the loop, I don't see the variable if it contains the output. However, I can see the variable if a new value is defined.

In the code below, I assign $result to $_. If I am inside the loop, I can see it. If I am outside the loop I cannot. However, I can define $result="test" before I leave the loop, and I can then see it outside the loop. It appears that I cannot pass the $_ (output of the NAS Command)

How do I pass the $_ outside the loop?

Here is my code:
#!C:\strawberry\perl\bin use warnings; use strict; use Net::SSH2; use MIME::Lite; use Config::Tiny; my $ssh2 = Net::SSH2->new(); my $ipaddr="w.x.y.z"; my $username="username"; my $password="password"; $ssh2->connect("$ipaddr") || die "PROBELM - $!"; $ssh2->auth_password("$username","$password") || die "Username +/Password not right"; my $chan = $ssh2->channel(); $chan->blocking(0); $chan->exec('export NAS_DB=/nas;/nas/bin/nas_fs -list'); print "Chan= $chan\n"; my $result; while (<$chan>) { $result=$_; #Desired output. Only works inside the loop. $result='test'; #Used for testing, this works inside and outside of lo +op #print $result; #Works } print $result; #Works if $result is set, but not with a $_; print "END";

Replies are listed 'Best First'.
Re: Using Variable outside of loop with Net::SSH
by aaron_baugher (Curate) on May 08, 2015 at 13:30 UTC

    You can't get at the loop's inner value of $_ outside the loop, because the while command automatically localizes it. Just do it the way you do here with $result, by assigning it to a variable that does exist outside the loop.

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

      I understand that $_ is local. This is why I assign it to the $results variable. That variable is global. The strange part is that $result="test" will work outside the loop. To me, this would seem that since $result is initialized with "my" before the loop, I can use it inside out outside the loop. And, this works for almost all variables, $results="test" is good. So it seems to be correct. However, the problem is when I use $result=$_

        Doing $result=$_ inside the loop should work, but after the loop, $result will have the value of $_ from the last loop that executed. That might not be what you expected. I would test it by trying something like this:

        my $result; while(<$chan>){ $result = $_; print "Inside loop: $result\n"; } print "Outside loop: $result\n";

        That should show you what's being assigned in each loop, and the last inside loop should show the same value as what ends up outside it.

        One other thing: if you have other code inside the loop which you've removed for the sake of this sample, make sure it's not changing the value of $_ before you assign it to $result.

        Aaron B.
        Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2024-04-23 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found