For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > Read output from script line by line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Read output from script line by line
Tielman Koekemoer \

2005-06-09, 8:56 am


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 = `script.sh`; which seems
sloppy. Or is that the best way?

TIA


Marcos Rebelo

2005-06-09, 8:56 am

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:
>=20
> Hello all,
>=20
> 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?
>=20
> TIA
>=20
>=20
>

Thomas Bätzler

2005-06-09, 8:56 am

Tielman Koekemoer (TNE) <KoekemTN@telkom.co.za> asked:
> Is there a way you read input from a script line by line. I'd
> rather parse output line by line than do: @out = `script.sh`;
> which seems sloppy. Or is that the best way?


No, not really.

You can do a "pipe open" like this

my $code = '/path/to/your/script';
my @args = qw( -x -y -z );

open( IN, '-|', $code, @args ) or die "Can't spawn '$code': $!";

and then leisurely read your input:

while( my $line = <IN> ){
# do something
}

In any case, I recommend that you read the perlopentut and
perlipc manpages for thw whole story ;-)

HTH,
Thomas
Tielman Koekemoer \

2005-06-09, 8:56 am

=20
Thanks Marcos.

> -----Original Message-----
> From: marcos rebelo [mailto:oleber@gmail.com]=20
> Sent: 09 June 2005 09:48 AM
> To: Tielman Koekemoer (TNE)
> Cc: Perl Beginners
> Subject: Re: Read output from script line by line
>=20
> This way works
>=20
> open(my $FH, 'script.sh |') or die ...;
> while (my $line =3D <$FH> ) {
> ...
> }
> close($FH);
>=20
> On 6/9/05, Tielman Koekemoer (TNE) <KoekemTN@telkom.co.za> wrote:
> I'd rather=20
[color=darkred]
>=20

Tielman Koekemoer \

2005-06-09, 8:56 am

Thanks Thomas.=20

> -----Original Message-----
> From: Thomas B=E4tzler [mailto:t.baetzler@bringe.com]=20
> Sent: 09 June 2005 09:50 AM
> To: 'Perl Beginners'
> Cc: Tielman Koekemoer (TNE)
> Subject: RE: Read output from script line by line
>=20
> Tielman Koekemoer (TNE) <KoekemTN@telkom.co.za> asked:
> I'd rather=20
[color=darkred]
>=20
> No, not really.
>=20
> You can do a "pipe open" like this
>=20
> my $code =3D '/path/to/your/script';
> my @args =3D qw( -x -y -z );
>=20
> open( IN, '-|', $code, @args ) or die "Can't spawn '$code':

$!";
>=20
> and then leisurely read your input:
>=20
> while( my $line =3D <IN> ){
> # do something
> }
>=20
> In any case, I recommend that you read the perlopentut and=20
> perlipc manpages for thw whole story ;-)
>=20
> HTH,
> Thomas
>=20

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com