in reply to searching files
If you don't want to follow the advice of others concerning Path::* (and there are other modules to do similar things) and want to roll your own regex, be warned that regex be tricky.
For instance, the qr/${user_input}_ABC_.*pd/ regex used here fails (i.e., accepts as valid) the xyzabc_ABC_hipdip filename. (There's also the problem that the logical sense of the test is wrong, but that's minor.)
Line-end anchors \A \z can be useful here, as can \Q \E metaquoting escape sequences to guard against regex metacharacters included in the $user_input portion of the filename. E.g.:
Note that the initialization of the $user_input has been changed to 'ab+c' so as to include a regex metacharacter as an example of the effect of the \Q \E escapes. Update: The quotemeta built-in can be used instead of \Q \E escapes:c:\@Work\Perl\monks>perl -wMstrict -le "my $user_input = 'ab+c'; my $excluded = qr{ \A \Q$user_input\E _ABC_ .* [.] pdf \z }xms; ;; FILE: foreach my $file (qw( ab+c_ABC_something.pdf ab+c_ABC_anything.pdf ab+c_ABC_.pdf xab+c_ABC_.pdf ab+c_ABC_.pdfx xab+c_ABC_.pdfx abc_ABC_something.pdf abc_ABC_anything.pdf abc_ABC_.pdf abc _.pdf xyzabc_ABC_hipdip foobar )) { next FILE if $file =~ $excluded; print qq{'$file' is not excluded}; } " 'xab+c_ABC_.pdf' is not excluded 'ab+c_ABC_.pdfx' is not excluded 'xab+c_ABC_.pdfx' is not excluded 'abc_ABC_something.pdf' is not excluded 'abc_ABC_anything.pdf' is not excluded 'abc_ABC_.pdf' is not excluded 'abc' is not excluded '_.pdf' is not excluded 'xyzabc_ABC_hipdip' is not excluded 'foobar' is not excluded
my $user_input = quotemeta 'ab+c';
my $excluded = qr{ \A $user_input _ABC_ .* [.] pdf \z }xms;
Note also that thorough testing is wise in case you are using a regex approach; see Test::More and its ilk.
Please see perlre, perlretut, and perlrequick.
Give a man a fish: <%-{-{-{-<
|
---|