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

Re: check parameters

by hdb (Prior)
on Apr 08, 2013 at 14:18 UTC ( #1027514=note: print w/ replies, xml ) Need Help??


in reply to check parameters

Build the pattern first, then use it.

my $pattern ="(".join('|', @{$opts->{params}}).")"; if (!grep { /^$pattern/i } keys %$format) { ...


Comment on Re: check parameters
Download Code
Re^2: check parameters
by ag4ve (Monk) on Apr 08, 2013 at 14:26 UTC
    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 :(

        What is wrong with the code hdb gave you?

        That code does not have the problem your code has, so why are you not using it?

        As a hint, a string is always true in Perl. And your code simply constructs a string but never matches against it:

        ... if ("(".join('|', @{$opts->{params}}).")" # This constructs a string and !grep { /^$_/i } keys %$format) # and this matches $_ agains +t itself ...
        This should fail and doesn't:
        use strict; use warnings; use Pod::Usage; my $format = { abc => 1, bar => 1, fii => 1, wee => 1, }; my $opts; $opts->{params} = ["a", "fa"]; my $str = "(".join('|', @{$opts->{params}}).")"; if (!grep { /^$str/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";

      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: note [id://1027514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2015-07-03 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (55 votes), past polls