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

Need help to avoid using a GLOBAL variable.

Expand Messages
  • Bobby Jafari
    Hi List, I have a problem with my subroutine. The cutdown version is: sub LoginViaCli { my ($userId, $password, $terminalIp, $termPort) = @_; $telnet = new
    Message 1 of 5 , Jan 4, 2006
      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)

      All comments are welcome.

      Thanks for taking the time to read this.

      Bobby Jafari
      Test Engineer
      Senetas Security Pty Ltd
      Office: +61 3 9868 4529
      Mobile: +61 404 089 021
      E-Mail: bobby.jafari@...
    • Charles K. Clarkson
      ... I m not sure I understand. Are you saying you cannot use $telnet *outside* the subroutine? Or are you saying this fails *inside* the subroutine when
      Message 2 of 5 , Jan 4, 2006
        Bobby Jafari <> wrote:

        : 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)

        I'm not sure I understand. Are you saying you cannot use $telnet
        *outside* the subroutine? Or are you saying this fails *inside* the
        subroutine when $telnet is a lexical variable?


        Charles K. Clarkson
        --
        Mobile Homes Specialist
        254 968-8328
      • Ken Shail
        ... . What is the best way to do this (Other than using: our $telnet) return a reference to $telnet
        Message 3 of 5 , Jan 5, 2006
          >----- 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 4 of 5 , Jan 5, 2006
            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 5 of 5 , Jan 5, 2006
              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.