Copy array into input read process?

T

Tuxedo

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

J. Gleixner

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


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.
 
T

Tuxedo

J. Gleixner said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top