trying to get line-by-line output of a command pipe in perl

Hello;

  I'm trying to obtain line-by-line output from a command pipe in perl.

  Unfortunately, I am firmly held to 5.8.8 version of perl on this
specific machine :-(

  Apparently, creating an array for my command prevents me from
including the final pipe symbol when trying to use the three-argument
form of open when using an array for my command rather than a scalar.

code snippet #1:
my @cmd = ("make", "target_name", "2>&1");
open my $fh, @cmd, "|" or die "blah: $!\n"; fails, "Unknown open() mode '5'"

code snippet #2:
my @cmd = ("make", "target_name", "2>&1", "|");
open my $fh, @cmd or die "blah: $!\n"; fails, "Unknown open() mode '5'"

Is it true that for me to use command pipes for line-by-line output
that I have to abandon the list form of the open or system command and
use the scalar form?

Bottom line: I'd like to have a non-scalar form of the open and
line-by-line handling of the output of the command pipe if that is
possible.

Thanks,
Ken Wolcott
0
kennethwolcott
12/23/2013 11:02:13 PM
perl.beginners 29388 articles. 4 followers. Follow

3 Replies
809 Views

Similar Articles

[PageSpeed] 33
Get it on Google Play
Get it on Apple App Store

On Mon, 23 Dec 2013 15:02:13 -0800
Kenneth Wolcott <kennethwolcott@gmail.com> wrote:

> open my $fh, @cmd, "|" or die "blah: $!\n"; fails, "Unknown open()
> mode '5'"

If you use an array, it is _not_ sent thru the shell. Try the
three-argument open:

    open my $fh, '-|', @cmd or die "blah: $!\n"; fails, "Unknown open()
    mode '5'";


-- 
Don't stop where the ink does.
	Shawn
0
shawnhcorey
12/23/2013 11:48:24 PM
Hi all,

On Mon, 23 Dec 2013 18:48:24 -0500
Shawn H Corey <shawnhcorey@gmail.com> wrote:

> On Mon, 23 Dec 2013 15:02:13 -0800
> Kenneth Wolcott <kennethwolcott@gmail.com> wrote:
>=20
> > open my $fh, @cmd, "|" or die "blah: $!\n"; fails, "Unknown open()
> > mode '5'"
>=20
> If you use an array, it is _not_ sent thru the shell. Try the
> three-argument open:
>=20
>     open my $fh, '-|', @cmd or die "blah: $!\n"; fails, "Unknown open()
>     mode '5'";

That won't get sent through the shell either:

[SHELL]

shlomif[homepage@default]:$trunk$ perl -E 'open my $fh, "-|", "make", "2>&1=
" or
die "Foo $!"'
make: *** No rule to make target '2>&1'.  Stop.
shlomif[homepage@default]:$trunk$=20

[/SHELL]

One needs to use a single command with a trailing "|" and making a wise
use of something like https://metacpan.org/release/String-ShellQuote (which=
 I'm
not sure if it supports MS Windows properly, but should work on UNIXes):

shlomif[homepage@default]:$trunk$ perl -E 'open my $fh, "make 2>&1|" or die
"Foo $!"; while(<$fh>) {print} close($fh);'

You may also wish to look at https://metacpan.org/pod/IPC::Run .

Regards,

	Shlomi Fish

--=20
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Text Editors and IDEs - http://shlom.in/IDEs

Vizzini: He didn=E2=80=99t fall?! Inconceivable!
Inigo Montoya: You keep using that word. I do not think it means what you
think it means.
    =E2=80=94 http://en.wikiquote.org/wiki/The_Princess_Bride_%28film%29

