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

Hello Monks,

Yesterday, I was so glad to get the first part of a script up and running with your help, that i just left home without testing to see if the program would work if made into a loop.

#!usr/bin/perl -w use strict; my @cmd=qw(lpr -S SYSTEMX -P QGPL/SPYVIEW -o l C:\\ABCDEFGH.txt); system(@cmd)==0 or die "cannot do this";
This now works fine.But if i put it into a loop,to do the same action with all the files in a directory, the script exits saying it cannot open $file for reading.Any idea why this is happening? I must have made some stupid mistake that eludes me.Could anyone help?
#!usr/bin/perl -w use strict; my @files=<c:\\ascii\\*>; foreach my $file(@files){ my @cmd=qw(lpr -S SYSTEMX -P QGPL/SPYVIEW -o l $file); system(@cmd)==0 or die "cannot do this"; }

Is there a problem with the system command that it won't run in a loop or is there anything else I have made a mistake with?

Thanks , -Sandhya

Replies are listed 'Best First'.
Re: system commands aren't working as a loop.
by ikegami (Patriarch) on Feb 12, 2009 at 17:20 UTC
    qw(lpr -S SYSTEMX -P QGPL/SPYVIEW -o l $file)

    means

    (split(' ', 'lpr -S SYSTEMX -P QGPL/SPYVIEW -o l $file'))

    Do you see the problem? If not, print out the contents of @cmd. Solution:

    my @options = ( -S => 'SYSTEMX', -P => 'QGPL/SPYVIEW', -o => 'l', ); # Avoid being confused for an option. $file =~ s{^(?=-)}{./}; system(lpr => @options, $file)
Re: system commands aren't working as a loop.
by Corion (Patriarch) on Feb 12, 2009 at 17:18 UTC

    Maybe read up on what perlop says about qw, or just print out @cmd before running it.

    Also try:

    perl -le "print qq([$_]\n) for qw($foo $bar $baz);"

    Alternatively, try taking away the loop and just using the first entry from @files.

Re: system commands aren't working as a loop.
by mr_mischief (Monsignor) on Feb 12, 2009 at 17:30 UTC
    This doesn't have to do with your immediate problem, but one thing that might make you life easier when dealing with Perl on Windows is to remember that unlike cmd.exe, the Windows system internally can use forward slashes for paths. So where you have "c:\\ascii\\" it could actually be "c:/ascii/" instead.
      i tried that. the prob is the as400 receives the file as /test1 which i was trying to avoid
Re: system commands aren't working as a loop.
by smanicka (Scribe) on Feb 12, 2009 at 17:27 UTC
    Thanks to both of you.When i tried to print out @cmd, It was printing them out, but I can't understand why, was not substituting the actual values of $file.Since I din't have an actual file called $file, it was trowing me an error.Once I changed it as @ options etc, it succeeds, atleast from my side.I just need to go on to the as400 to see if it has recieved the files

      Once I changed it as @ options etc,

      The key difference is moving $file outside of qw() because qw() doesn't interpolate. Creating an array of options instead of one containing the whole command is just aesthetics. It all results in the same list of arguments.