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,


In reply to Re: Hurdle with summarizing results from complex data structure related to MySQL result handling by Athanasius
in thread Hurdle with summarizing results from complex data structure related to MySQL result handling by hiyall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.