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

Hi all, I am a Perl noob. I will admit that here right away. Any pointers here would be helpful. I am trying to build a little menu for Ethernet switch configurations.

#!/usr/bin/perl -w # use warnings; use diagnostics; use warnings; use strict; use Term::ANSIScreen qw(cls); our @switches; our $clear_screen = cls(); # Subroutine vars our $slotcount = 1; ###################### START of MENU 1 / MODEL SELECT ############### +#### while (my $modelselect ne '10') { print $clear_screen; print "########################################################### +##########\n"; print "## Configuration Script for Catalyst Access Switches IOS + 15.x ##\n"; print "########################################################### +##########\n"; print "## + ##\n"; print "##  Select Model of Switch: + ##\n"; print "## (1) - Cat6509 (2) - Cat6506 (3) - Cat6513 + ##\n"; print "## (2) - Cat4507 (5) - Cat4506 (6) - Cat4510 + ##\n"; print "## (7) - WS-C3850-24 (8) - Cat3850-48 (9) - Cat3850 +Stack ##\n"; print "## (10) - Exit + ##\n"; print "## + ##\n"; print "########################################################### +##########\n"; print "Please enter your selection:\n"; $modelselect = <STDIN>; chomp $modelselect; if ($modelselect eq 1) { print "You have selected $modelselect\n"; my $slotlimit = 9; print "You have selected a 'WS-C6509'.\n"; print "$slotlimit lincecard slots have to be configured:\n +"; while ($slotcount <= $slotlimit) { print "Calling Subroutine 'Slotmenu' with value $s +lotcount\n"; &slotmenu($slotcount); push (@switches, my $portcount); $slotcount++; } } if ($modelselect eq 2) { print "You have selected $modelselect\n"; my $slotlimit = 6; print "You have selected a 'WS-C6506'.\n"; print "$slotlimit lincecard slots have to be configured:\n +"; while ($slotcount <= $slotlimit) { print "Calling Subroutine 'Slotmenu' with value $s +lotcount\n"; &slotmenu($slotcount); push (@switches, my $portcount); $slotcount++; } } print "@switches\n"; sub slotmenu { print $clear_screen; print "Value actually passed to Subroutine 'Slotmenu': $slotcount\ +n"; print "########################################################### +##########\n"; print "## Configuration Script for Catalyst Access Switches IOS + 15.x ##\n"; print "########################################################### +##########\n"; print "## + ##\n"; print "## Configure Slot $slotcount: + ##\n"; print "## Select port count, speed, or other layout + ##\n"; print "## + ##\n"; print "## 1000 Mbit/s = (A) 10000 Mbit/s = (B) + ##\n"; print "## 24 X Blade = (1) 48 x Blade = (2) + ##\n"; print "## Supervisor = (S) + ##\n"; print "## No linecard = (0) + ##\n"; print "## + ##\n"; print "## 'Enter as: A2 for 48 x 1000 Mbit lincecard + ##\n"; print "## + ##\n"; print "########################################################### +##########\n"; print "Please enter number ports on linecard:\n"; my $portcount = <STDIN>; chomp $portcount; #return $portcount; push (@switches, $portcount) } }

I keep getting this strange output when i run this script:

Use of uninitialized value $switches[1] in join or string at morecurre +nt.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[3] in join or string at morecurre +nt.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[5] in join or string at morecurre +nt.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[7] in join or string at morecurre +nt.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[9] in join or string at morecurre +nt.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[11] in join or string at morecurr +ent.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[13] in join or string at morecurr +ent.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[15] in join or string at morecurr +ent.pl line 69, <STDIN> line 11 (#1) Use of uninitialized value $switches[17] in join or string at morecurr +ent.pl line 69, <STDIN> line 11 (#1) A1 B1 B12 S S S S S S Use of uninitialized value $modelselect in string ne at morecurrent.pl + line 19, <STDIN> line 11 (#1)
Thank you in advance. - Georg

Replies are listed 'Best First'.
Re: Strange undefined scalars in arry
by BrowserUk (Patriarch) on Jan 13, 2014 at 14:17 UTC

    Every time this line

    while (my $modelselect ne '10')
    executes, you are creating a brand new variable called $modelselect. It will always be uninitialised; and thus will never be eq to '10';

    Try replacing that line with these two and you might have more success:

    my $modelselect; while ( $modelselect ne '10')

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Strange undefined scalars in array
by Anonymous Monk on Jan 13, 2014 at 14:46 UTC
    push (@switches, my $portcount);
    is (mostly) equal to
    push (@switches, undef);
    And that's likely where your undefs come from.
Re: Strange undefined scalars in array
by Preceptor (Deacon) on Jan 13, 2014 at 21:56 UTC

    I think the core problem here, is you misunderstand what 'my' and 'our' does.

    'my' creates a new variable, valid within the current scope, and as a new variable - it's undefined.

    'our' is used to import variables from other packages. You probably don't want to use it.

    Perldoc on 'our'

    Perldoc on 'my'

    Normally, 'my' is what you'll want to use, but when using it in a loop, if it's _inside_ the loop, you'll get a new variable each iteration.

    So:

    while (my $modelselect ne '10')

    Is creating a new '$modelselect' each time you run that loop, which is almost certainly not what you want to do, because that way your loop says:

    while (undef ne '10')

    Likewise:

    push (@switches, my $portcount);

    Is doing the same thing. $portcount is being defined right there (and if you'd already declared it, would spit out an error) - you do actually have two separate 'my $portcounts' but they're in different subroutines, which means they're completely separate instances of the same variable.

Re: Strange undefined scalars in array
by GeorgMN (Acolyte) on Jan 13, 2014 at 15:48 UTC

    Hi again monks, thank you for your help. That works now. Peace be with you. :)