Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: check parameters

by hdb (Monsignor)
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) { ...

Replies are listed 'Best First'.
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?


      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 :(

        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";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027514]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-02-18 00:19 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.