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

I've got a rookie question. I've been playing with it for a while today and can't get it to work. I'm trying to put together a script that matches a regex against a flat file that contains all our switches' names.

In the foreach loop, it prints off the expected results (the names that match $hostregex) and until I added the

my @version = $session->cmd('show version  ');

line, it appeared to go to the switch in question.

When I added

 my @version = $session->cmd('show version  ');

I get the following error:

myname@bast1:~/bin$ testloop 101a Global symbol "$session" requires explicit package name at /home/mynam +e/bin/testloop line 31. Execution of /home/myname/bin/testloop aborted due to compilation erro +rs. myname@bast1:~/bin$

Here's the script. Any help would really be appreciated.

#!/usr/bin/perl use Net::Telnet::Cisco ; use strict; #always and forever, amen. use warnings; #or else. use lib '/home/myname/bin/lib'; use subs ; if ($#ARGV != 0) { print "usage: enter the switchname \n"; exit; } my $hostregex = $ARGV[0] ; my $infile = "/home/myname/zen/zen_devices_out" ; # print "You're using: '$infile'\n\n" ; open INF , "$infile" or die $! ; my @lines = <INF> ; my @matches = (grep(/$hostregex/i,@lines)) ; foreach (@matches) { chomp($_) ; print "'$_'\n" ; my ( $p1 , $p2 ) = get_p() ; my $session = Net::Telnet::Cisco->new(Host => $_); $session->login('myname', $p1); if ($session->enable($p2) ) { } else { warn "Can't enable: + " . $session->errmsg; } } my @version = $session->cmd('show version '); print @version ; exit ;

Replies are listed 'Best First'.
Re: Global symbol "$session" requires explicit package name
by jakobi (Pilgrim) on Oct 16, 2009 at 22:10 UTC
    Answer: There is no $session in scope at your @version line. Hint: Fix your indenting and formatting of parens, esp. for the foreach block, then it's very obvious.

    cu
    Peter

Re: Global symbol "$session" requires explicit package name
by muba (Priest) on Oct 16, 2009 at 22:41 UTC

    In addition to what jakobi said above, you might also want to check the return value of Net::Telnet::Cisco->new(). Maybe it wasn't able to establish a connection!

    Also, if ( EXPRESSION ) { } else { ... } doesn't earn a beauty award either. I'd suggest you rewrite the whole foreach block like this:

    foreach (@matches) { chomp; print "'$_'\n"; my ($p1, $p2) = get_p(); if (my $session = Net::Telnet::Cisco->new(Host => $_) ) { $session->login('myname', $p1); warn "Can't enable: " . $session->errmsg unless $session->enable($p2); my @version = $session->cmd('show version '); print @version, "\n"; } else { die "Couldn't connect to $_"; } } exit;

    However, I'm having this hunch that the version thingy is going to be the same for each iteration, so you might also want to consider something like this:

    my @version; foreach (@matches) { .... @version ||= $session->cmd('show version '); } print @version, "\n";
      Hmmm ,

      ...check the return value of Net::Telnet::Cisco->new() - whilst checking such return values (or utilising autodie) is, IMO, never a bad idea, this call _must_ have succeeded since Net::Telnet tells us that the default action on error (not overridden by your code) is to die.

      Hence, since no such behaviour was forthcoming, we can infer that the call to the constructor must have been successful and thus a session opened.

      A user level that continues to overstate my experience :-))

        You're right: the call must have succeeded, then. And IMO you're right again: checking return values (or using autodie indeed) is never a bad idea.

        I'll be upfront with you. I was merely trying to advocate good practice. Thank you for pointing out I failed to live up to a very famous one: RTFM. I didn't care to check the docs so perhaps a mild apology is in order.

Re: Global symbol "$session" requires explicit package name
by WearyTraveler (Novice) on Oct 18, 2009 at 03:46 UTC

    Thank you for all the suggestions.

    I'll give them a try and see how it goes!

    Thank you!