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

I have written a program which can load regular expressions from a user designated file containing expressions in a standardized format, but it does not work successfully with all legal expressions. I would like to determine if there is a way to have the following expressions parsed appropriately to achieve the desired result of finding a 'shebang' line and inserting a usage line for the English module, (surrounded by two blank lines!)

Here's a copy of an 'LTL'-separated line which I am loading from a file containing a hash key, a search expression, and a replace expression:

" (^\#!/usr/bin/perl[^\n]*\n)LTL(^\#!/usr/bin/perl[^\n]*\n)LTL(^\#!/usr/ +bin/perl[^n]*n)LTL${ "
When loaded into my code it yields:
$regExpKey = (^\#!/usr/bin/perl[^\n]*\n) $searchPattern = (^\#!/usr/bin/perl[^\n]*\n) $replacementPattern = ${^MATCH}\x0Ause English \( no_match_vars \);\x0 +A
When I slurp the file to be searched into the $content variable I execute
$content =~ s{$searchPattern}{${^MATCH}$replacementPattern}gxms;
and it $content becomes the following:
" ${^MATCH}\x0Ause English \( no_match_vars \);\x0A "

e.g. ${^MATCH} is not interpolated(?) and the newlines, (\x0A), are not inserted, even if I were to use "\n"'s!

Please help me understand whether or not what I am attempting is doable and/or practical and, if so, how do I rectify what I am doing in error?

20090605 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: how to load regular expressions from a file
by CountZero (Bishop) on Jun 02, 2009 at 13:48 UTC
    Please put your code inside <code> ... </code>-tags. As it is now, it gives me headaches just looking at it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: how to load regular expressions from a file
by ikegami (Patriarch) on Jun 02, 2009 at 14:34 UTC

    ${^MATCH} is not interpolated?

    Indeed, because ${^MATCH} doesn't appear in any literals. Perl isn't in the habit it executing your strings without being told.

    my $replacementExpr = '"${^MATCH}\\x0Ause English ( no_match_vars );\\x0A"'; s{$searchPattern}{eval($replacementExpr)}egxms;