in reply to Execute regexp on directory tree

Your problem lies here: my $substitute = qq{throw \("$1"\)};

Using qq makes Perl interpolate variables which means that the current value of $1 (at this place undef) is inserted.

Update:
Well, typical, sorry, didn't double check what I wrote (and deleted now :). You have to use the following trick to achieve what you want:

my $substitute = q{qq{throw ("$1")}}; # and later substitute like this $line =~ s/$regex/$substitute/ee;
The reason for this is that perl is not automatically doing an interpolation on the substitution string you insert. You have to force that by evalutating before substitution.

-- Hofmator

Replies are listed 'Best First'.
Re: Re: Execute regexp on directory tree
by karmas (Sexton) on Dec 12, 2001 at 16:02 UTC
    I tried to make changes you proposed, but, it didn't work: instead of
    s/throw "[^"]+"/throw ($1)/
    The result semms like using:
    s/throw "[^"]+"/throw (\$1)/
    So no substitution using matched values is performed, I guess I need that substitution regexp operator would evaluate the $substitute, whereas now it doesn't and just replaces using literal value. P.S. Anarion's suggestion also didn't work for me:
    C:\Projects\Config2\9XImage.cpp: throw "Error writing to floppy!"; qq<throw ("$1")>
    Also literal evaluation.