Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

check parameters

by ag4ve (Monk)
on Apr 08, 2013 at 14:04 UTC ( #1027509=perlquestion: print w/replies, xml ) Need Help??
ag4ve has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to get a check that tests the first letter or the full word of parameters and it is failing (or succeeding I suppose).
use strict; use warnings; use Pod::Usage; my $format = { abc => 1, bar => 1, fii => 1, wee => 1, }; my $opts; $opts->{params} = ["a", "b"]; if (!grep { /^(join('|', $opts->{params}))/i } keys %$format) { pod2usage( -verbose => 0, -output => \*STDERR, -msg => "$0 bad parameter difinition used [" . join(", ", so +rt {$a cmp $b} @{$opts->{params}}) . "].\n" . "Possible parameters are: " . join(", ", sort {$a cmp $b} keys %$format) . "\n"); } print "1\n";
EDIT: It seems I haven't defined what should succeed and fail - it should match the first characters in a string. So, In the above example with parameters: abc, bar, fii, and wee,
a should succeed
b should succeed
ac should fail
aB should succeed
fA should fail
ee should fail

Replies are listed 'Best First'.
Re: check parameters
by hdb (Monsignor) on Apr 08, 2013 at 14:18 UTC

    Build the pattern first, then use it.

    my $pattern ="(".join('|', @{$opts->{params}}).")"; if (!grep { /^$pattern/i } keys %$format) { ...
      Ah, should've figured. Thanks that works.
      if ("(".join('|', @{$opts->{params}}).")" and !grep { /^$_/i } keys %$ +format)
      That looks aweful - is there a better way of doing this?


        if (!grep { /^${\ (join '|', @{$opts->{params}}) }/i } keys %$format)
        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
        Ugh, actually something about that doesn't work.... It passes when it shouldn't :(
Re: check parameters
by blue_cowdawg (Monsignor) on Apr 08, 2013 at 14:13 UTC

    Why not just use Getopt::Long? Sample:

    #!/usr/bin/perl -w use strict; use Getopt::Long; my $abc = 1; my $bar=1; my $fii=1; my $wee=1; my $result = GetOptions("abc=i" => \$abc, "bar=i" => \$bar, "fii=i" => \$fii, "wee=i" => \$wee ); | etc...
    If I remember correctly (can't remember what I had for breakfast and I have the same thing every day) Getopt::Long is part of CORE and therefore you need to load it on your system.

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      I am using Getopts::Long. This is:
      GetOptions( param|p=s@ => \$opts->{params} );
      What I presented was a test showing where I am failing.
            I am using Getopts::Long. This is:

        Didn't notice that in the code you presented.

        I'm not sure what is failing in your code or why. What were you expecting? Seems to me you could accomplish what you are trying to do with much more readable code.

        If I'm trying to test for the presence of a command line parameter I normally set the variables to something null and test later to see if they've been set. Here's an example:

        | handwaving my $result = GetOptions( 'file=s' => \$infile, 'debug' => \$debug, 'verbose' => \$verbose, 'out=s' => \$outfile ); upchuck_and_die unless ( $file && $outfile ) ; | | stuff | later on sub upchuck_and_die { print "you must specify --infile=<file> and --outfile=<otherfile> +\n"; exit(-1); }
        You could do the same thing with a hashref but I like the readability of using discrete variables instead.

        “Let us redefine progress to mean that just because we can do a thing, it does not necessarily mean we must do that thing.” – Federation President, Star Trek VI

        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027509]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2018-05-27 20:36 GMT
Find Nodes?
    Voting Booth?