Please reply to list if it's a mailing list post - http://shlom.in/reply .
0
shlomif
12/24/2013 7:01:21 AM
On Thu, Dec 26, 2013 at 5:34 PM, John W. Krahn <jwkrahn@shaw.ca> wrote:
> Kenneth Wolcott wrote:
>>
>> Hello;
>
>
> Hello Kenneth,
>
>
>
>>    I'm trying to obtain line-by-line output from a command pipe in perl.
>>
>>    Unfortunately, I am firmly held to 5.8.8 version of perl on this
>> specific machine :-(
>>
>>    Apparently, creating an array for my command prevents me from
>> including the final pipe symbol when trying to use the three-argument
>> form of open when using an array for my command rather than a scalar.
>>
>> code snippet #1:
>> my @cmd = ("make", "target_name", "2>&1");
>> open my $fh, @cmd, "|" or die "blah: $!\n"; fails, "Unknown open() mode
>> '5'"
>>
>> code snippet #2:
>> my @cmd = ("make", "target_name", "2>&1", "|");
>> open my $fh, @cmd or die "blah: $!\n"; fails, "Unknown open() mode '5'"
>
>
> Those won't work because the second argument for open() has to be the mode
> and it has to be a scalar value:
>
> $ perl -le'print prototype "CORE::open"'
> *;$@
>
> So the first example is:
>
> open my $fh, 3, "|" or ...;
>
> And the second example is:
>
> open my $fh, 4 or ...;
>
> Because an array in scalar context is the number of elements in the array.
>
> The correct syntax is:
>
> open my $fh, '-|', @cmd, or ...;
>
> But using a list instead of a string means that "2>&1" will be treated as a
> text string and will not effect the STDERR stream.
>
> For STDERR redirection you need to run it through the shell by using a
> string:
>
> open my $fh, 'make target_name 2>&1 |' or ...;
>
>
>
>
> John
> --
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction.                   -- Albert Einstein

Hi John, Shawn and Shlomi;

  Thank you for your help.

  I am successfully running now!

Thanks,
Ken Wolcott
0
kennethwolcott
12/27/2013 1:41:28 AM
Reply:

Similar Artilces:

Line-To-Line
I have one string like this: Hello, I am a shopkeeper (linefeed) Thus, I like to say... (linefeed) Good Morning (linefeed) How can I put this string line-to-line recording it into a file? How about something like this.... long ln_pos,ln_filenum,ln_start_point string tmp_part ln_start_point = 1 //fileopen ln_filenum = FileOpen("c:\export.txt",LineMode!,Write!,LockWrite!,Replace!) ln_pos = Pos(ls_your_string,'~n',1) //~r~n if CR LF for ln_pos > 0 tmp_part = Mid(ls_your_string,ln_start_point,ln_pos) FileWrite(filenum,tmp_part) ln_start_...

Lines, what lines?
Given all the talk about high voter turnout, I got to the polls about 15 minutes after they opened at 7:15am. This was about the same time I went in 2006. The line today was about the same as then. It took about 30 minutes to vote for McCain and go. By the time I left, the line was about 30% shorter. Weather was good and parking was adequate. Are the reports of long waits just media hype? Don Abel wrote: > Are the reports of long waits just media hype? Maybe you ought to read Kirt's post in the Mark 8:10 blabla thread. Don Abel wrote: > Are the reports of long waits...

Line by line
Hi, I have a file extremely large in size and length. I want to read the file line by line and worry about matching up each line and running that line through a subroutine instead of opening the entire file (which would take a while). So how do I open a file up and process each line individually? On Dec 17, 2003, at 7:28 PM, PerlDiscuss - Perl Newsgroups and mailing lists wrote: > Hi, I have a file extremely large in size and length. I want to read > the > file line by line and worry about matching up each line and running > that > line through a subroutine i...

RE: How to get 1st line, last line and no of lines in a file
> -----Original Message----- > From: Madhu Reddy [mailto:saitech74@yahoo.com] > Sent: Wednesday, February 19, 2003 1:25 PM > To: beginners@perl.org > Subject: How to get 1st line, last line and no of lines in a file > > > Hi, > How to get first line, last line and no of lines in > a file..... > > is there any perl functions available for that ? > right now what i am doing is > > open file > while (<FH> > { > $lines++; > } > close(FH) > > This operation is expensive.. > suppose, if file...

launch a command line program and get its console output
Hi, I am creating a front end for a console aplication (ffmpeg if I can get it working). I know that using @mozilla.org/process/util;1 it is possible to run a proccess with some parameters. But, is there a way to get the console output of that proccess? To perform some operation, like inform the user if all was ok and things like that. Really thanks for any help to point me to the solution. Mariano. Mariano Cuenze wrote: > Hi, I am creating a front end for a console aplication (ffmpeg if I can > get it working). I know that using @mozilla.org/process/util;1 it is > possibl...

TFS Command Line Get Command
Hi Can you pls help me how to pull the latest version or label from the TFS through Command Line. I went through the msdn documentation. I am not able to get a clear picture. I am expecting some command with example where I can pass the server name, Credentials and sourcepath and Destination path. Basically I want to pull the get latest to a local path which  will be specified in the command line utility. Hope so I am clear.  Pls help me.RegardsFinny  You can use tf command line utilityThis utility can be found in C:/ProgramFiles/Microsoft Visual Studio (9 or 8...

How to capture the output of a command line app using anonymous pipes?
I have spawned a child process using the CreateProcess API. This part works ok. I have research online and I found some examples about how to capture the output of the child process started with CreateProcess in Real Time using pipes but these examples are for MFC and Delphi. A good Delphi example found it here : http://www.sonsivri.com/forum/index.php?topic=21472.0 // Example Delphi code procedure TForm1.RunDOSinMemo(parameter:string); const ReadBuffer = 32768; // 32kb buffer var Security : TSecurityAttributes; ReadPipe,WritePipe : THandle; start : TStar...

How to pass command line arguments to an extension using a command line on MacOS
I am developing an extension that processes some command line options. I want to invoke the Firefox from a command line like this: > ...../firefox -myarg1 -myarg2 I am running into a problem on Mac OS. When I run such command when there is no FireFox running it works fine. However if I run the command again I get an error saying that there is already an instance of Firefox running and only one instance can run at a time (paraphrasing here). I have tried to use the -remote option but no luck. Does anyone know how to solve this problem? Does anyone know if invoking of chatzill...

How to pass command line arguments to an extension using a command line on MacOS?
I am developing an extension that processes some command line options. I want to invoke the Firefox from a command line like this: > ...../firefox -myarg1 -myarg2 I am running into a problem on Mac OS. When I run such command when there is no FireFox running it works fine. However if I run the command again I get an error saying that there is already an instance of Firefox running and only one instance can run at a time (paraphrasing here). I have tried to use the -remote option but no luck. Does anyone know how to solve this problem? Does anyone know if invoking of chatzill...

how to get the content from mle line by line
I want to use the funtion TextLine(),but it need to set the insertion point,and I do not want to get all Use Mid() on the Text property to pick out any group of characters you'd like. "acme" <hbni@163.com> wrote in message news:4323f562$1@forums-1-dub... > I want to use the funtion TextLine(),but it need to set the insertion > point,and I do not want to get all > > yes I do this,and I think MLE should be enhanced "Jerry Siegel" <jerrys@dataDASHsciDOTcom.die.spammer.die.die.die> wrote in message news:43259333$1@forums-2-du...

Read output from script line by line
------_=_NextPart_001_01C56CC2.2D642292 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Hello all, Is there a way you read input from a script line by line. I'd rather parse output line by line than do: @out =3D `script.sh`; which seems sloppy. Or is that the best way? TIA ------_=_NextPart_001_01C56CC2.2D642292-- This way works open(my $FH, 'script.sh |') or die ...; while (my $line =3D <$FH>) { ... } close($FH); On 6/9/05, Tielman Koekemoer (TNE) <KoekemTN@telkom.co.za> wrote: >...

Getting Command line as is
Hi all, is it possible (and how) to get the command line as is, even if the parameters are separated by two or more spaces. "Colin Maharaj" <noreply@nohost.com> wrote in message news:374656@forums.embarcadero.com... > Hi all, > is it possible (and how) to get the command line > as is, even if the parameters are separated by > two or more spaces. Have you looked at the GetCommandLine() API function? http://msdn.microsoft.com/en-us/library/ms683156%28v=vs.85%29.aspx - Dennis No I did not, not until you told me but it is exactly what I wanted th...

Piping to command line
There are a couple of command line tools that I would love to be able to pipe the source of a document to with just a single click from firefox. I have looked over the documentation a bit but can't find a way to do that. That way things like total validator can validate pages behind a login, or pages that rely on session variables, as well as some custom tools in various states of being written. dwalter wrote: > There are a couple of command line tools that I would love to be able > to pipe the source of a document to with just a single click from > firefox. I have loo...

retreiving each line, line by line in a multiline tyextbox??
I have a multi line textbox and I need to be able to retreive the data in the textbox but line by line. Meaning: that in a multiline textbox in which a user has typed text which fills up 3 lines in the textbox; I need to retreive the first line, and then the second etc.. I figure since there is a text wrap, the data inside the textbox control must contain some formating characters somewhere like a line break, or new line, or linefeed etc... How is this done please? Thanks marc Why would you want to retrive each line independly?  One quick approach would be taking the whole string...

how to get the line number of a line in a Power script ?
in the C language, we have the variable __LINE__ to in,dicate to us the value of the current line at runtime. In PB script we would like to get the value of the line to print it or display it , but we don't known how to do can Somebody help me ? thanks PopulateError(0, 'whatever') MessageBox('', 'line number = ' + string(Error.Line) ) On Mon, 20 Mar 2000 16:28:59 +0100, "MARDUEL" <patrick.marduel@energy.alstom.com> wrote: >in the C language, we have the variable __LINE__ to in,dicate to us the >value of the current line at ru...

Web resources about - trying to get line-by-line output of a command pipe in perl - perl.beginners

In-Depth: A Line-by-Line Look at Facebook’s Updated Platform Developer Policies
As part of its game-focused product updates, Facebook changed a wide range of policies around what developers can and can’t do on the platform. ...

Line By Line
... shadow, hunt, patience, search, silence: the elements of fishing are perfect fodder for writing, but they can also lead to sentimental lines ...

Line By Line: Every Empty Promise in Obama’s Cuba Speech
... sweeping changes to the United States’ approach to diplomacy with Cuba yesterday, in a statement broadcast simultaneously with a speech by President ...

Poet Fired For Tweeting ‘Gone With The Wind’ Line By Line Because RACISM
'Unmediated quotes of Margaret Mitchell's novel' are 'unacceptable provocations'

Line by Line Analysis of President Obama’s Afghanistan Escalation Speech (Part 1)
... line. The President is an enormously gifted rhetorician ...

Sad Keanu No More: Twitter Account Recreates 'Speed' Line by Line
... line. And it all started with a tweet summing up the Keanu pièce de résistance. Image: Speed_1994 See also: 10 DIY Photo Filters to Think ...

Line by Line, E-Books Turn Poet-Friendly - NYTimes.com
Digital publishers have gotten better at creating e-books that preserve a poet’s meticulous formatting, but some writers are still leery of surrendering ...

Line by Line, E-Books Turn Poet-Friendly
... poet, first learned that the digital editions of his poetry looked nothing like the print version, he was stunned. There were no line breaks, ...

Read Joyce's Ulysses Line by Line, for the Next 22 Years, with Frank Delaney's Podcast - Open Culture ...
If you need someone to host a multi-decade podcast on James Joyce’s Ulysses, then why settle for less than the most eloquent man in the world? ...

“UNCONSCIONABLE POWER GRABS:” SOPA and PROTECT-IP: A Line-By-Line Analysis of the Bills We Must Kil…
“UNCONSCIONABLE POWER GRABS:” SOPA and PROTECT-IP: A Line-By-Line Analysis of the Bills We Must Kill. As I noted last night, they seem to be ...

Resources last updated: 11/30/2015 10:47:31 AM