When I was initially writing, I was able to print from within each subroutine, exactly what I wanted the output to be. However, once I removed the print statements from the subroutines and created the "print" subroutine, my output is not what I intend. The code:
#!/usr/local/bin/perl -w use strict; use File::Find; die "Usage: get-router-info <yyyy/mmdd>\n" unless (@ARGV == 1); ###############VARIABLE DEFS############### my $newdate = $ARGV[0]; my $DATE = $newdate; $DATE =~ s-/--; my $versdir="/a/very/long/path/$newdate/to/version/pop"; my $diag_dir ="/a/very/long/path/$newdate/to/diagbus/pop"; my $rtr_names = "/export/home/limo/Perl/router-list.$DATE"; my $eddie_prv = "/path/to/some/other/file.$DATE"; my $eddie_pub = "/path/to/some/different/file.$DATE"; my $asdir = "/nfs/jumbo/b/Peering/AS123"; my $rtr_diag; my $ver_print_string; my %slots = (); my %version =(); my %rtrs = (); my %peers = (); my @diaglist = (); ########################PROGRAM BEGINS################ #read list of routers for collection into %rtrs &read_rtr_hash; # decide if they are peering, who they peer with. &decide_peer; #get router type and IOS version find(\&version, $versdir); #get module configuration, unless module is procesor, power supply, #clock, or switch find(\&diag, $diag_dir); &print; #########################SUBSROUTINES############################## sub read_rtr_hash { open (RTRLIST, $rtr_names) or die "Cannot open $rtr_names: $!\n"; while (<RTRLIST>) { chomp; my @rtr = split; my $init = "1"; foreach my $device (@rtr) { push @{$rtrs{$device}}, $init; } } } sub decide_peer { open (PRV, "<$eddie_prv") or die "Cannot read $eddie_prv: $!"; while (<PRV>) { chomp; my ($prv, $dev) = (split(' ', lc($_)))[0,3]; if ( exists $rtrs{$dev} ) { if ( exists $peers{$dev}) { $peers{$dev}[0].= "$prv, "; } else { push @{$peers{$dev}}, ("$prv, ", ""); } } } open (PUB, "<$eddie_pub") or die "Cannot read $eddie_pub: $!"; while (<PUB>) { chomp; my ($pub, $dev) = (split(' ', lc($_)))[0,3]; if ( exists $rtrs{$dev} ) { if ( exists $peers{$dev}) { $peers{$dev}[1].= "$pub, "; } else { push @{$peers{$dev}}, ("", "$pub, "); } } } } sub version { @ios_ver = `cat $_ |grep IOS` unless ( $_ =~ m/^\./); my $vers = $_; $vers =~ s/\.bbnplanet\.net//; if (exists $rtrs{$vers}) { foreach my $rtr_name(@ios_ver) { my @tmp_array = split(' ',$rtr_name); if ($rtr_name =~ m/(Version.*?),/) { push @{$version{$vers}}, $rtr_name; $ver_print_string = ("$i[2] $1"); } } } } sub diag { @diaglist = `cat $_ |grep SLOT` unless ($_ =~ m/^\./); my $router = $_; $_ =~ s/\.bbnplanet\.net//; if (exists $rtrs{$router}) { foreach $rtr_diag(@diaglist) { next if ($rtr_diag =~ m/Clock|Switch|Power|Processor/); push @{$slots{$router}}, $rtr_diag; } } } sub print { foreach my $vers_key (sort keys %version) { foreach my $mod_key (sort keys %slots) { my $module = join "", @{$slots{$mod_key}}; foreach my $peer_dev (sort keys %peer) { my ($prv, $pub) = @{$peer{$peer_dev}}; print "$peer_dev:\n"; print "================\n"; print "belongs to AS123\n"; print "private peers: $prv\n"; print "public peers: $pub\n\n"; print "IOS Version: $vers_key\n"; print "$module\n"; } } } } ######################END#######################
The output should be data, retrieved from various files, all data corresponding to each device contained in a pre-specified list of devices. What happens, is that, $peer prints correctly, but the rest of the variables are incorrect, meaning that while they get populated with data, it doesn't correspond to $peer. It corresponds with a different device. Sort of "mismatched". Example:
Router A: =========== belongs to AS123 private peers: 456 #oops, this is data from router b print "public peers: 789 #oops, this is data from router c print "IOS Version: #etc...etc..etc
Can anyone spot anything obviously wrong with my "print" subroutine? Hopefully, this is clear. If more info is needed, please ask.

In reply to Print Subroutine Problem by Limo

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.