Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: check parameters

by hdb (Parson)
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 exploiting the Monastery: (14)
As of 2014-07-25 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (173 votes), past polls