Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

Comment on check parameters
Download Code
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
Re: check parameters
by hdb (Prior) 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?
        Ugh, actually something about that doesn't work.... It passes when it shouldn't :(

        Untested:

        if (!grep { /^${\ (join '|', @{$opts->{params}}) }/i } keys %$format)
        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027509]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-10-23 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls