Copy array into input read process?

Discussion in 'Perl Misc' started by Tuxedo, Nov 1, 2012.

  1. Tuxedo

    Tuxedo Guest

    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
    Tuxedo, Nov 1, 2012
    #1
    1. Advertising

  2. Tuxedo

    J. Gleixner Guest

    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.
    >
    > 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.
    J. Gleixner, Nov 1, 2012
    #2
    1. Advertising

  3. Tuxedo

    Tuxedo Guest

    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
    Tuxedo, Nov 2, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    702
    Allan Herriman
    Sep 9, 2003
  2. Alex
    Replies:
    2
    Views:
    1,195
  3. Replies:
    26
    Views:
    2,075
    Roland Pibinger
    Sep 1, 2006
  4. Marcus
    Replies:
    25
    Views:
    1,592
    Francesco
    Aug 19, 2009
  5. Gauri
    Replies:
    3
    Views:
    170
Loading...

Share This Page