Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Archive::Zip matching different file extension

by Anonymous Monk
on Apr 14, 2021 at 21:00 UTC ( #11131296=note: print w/replies, xml ) Need Help??


in reply to Re^2: Archive::Zip matching different file extension
in thread XML::XPath matching different file extension

FYI, in the Perl world it is very common to find that for your convenience a library will accept either a string-pattern or a regular expression as an argument. Regular expressions, as haukex just demonstrated, are by far the most powerful alternative.
  • Comment on Re^3: Archive::Zip matching different file extension

Replies are listed 'Best First'.
Re^4: Archive::Zip matching different file extension
by AnomalousMonk (Bishop) on Apr 15, 2021 at 06:56 UTC
    ... it is very common to find that ... a library will accept either a string-pattern or a regular expression .... Regular expressions ... are by far the most powerful alternative.

    Strings and "regular expressions" (the Regexp object form produced by the qr// constructor) are both compiled to the same internal representation when used by a matching or match binding operator.

    Win8 Strawberry 5.8.9.5 (32) Thu 04/15/2021 1:56:15 C:\@Work\Perl\monks >perl use strict; use warnings; use Test::More 'tests' => 2; 1..2 use Test::NoWarnings; my $rqr = qr/\.(?:txt|png)$/i; # regex as Regexp object my $rsq = '(?i)\.(?:txt|png)$'; # regex as single-quoted string my @Inputs = qw( x.txt .txt x.png .png x.TxT .tXt x.pNg .PnG txt png .txtx .pngx foo xtxtx ); my @mqr = grep { $_ =~ $rqr } @Inputs; # matches from qr regex my @msq = grep { $_ =~ $rsq } @Inputs; # matches from '' regex print "qr matches: @mqr \n"; is_deeply \@mqr, \@msq, 'qr// vs q//'; ^Z qr matches: x.txt .txt x.png .png x.TxT .tXt x.pNg .PnG ok 1 - qr// vs q// ok 2 - no warnings
    There is no qr// expression for which an equivalent q// expression cannot (fairly easily) be written.

    That said, it's always best IMHO to write regexes as qr// expressions. Doing so avoids many subtle (and a few blatant) bugs having to do with differences in escaping and interpolation between single/double-quoted string constructors and the qr// constructor, and quantification is better-behaved:

    Win8 Strawberry 5.8.9.5 (32) Thu 04/15/2021 2:32:48 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $rqr = qr/foo/; my $rsq = q/foo/; my $s = 'foofoofoo'; print "$rqr match: '$1' \n" if $s =~ m{ ($rqr+) }xms; print "'$rsq' match: '$1' \n" if $s =~ m{ ($rsq+) }xms; ^Z (?-xism:foo) match: 'foofoofoo' 'foo' match: 'foo'
    I find it a neat syntactic feature that in $rqr? $rqr* $rqr+ $rqr{n} $rqr{n,m} expressions, the Regexp object is quantified atomically. This allows regexes to be factored in a reasonable way.

    (It should be noted that split has a number of special-case interpretations of its PATTERN argument (update: and some of these differentiate between string and Regexp representations).)


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

Re^4: Archive::Zip matching different file extension
by haukex (Bishop) on Apr 15, 2021 at 05:18 UTC
    in the Perl world it is very common to find that

    Too much of a generalization. One should always read the documentation as to the module's API.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-05-17 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (152 votes). Check out past polls.

    Notices?