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

I created the perl script below It consist of multiple subroutines collecting seperate information. I am having a problem managing the array output scalar, I would like to see a different line input for each command and each host. This is the part as a beginner I having problem with. The hosts.txt file havng (server1, server2, server30 and the commands are one, two, three, four. Please help.
#!/usr/bin/perl use 5.010; use strict; use warnings; main(@ARGV); sub get_host # Collect the host name from a text file { open(HOST, '<', 'hosts.txt') or error("cannot open file ($!)"); my @host = <HOST>; return "@host\n"; } sub get_logon # Collect logon ID from keyboard { my $logon; print "Please Enter Logon ID:\n"; chomp ($logon =<>); return $logon; } sub get_psswd # Collect password from keyboard { my $psswd; print "Please Enter Password for hostID:\n"; chomp ($psswd =<>); return $psswd; } sub get_command # Collect the commands from a text file { open(CMD, '<', 'cmds.txt') or error("cannot open file ($!)"); my @cmd = <CMD>; return @cmd; } sub main # Command execution and export data to local file { my @host = get_host(); print @host; print "\n"; my $logon = get_logon(); print $logon; print "\n"; my $psswd = get_psswd(); print $psswd; print "\n"; my @cmds = get_command(); print @cmds; my $output = qx(cmd); open my $OUTPUT, '>>' , 'devbfs51chassisshow.txt' or die " Couldn't op +en output.txt: $!\n"; print "1plink -ssh -pw $psswd -l $logon @host @cmds"; }

Replies are listed 'Best First'.
Re: Perl Array output Help
by Laurent_R (Canon) on Jun 01, 2015 at 19:49 UTC
    Trying to make some sense of your garbled code:
    #!/usr/bin/perl use 5.010; use strict; use warnings; main(@ARGV); sub get_host # Collect the host name from a text file { open(HOST, '<', 'hosts.txt') or error("cannot open file ($!)"); my @host = <HOST>; return "@host\n"; } sub get_logon # Collect logon ID from keyboard { my $logon; print "Please Enter Logon ID:\n"; chomp ($logon =<>); return $logon; } sub get_psswd # Collect password from keyboard { my $psswd; print "Please Enter Password for hostID:\n"; chomp ($psswd =<>); return $psswd; } sub get_command # Collect the commands from a text file { open(CMD, '<', 'cmds.txt') or error("cannot open file ($!)"); my @cmd = <CMD>; return @cmd; } # etc.
    Isn't it somewhat clearer than the mess that you posted??
      Thanks
Re: Perl Array output Help
by FreeBeerReekingMonk (Deacon) on Jun 01, 2015 at 21:43 UTC

    Guesstimating what you are asking, you need to loop over each $host

    # Command execution and export data to local file sub main { my @hosts = get_host(); print @hosts; print "\n"; for my $host (@hosts){ print "For host $host...\n"; my $logon = get_logon(); print $logon . "\n"; my $psswd = get_psswd(); print $psswd . "\n"; my @cmds = get_command(); print @cmds; my $output = qx(cmd); open OUTPUT, '>>' , 'devbfs51chassisshow.txt' or die " Couldn't open output.txt: $!\n"; print $OUTPUT "1plink -ssh -pw $psswd -l $logon $host @cmds"; } close OUTPUT; }

    See also: looping through array

      Thank you for the help, I am just trying to learn. I will look into looping through array.
Re: Perl Array output Help
by james28909 (Deacon) on Jun 01, 2015 at 19:11 UTC
    Please use code tags around your code or suffer the consequences MUHAHAHAHAHAAAA!!!!!
    like this:
    <code> use strict; use warnings; open my $file, '>', 'file; binmode($file); #more code here #then finally: </code>
    Wrap your code in code tags like that and it will help us help you better :)
        Thank you :)
      thank yo u:) I will look into Code Tags