derek3000 has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to write a little GUI using Tk to search through a log file of the user's choosing to look for a user-entered pattern. Also, I want to have radio buttons that they can use to modify the matching(i.e., case-sensitive, global, etc.). I tried a silly little driver program just to see if I could do it, and it doesn't seem to work:

sub driver{ my $pattern = shift; my $modifiers = shift; while(<LOG>){ if ($_ =~ /$pattern/$modifiers){ print $_; } close LOG; } }

ok, there may be typos in that, but then again, there may be typos in the driver program that I have. I looked in the camel and didn't find it. I get an error "string found where operator expected". If there's something up with the way I wrote it, I may have just typed it wrong. Is there anything special I have to do to interpolate the modifiers.

Replies are listed 'Best First'.
Re: Store regex modifiers in variables?
by no_slogan (Deacon) on Jul 10, 2001 at 00:07 UTC
    You can do it, but not the obvious way.
    /(?$modifiers)$pattern/

      Or /(?$modifiers:$pattern)/ where $modifiers can be "i-sm", for example, to turn on "i" and turn off "s" and "m". The first form (without a ":" affects the rest of the regex. The second form only affects the stuff between the parens.

      See "perldoc perlre" for more info. See also qr in "perldoc perlop".

              - tye (but my friends call me "Tye")

      I was looking for a similar issue. My problem wasn't with a match, but a substitution. This node is fairly old, however very relevant to my problem and I'm adding my comments in case someone needs a similar answer.

      <bold>Project details:</bold>
      I'm building a "interactive shell" to make our life easier. (DBA Group) In one shell I plan to tie Oracle, Sybase (DBI) and Sybase Replication together. Replication is also managed via DBI, but I provide a customized syntax for manipulating it. (They don't provide bulk loading functionality for heterogeneous environments. Thats done via other Perl scripts/modules) ;) I wanted to provide the user with a way of manipulating the input "buffer" via regex.

      The only modifiers I allow are i,s,m & g for simplicity. ('e' & 'm' would be more difficult to "grammerize" than I'm willing to spend time on) While i,s & m are fine using the syntax (?$mods), I don't believe the g is. Well, no big deal.. My solution:

      if ($mods=~s/g//g) { $buffer=~s/(?$mods)$expr1/$expr2/g; } else { $buffer=~s/(?$mods)$expr1/$expr2/; }

      Hope this helps someone else, like myself! Great thread, thanks to all!

      Regards,
      SMF 8)

Re: Store regex modifiers in variables?
by tachyon (Chancellor) on Jul 10, 2001 at 00:28 UTC

    You can also do it using an eval which is perhaps more obvious than no_slogan's method but not as elegant.

    $_ = 'foo('; $regex = 'FOO('; $modifiers = 'i'; # a bit of error checking never goes astray die "Illegal modifier!" unless $modifiers =~ m/^[ismxo]\z/; # escape everything except the essential regex char and \w \s # this is a security measure as discussed below $regex =~ s/([^\w\s\\|()[\]{}^\$*+?.])/\\$1/g; # generate the regex with error checking $regex = eval "qr/$regex/$modifiers"; die "Eval failed $@\n" if $@; # test it print "match" if /$regex/;

    There is a significant security issue eval-ing user supplied data, or in fact using raw user data in many places in a script. The problem is scary. If $regex contains a string like this '/; `some bad command`; $hacked =1' then when this interpolates we will eval

    $regex = eval "qr//; `some bad command`; $hacked =1/$modifiers"
    

    This may execute some bad command on your system with the same permissions as the script. Both the solutions presented suffer from this problem. To solve it (at least partially) you need to escape all the non regex metachars.

    $regex =~ s/([^\w\s\\|()[\]{}^\$*+?.])/\\$1/g;

    This will leave \w \s and the regex chars unescaped but throw in escapes for everything else including semicolons and backtics. It can probably still be hacked - did someone say merlyn - but the escapes should throw a spanner in the works for the casual hacker.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Store regex modifiers in variables?
by japhy (Canon) on Jul 10, 2001 at 01:39 UTC
Re: Store regex modifiers in variables?
by derek3000 (Beadle) on Jul 10, 2001 at 00:39 UTC
    thanks everyone. ++ all around. well, tomorrow when I have votes.