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

Dear monks, I am looking for enlightenment in this situation

I would like to pass the session reference as an function parameter "like code below", but in this situation I am unable to make it right :( And I will love your advices, thanks4all

use Net::Telnet::Cisco; sub doOne{ my $sess = shift; my $cmd = "sh ver"; my @lines = $sess->cmd("$cmd"); foreach my $line (@lines)... # do the parsing return... } my $session = Net::Telnet::Cisco->new( Host => $host, Input_log => $logfile, Timeout => 15, Errmode => "return"); if($session){ $session->login(Name => "$user", Password => "$pass", Timeout => 15) or warn "> \[Login Error\]: $!"; my @arr1 = doOne($session); # session is a hash reference? my @arr1 = doTwo($session); # tried the backslash in front of it $session->close(); }

Replies are listed 'Best First'.
Re: session as function argument in net:telnet:cisco
by Corion (Patriarch) on Nov 16, 2013 at 16:51 UTC
    my @lines = $$sess->->

    This code simply makes no sense at all.

    Your approach of passing the session is the normal way. There is no need to do any special things on the receiving side. You just use $sess in your subroutine as you would use $session in you main program.

      sorry wrong paste! already corrected. I am missing something in the session reference passing.. thanks anyway,
      Too many arguments for main::doOne at ./script.pl line 16, near "$sess +ion)" Execution of ./script.pl aborted due to compilation errors.
        Too many arguments for main::doOne ...
        sub doOne(){ ... }

        You define the  doOne() subroutine as taking no arguments (using the  () prototype; see Prototypes in perlsub), and then you try to pass it an argument when you invoke it. That won't work. In general, don't use prototypes unless you know what they are, what they do, and why you're using one in a particular instance. See Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen.

        Update: mvcorrea: I see that the code in the OP has been 'fixed' again to remove the prototype from the  doOne() function definition (it was altered once before in response to Corion's reply), and again there is no notation that a critical alteration has been made. Please don't do this: it makes comments based on the original post seem incoherent. By all means, make changes to your posts, but please make a note of the changes within the changed post! (If you post as a registered user, you can always go back and edit your post.)