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.

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

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.