I'm cleaning up an old script with newfound knowledge.   Part of this cleanup is to move user-modified parameters to an external file.

&CONFEXT is supposed to populate $timeout, @targets, and @command from the file ciscoconf.conf.   As the Dumps show, CONFINT isn't fetching all the info into the appropriate arrays.   I think my "push" syntax is good, and suspect my control structure is *not*.   Would any wise monk(s) care to show me the error of my ways?

Hopefully relevant sections of code follow:
    tired groggy and quite loopy,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

###################################################################### +### # Begin small slice of ciscoconf.pl use Data::Dumper; #only for testing use vars qw( $section $timeout @timeout $target @targets $command @commands ); my $conf = 'ciscoconf.conf'; # list of target devices and +commands my $tmp = 'ciscoconf.tmp'; my $log = 'ciscoconf.log'; #&CONFINT(); &CONFEXT(); ---------------------------------------------------------------------- +--- sub CONFINT { $timeout = '15'; @targets = ('192.168.1.27','172.16.0.138' ); @commands = ( 'sho clo', 'set leng 0', 'term leng 0', 'sho clo', 'disa', ); open (DUMP, "> ciscoconfint.dump") or die "Error opening ciscocon +fint.dump for write: $!"; print DUMP Dumper(@timeout), "\n"; print DUMP Dumper(@targets), "\n"; print DUMP Dumper(@commands), "\n"; close (DUMP) or die "Error closing ciscoconfint.dump after write: + $!"; } ---------------------------------------------------------------------- +--- sub CONFEXT { open (CONF, "<$conf") or die "Error opening $conf for read: $!"; while (<CONF>) { next if /^#/ or /^s.*$/ or /^!/; # ignore comment lines chomp; if (/^\[([^\]]+)\]/) { $section = $1; next; } else { if ($section =~ /timeout/i) { push @timeout,$_; } if ($section =~ /targets/i) { push @targets,$_; } if ($section =~ /commands/i) { push @commands,$_; } } } close (CONF) or die " ciscoconf.pl: Error closing $conf after re +ad: $!"; open (DUMP, "> ciscoconfext.dump") or die "Error opening ciscocon +fext.dump for write: $!"; print DUMP Dumper(@timeout), "\n"; print DUMP Dumper(@targets), "\n"; print DUMP Dumper(@commands), "\n"; close (DUMP) or die "Error closing ciscoconfext.dump after write: + $!"; } # End small slice of ciscoconf.pl ###################################################################### +### # begin ciscoconf.conf [Timeout] 15 [Targets] 192.168.1.27 172.16.0.138 [Commands] sho clo set leng 0 term leng 0 disa sho clo # end ciscoconf.conf ###################################################################### +### # begin ciscoconfext.dump $VAR1 = 15; $VAR2 = ''; $VAR1 = '192.168.1.27'; $VAR2 = '172.16.0.138'; $VAR3 = ''; $VAR1 = 'term leng 0'; $VAR2 = 'disa'; # end ciscoconfext.dump ###################################################################### +### # begin ciscoconfint.dump $VAR1 = 15; $VAR1 = '192.168.1.27'; $VAR2 = '172.16.0.138'; $VAR1 = 'sho clo'; $VAR2 = 'set leng 0'; $VAR3 = 'term leng 0'; $VAR4 = 'sho clo'; $VAR5 = 'disa'; # end ciscoconfint.dump ###################################################################### +###

In reply to Parsing external config file by ybiC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.