On 5/14/07, tannhauser <[EMAIL PROTECTED]> wrote:
snip
My problem:
I also want to replace quotions, depending whether they are at the beginning or
the end of a word. For example "tick" should become ``tick''. Annoyingly, my 
script just
gives me ``$1ic$1''. Any ideas?
snip

The problem here is that the replace part of s/// is for all effective
purposes a double quoted string.  Double quoted strings do allow
interpolation when they are seen as literals, but not on each use.
And this is a good thing (tm), can you imagine how confusing it would
be if you had to deal with a situtation like this:

my $doc = "the unknown amount of dollars is represented by \$x";
print $doc, "\n";

The interpreter would try to interpolate $x into $doc and since it
doesn't exist you would get an error.  You would have to do what the
poor shell programmers do and include as many backslashes as necessary
to get it to the right value when it was used.  But enough about what
isn't the case.

It also isn't as easy as tacking on an e option since that just brings
us full circle with the interpolated string problem, but tacking on an
e option and calling eval on the string does the trick; however, it
looks like you have over generalized the problem and are just making
work for yourself and the interpreter.  In addition, if you are
reading through the file line-by-line you will miss situations like
this

"foo foo bar
bar baz baz"

And of course you may need to deal with escaped quotes

"foo \"bar\" baz"

I would suggest you de-generalize your code some and take a look at
Conway's Text::Balanced*.

#!/usr/bin/perl
use strict;
use warnings;

my $search  = qr{"([^"]+?)"};
my $replace = q(qq{``$1"});

my $value = qq("foo bar baz");

print "$value\n";
$value =~ s/$search/eval $replace/ge;
print "$value\n";

* http://search.cpan.org/~dconway/Text-Balanced-v2.0.0/lib/Text/Balanced.pm

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to