Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy array into input read process?

21 views
Skip to first unread message

Tuxedo

unread,
Nov 1, 2012, 4:43:53 PM11/1/12
to
The below procedure, using lynx, simply dumps the source of a URL when
calling it with a single argument and prints each line:

open(my $fh, '-|',
'/usr/bin/lynx','-source','-nonumbers','-cache=0','-nolist', $ARGV[0]) or
die $!;
while (my $line = <$fh>) {
print $line;
}

Placed in a lynx.pl file, it can be run as
./lynx.pl example.com

Following the '-|' bit some fixed options are passed:
'/usr/bin/lynx','-source','-nonumbers','-cache=0','-nolist'
... and thereafter the URL.

How can this be better done in allowing arguments to be passed like:
./lynx.pl example.com -source -nonumbers -cache=0 -nolist

... then capturing the arguments somehow like follows:
open(my $fh, '-|', '/usr/bin/lynx', @flags $ARGV[0]) or die $!;

In the end it's not much different from running lynx directly but I would
like some fixed and some optional arguments passed, and I'm not sure how to
construct and push additional arguments into a list, then copy the final
array into the file handle reading process between the -| bit and the
$ARGV[0] to allow for a flexible number of command line options.

Many thanks for any ideas.

Tuxedo

J. Gleixner

unread,
Nov 1, 2012, 5:41:00 PM11/1/12
to
my $cmd = q{ /usr/bin/lynx -source -nonumbers -cache=0 -nolist };

print `$cmd @ARGV`;

To handle command line options, give the Getopt::Long module a try, or
one of the other Getopt modules. You could set defaults and over-ride
them or set new ones via the command line.

Tuxedo

unread,
Nov 2, 2012, 2:58:08 AM11/2/12
to
J. Gleixner wrote:

> On 11/01/12 15:43, Tuxedo wrote:
[...]

>
> my $cmd = q{ /usr/bin/lynx -source -nonumbers -cache=0 -nolist };
>
> print `$cmd @ARGV`;
>
> To handle command line options, give the Getopt::Long module a try, or
> one of the other Getopt modules. You could set defaults and over-ride
> them or set new ones via the command line.

Thanks for these tips. I will try Getopt::Long.

Tuxedo

Ben Morrow

unread,
Nov 5, 2012, 3:41:58 PM11/5/12
to

Quoth "J. Gleixner" <glex_n...@qwest-spam-no.invalid>:
> On 11/01/12 15:43, Tuxedo wrote:
> > The below procedure, using lynx, simply dumps the source of a URL when
> > calling it with a single argument and prints each line:
> >
> > open(my $fh, '-|',
> > '/usr/bin/lynx','-source','-nonumbers','-cache=0','-nolist', $ARGV[0]) or
> > die $!;
> > while (my $line =<$fh>) {
> > print $line;
> > }
> >
> > Placed in a lynx.pl file, it can be run as
> > ./lynx.pl example.com
> >
> > Following the '-|' bit some fixed options are passed:
> > '/usr/bin/lynx','-source','-nonumbers','-cache=0','-nolist'
> > ... and thereafter the URL.
> >
> > How can this be better done in allowing arguments to be passed like:
> > ./lynx.pl example.com -source -nonumbers -cache=0 -nolist
> >
> > ... then capturing the arguments somehow like follows:
> > open(my $fh, '-|', '/usr/bin/lynx', @flags $ARGV[0]) or die $!;
> >
> > In the end it's not much different from running lynx directly but I would
> > like some fixed and some optional arguments passed, and I'm not sure how to
> > construct and push additional arguments into a list, then copy the final
> > array into the file handle reading process between the -| bit and the
> > $ARGV[0] to allow for a flexible number of command line options.
>
>
> my $cmd = q{ /usr/bin/lynx -source -nonumbers -cache=0 -nolist };
>
> print `$cmd @ARGV`;

No. What happens if I run

./lynx.pl "; rm -rf /"

? (Not to mention the futility of capturing the output into a buffer
only to print it out again immediately.)

Try

my @lynx = qw{ /usr/bin/lynx -source -nonumbers -cache=0 -nolist };
open(my $fh, "-|", @lynx, @ARGV) or ...;

or maybe

exec @lynx, @ARGV;

or, better, learn how to use shell.

Ben

0 new messages