I have written a function in perl (Thanks to perl monk) The below script works as expected and provides the desired output. I have been learning about functions and making function calls. I'm trying to make a function call by assigning it to a variable.
Now I tried making the function call my assigning it to a variable $p and that should throw an error if some thing goes wrong in the function. This is what i tried .use strict; use Carp; use warnings; use autodie; use feature qw/say/; use Net::SSH::Expect; print "\n[INFO] script Execution Started\n"; #-- ssh into a router my $ssh = Net::SSH::Expect->new( host => "ip addr", password => 'password', user => 'user name', raw_pty => 1, ); my $login_output = $ssh->login(); #--Function to read config file and execute the commands on the rout +er accordingly. sub mysub { my ($ssh,$filename) = @_; $ssh->exec("cmd 1"); $ssh->exec("cmd 2"); open my $p, '<', $filename; while (my $config = <$p>) { chomp $config; my $conf = $ssh->exec("$config"); print("$conf"); } } #--> Function call. mysub($ssh,"file name.txt");
my $p = mysub($ssh," file name.txt "); if ($p) { print {*STDERR} "Something went wrong [$p]." or carp 'Could not print error about the config file'; return 0; }
However this is not working. Any suggestions ?
In reply to Perl function calls. by user786
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |