[Chicago-talk] Q on '-n'

Steven Lembark lembark at jeeves.wrkhors.com
Fri Sep 19 01:16:19 CDT 2003



--On Tuesday, September 16, 2003 02:14:39 -0500 Walter Torres 
<walter at torres.ws> wrote:

> I'm trying to figure out how to utilize '-n' best.
>
> I have a cmd line...
>
>    myScript.pl file_1.txt file_2.txt -o result.out
>
> I want to cycle through all file[s] given by the cmd line (if it's a path,
> all the files in that dir, but that's for later) and spit the results
> (whatever it may be) into the file given via the '-o' parameter.
>
> My test script (see below)
>
> Is this the best way to accomplish this?
>
> Thanks for your help.

See Getopt::Long.
Also you want the switch before the command line arguments:

    foo -o bar.out file1 file2 file3;

so:

    #!/blah/perl -w

    use Getopt::Long;

    my @optionz =
    qw(
        verbose!
        outpath=s
    );

    my $cmdline = {};

    unless( GetOptions($cmdline, at optionz) )
    {
        print "Usage: ... ";
        exit 2;
    }

    my $verbose = $cmdline->{verbose} || 0;

    my $outpath = $cmdline->{outpath} || "./defaultpath.out";

    open my $fh, '>', $outpath or die "$outpath: $!";

    ...



-- 
Steven Lembark                                            2930 W. Palmer
Workhorse Computing                                    Chicago, IL 60647
                                                         +1 888 910 1206



More information about the Chicago-talk mailing list