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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |