Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: check parameters

by ag4ve (Monk)
on Apr 08, 2013 at 14:26 UTC ( #1027518=note: print w/ replies, xml ) Need Help??


in reply to Re: check parameters
in thread check parameters

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?


Comment on Re^2: check parameters
Download Code
Replies are listed 'Best First'.
Re^3: check parameters
by tobyink (Abbot) on Apr 08, 2013 at 23:36 UTC

    Untested:

    if (!grep { /^${\ (join '|', @{$opts->{params}}) }/i } keys %$format)
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re^3: check parameters
by ag4ve (Monk) on Apr 08, 2013 at 14:31 UTC
    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 ...

        ag4ve, can you pls state what you want to achieve? Here is what happens step-wise:

        my $pattern ="(".join('|', @{$opts->{params}}).")"; # pattern is now "(a|b)" my @test = grep { /^$pattern/i } keys %$format; # @test = ( "bar", "abc" )

        !grep... puts grep into scalar context, ie it will return 2, the length of @test above. !2 is false therefore your "if" block will not be executed.

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

        You want it to fail if one of them fails, correct? Try this:

        if ( scalar( grep { /^$pattern/i } keys %$format ) < scalar( keys %$fo +rmat ) ) { ...

        Or this? (Observe the position of the exclamation mark.)

        if ( grep { !/^$str/i } keys %$format)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2015-07-30 21:42 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 (273 votes), past polls