in reply to For loop and Sub Routines

The problem is with the embedded "sub checkStatus".

It uses the global "$ssh", which gets nailed in at compile-time. (Using the term 'Compile-time' loosely).

I suggest taking the sub declaration outside the "for" loop, then passing in $ssh.

Update:Here is an illustration of the issue:

>perl -E 'for (1..2){say "In loop $_"; my $z=$_+20; x($_); sub x{my $y +=$_[0]; say "In Sub. Param= $y. z=$z"}}' In loop 1 In Sub. Param= 1. z=21 In loop 2 In Sub. Param= 2. z=21
When the parameter is "2", then $z should be 22. This would be the case if $z were passed-in.

             My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Replies are listed 'Best First'.
Re^2: For loop and Sub Routines
by nickt9999 (Acolyte) on Jul 04, 2013 at 17:51 UTC

    Thanks for the quick reply...

    As I am a bit of a newbie to this, are you suggesting.

    I am on the train home so cant test this for sure...

    for my $router ( @$router_data ) { my $tty; ### Create SSH Session print "\n" . "Logging into $router->{routerName}..." . " \n"; my $ssh = Net::SSH::Expect->new ( host => $router->{routerName}, password=> $pWord, user => $uName, timeout => '3', raw_pty => 1 ); ### Logon to the Router $tty = $ssh->login(); if($tty !~ /$router->{routerName}/) { die "Error: no prompt received.\n\n"; } if ( $Action eq 'status' ) { checkStatus($router->{routerName,$ssh}); } } sub checkStatus { my $routerName = $_[0]; my $ssh = $_[1]; my $test; if ($routerName eq 'asr01') { $test = $ssh->exec("show run | in hostname "); } else { $test = $ssh->exec("show clock"); } print $test; }
      The call should read:
      checkStatus($router->{routerName}, $ssh);
      ALso, it is more common (and lazier, and more idiomatic) to code the passed parameters this way:
      sub checkStatus { my ($routerName, $ssh) = @_; ...
      This allows you to add parameters easily, and makes parameter positions visually identifiable without having to examine indexes.

                   My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.