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";

In reply to Using Variable outside of loop with Net::SSH by Nich240

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.