Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Passing an array into an open command

by mikeysmailbox (Initiate)
on Mar 18, 2004 at 18:01 UTC ( #337747=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have the below code. The array @ABC is basicly 50 lines of log information. I need to pass this into a open statement and have all 50 lines pass into the ticket program. When the code is executed as shown below, all that is outputted is "49". How do I get the real log information.
open(RUN, "/usr/local/bin/ticket @ABC|"); while($line = <RUN>) { print "$line"; } close(RUN);
Thanks, Mike

Replies are listed 'Best First'.
Re: Passing an array into an open command
by tcf22 (Priest) on Mar 18, 2004 at 18:12 UTC
    It looks like it should work. I tried the following on Win32 Active Perl 5.8.
    my @a; foreach(1..50){ $a[$_-1] = $_; } open(T, " @a|"); while(<T>){ print; } close(T);
    foreach(@ARGV){ print "$_\n"; }
    If I run, I get 1..50 printed out on seperate lines, just like you would expect.

    UPDATE: Also works on Red Hat Linux 9. (Just change " @a|" to "./ @a|" of course).

    - Tom

      with passing @a to the script would you run into limits of a commandline if @a is say 10,000 elements, or does the exec not care? (it's been a long time since I've played with this)
        Linux allows me to go up to around 23,000. Windows however crashed when i reach 530. I assume that there is some size or argument limit on most Oper. Systems.

        - Tom

Re: Passing an array into an open command
by eXile (Priest) on Mar 18, 2004 at 20:19 UTC
    Mike, is the 'ticket' program expecting input from the command-line or on STDIN? If it is expecting input from STDIN and you want to process it's STDOUT, you can use IPC::Open2: (emulates 'ticket' program')
    #!/usr/bin/perl -w use strict; while (<STDIN>) { print "PROCESSED $_"; }
    #!/usr/bin/perl -w use strict; $|++; use IPC::Open2; my @loglines=("This is not a relevant logline", "This is not a relevant logline", "This might be an interesting logine", "Another interesting logline"); open2(*READ,*WRITE,"./") or die; foreach my $inputline (@loglines) { print WRITE "$inputline\n"; } close (WRITE); my $outputline; while ($outputline = <READ>) { print $outputline; }
    As a sidestep: if you use 'open', please check if it succeeds, like:
    open(RUN, "/usr/local/bin/ticket @ABC|") or die "useful error message +here";
Re: Passing an array into an open command
by NovMonk (Chaplain) on Mar 18, 2004 at 18:13 UTC
    Shouldn't it be

    while (<RUN>){ print $line; }


    If I'm completely missing the boat, I trust one of my brother/ sister monks will be gentle with their enlightenment....

    Good luck,


      I don't think so. the while (<RUN>) assigns the line from the RUN filehandle to $_, then you print out $line (which hasn't been assigned anything in this snippet). I don't think that's what the OP wants.
        Ah. I see.

        I was trying to identify that what he wanted to do was run through his <RUN> and print something-- I just used his $line variable without thinking about what that does.

        Oh well. At least I knew his  while ($line= <RUN>... was a problem. I may get the hang of this yet. Thanks for the feedback.



Re: Passing an array into an open command
by Roy Johnson (Monsignor) on Mar 18, 2004 at 20:04 UTC
    Any chance you've got newlines in your @ABC data? Have you printed out what the command line you assemble looks like? What does the output of 49 from the ticket program indicate?

    The PerlMonk tr/// Advocate
Re: Passing an array into an open command
by ambrus (Abbot) on Mar 18, 2004 at 20:00 UTC

    You might try using the new "three-argument" form of the open func, but it will have more than three arguments here.

Re: Passing an array into an open command
by amw1 (Friar) on Mar 18, 2004 at 18:10 UTC
    Here's how I'd do it.
    #populate @ABC for pourposes of the test @ABC = qw[ticket1 ticket2 ticket3]; foreach my $ticket (@ABC) { my $line = qx{/usr/local/bin/ticket $ticket}; # print qx{.... would also accomplish this # assuming you want to do more to $line than print print $line; }
    nevermind, missed the fact that all args were needed at once. *sigh*

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://337747]
Approved by davido
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2020-07-16 17:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found