Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Need help to avoid using a GLOBAL variable.

Expand Messages
  • Ken Shail
    ... . What is the best way to do this (Other than using: our $telnet) return a reference to $telnet
    Message 1 of 5 , Jan 5, 2006
    • 0 Attachment
      >----- Original Message -----
      >From: Bobby Jafari ; Bobby Jafari
      >To: perl-beginner@yahoogroups.com
      >Sent: Thursday, January 05, 2006 1:01 AM
      >Subject: [PBML] Need help to avoid using a GLOBAL variable.


      >Hi List,
      >
      >I have a problem with my subroutine. The cutdown version is:

      >sub LoginViaCli {
      > my ($userId, $password, $terminalIp, $termPort) = @_;
      > $telnet = new Net::Telnet ( Timeout => 10,
      > Host => $terminalIp,
      > Port => $termPort,
      > Errmode => 'return');
      > $result = $telnet->waitfor('/LOGIN:/i');
      > $result = $telnet->put ("$userId\n");
      > $result = $telnet->waitfor('/PASSWORD:/i');
      > $result = $telnet->put("$password\n");
      > return 'PASSED';
      >}

      >When I use sub LoginViaCli, after the sub has logged in, the only way I
      >have been able to talk to the telnet object is to make $telnet GLOBAL.
      >(ie, our $telnet)
      >
      .>What is the best way to do this (Other than using: our $telnet)

      return a reference to $telnet
    • Shawn Corey
      ... Why PASSED ? Your subroutine always returns it; it does indicate if it failed. Try: return $telnet; # Success Or: return undef; # Failure -- Just my
      Message 2 of 5 , Jan 5, 2006
      • 0 Attachment
        Bobby Jafari wrote:
        > Hi List,
        >
        > I have a problem with my subroutine. The cutdown version is:
        >
        > sub LoginViaCli {
        > my ($userId, $password, $terminalIp, $termPort) = @_;
        > $telnet = new Net::Telnet ( Timeout => 10,
        > Host => $terminalIp,
        > Port => $termPort,
        > Errmode => 'return');
        > $result = $telnet->waitfor('/LOGIN:/i');
        > $result = $telnet->put ("$userId\n");
        > $result = $telnet->waitfor('/PASSWORD:/i');
        > $result = $telnet->put("$password\n");
        > return 'PASSED';
        > }

        Why 'PASSED'? Your subroutine always returns it; it does indicate if it
        failed.

        Try:
        return \$telnet; # Success
        Or:
        return undef; # Failure


        --

        Just my 0.00000002 million dollars worth,
        --- Shawn

        "Probability is now one. Any problems that are left are your own."
        SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

        * Perl tutorials at http://perlmonks.org/?node=Tutorials
        * A searchable perldoc is available at http://perldoc.perl.org/
      • Bobby Jafari
        Hi All, Thanks for your inputs. To answer Charles K. Clarkson question, I was trying to access $telnet from outside the subroutine. I am now using references
        Message 3 of 5 , Jan 5, 2006
        • 0 Attachment
          Hi All,

          Thanks for your inputs. To answer Charles K. Clarkson question, I was
          trying to access $telnet from outside the subroutine. I am now using
          references to $telnet and dereferencing it when I need to access the
          object.

          Shawn Corey recommended the following and I have modified my sub as per
          his advice

          >Try:
          > return \$telnet; # Success
          >Or:
          > return undef; # Failure

          Thanks for all the feed back.
          Regards,
          Bobby
        Your message has been successfully submitted and would be delivered to recipients shortly.