Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Hurdle with summarizing results from complex data structure related to MySQL result handling

by Athanasius (Archbishop)
on Feb 17, 2015 at 16:11 UTC ( [id://1117003]=note: print w/replies, xml ) Need Help??


in reply to Hurdle with summarizing results from complex data structure related to MySQL result handling

Hello hiyall,

Feedback and critique is welcome.

roboticus has already provided an excellent answer, so I’ll just comment on one aspect of your code:

my($grant,$db,$schema,$user,$scope) = $results[0] =~ m/.*GRANT\s(.+)\s +ON\s(.+)\.(.+)\sTO\s(\S+)\@(\S+)/ix; if (defined $user && length $user > 0) { if (defined $db && length $db > 0) { $db =~ tr/'`//d; } if (defined $schema && length $schema > 0) { $schema =~ tr/'`//d; } if (defined $user && length $user > 0) { $user =~ tr/'`//d; } if (defined $scope && length $scope > 0) { $scope =~ tr/'`//d; } ... }

First, there is no point in testing the length of each string, because the capture groups are all quantified with a +, meaning “one or more” — so if a match succeeds at all (defined is true), the length must be at least 1.

Second, there is no point in separately testing whether each string is defined, as the regex — any regex — either succeeds or it fails, so either all the match strings are defined, or none of them are.1

Third, note that the trailing /x does nothing here, as you don’t have any whitespace in the regex.

So the above code can be better written like this:

if (my ($grant, $db, $schema, $user, $scope) = $results[0] =~ /.*GRANT +\s(.+)\sON\s(.+)\.(.+)\sTO\s(\S+)\@(\S+)/i) { $db =~ tr/'`//d; $schema =~ tr/'`//d; $user =~ tr/'`//d; $scope =~ tr/'`//d; ... }

1Update: Except in the cases where the captures are optional, either because zero matches are allowed: X?, X*, X{0,5}; or because the captures are part of an alternation: (?:(X)|(Y)). Thanks to AnomalousMonk for explaining this, below.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

  • Comment on Re: Hurdle with summarizing results from complex data structure related to MySQL result handling
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Hurdle with summarizing results from complex data structure related to MySQL result handling
by AnomalousMonk (Archbishop) on Feb 17, 2015 at 21:17 UTC
    ... any regex ... either succeeds or it fails, so either all the match strings are defined, or none of them are.

    A minor cavil: capture groups that are not required to match for an overall match will return undef in their respective $n capture variables and in list context.

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = 'abc'; ;; my @captures = $s =~ m{ (?: (a)(b)(c) | (w)(x)(y)) (z)? }xms; dd \@captures; " ["a", "b", "c", undef, undef, undef, undef]
    The  (?|pattern) construct available with Perl version 5.10 (see Extended Patterns in perlre) modifies this behavior in alternations, but you may still be left with undefs from other sources.
    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use 5.010; ;; my $s = 'abc'; ;; my @captures = $s =~ m{ (?| (a)(b)(c) | (w)(x)(y)) (z)? }xms; dd \@captures; " ["a", "b", "c", undef]


    Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found