On Mon, 2008-09-01 at 04:42 -0700, John W. Krahn wrote:
> Paolo Gianrossi wrote:
> > Hello, Experts!
>
> Hello,
> > However what i want to do is subtly different: i'd like to do this:
> >
> > my $rexp="m/match/g";
> > $text=~$rexp;
>
> The only way to use the /g option like that is to use eval:
>
> my $rexp = 'match';
>
> eval { $text =~ /$rexp/g };
>
>
> > or even
> >
> > my $rexp="s/match/subst/g";
> > $text=~$rexp;
>
> Same here with the /g option:
>
> my $rexp = 'match';
> my $replace = 'subst';
>
> eval { $text =~ s/$rexp/$replace/g };
>
>
First of all thank you (and all the others ;) for answering
This looks more hairy than I think it should...
Since I think I omitted some constraints I have, let me try to explain
my issue a tiny bit better.
What I would like to do is the following. I have a text file which I
slurp. Its contents are in $content.
Now I want to ask the user for a regex (any regex) and highlight where
(if) it matches.
If I try to hard cable the whole thing, it works just fine:
use strict;
use warnings;
use diagnostics;
my $text="this is a random text. Please match random string.\nAnother
random something.\n";
while ($text =~ s/random/final/){ #here it is hard-coded
my $l=length($`);
print substr($text, 0, $l);
print ">";
print substr($text, $l);
}
This behaviour is just what I want. Only, I'd like to ask for the regexp
to the user:
use strict;
use warnings;
use diagnostics;
my $text="this is a random text. Please match random string.\nAnother
random something.\n";
my $rexp=<STDIN>;
while ($text =~ $rexp){ #here it isn't hardcoded anymore.
my $l=length($`);
print substr($text, 0, $l);
print ">";
print substr($text, $l);
}
Of course this works not.
Also, though,
...
while (eval{$text =~ $rexp}){ # try to evaluate this, but not in the
# proper way
my $l=length($`);
print substr($text, 0, $l);
print ">";
print substr($text, $l);
}
doesn't work. eval{$text =~ $rexp} is always undef. Now, this puzzles
me, but whatever. My major point is that perldoc perlvar tells me that
$` and friends are dynamically local to the eval block, so I am no game.
Any clue about how to solve this?
thanks a lot again for any help :)
> Also see:
>
> perldoc -q "How can I expand variables in text strings?"
oh btw, didn't know about -q... That's something cool!
cheers
paolino
--
Paolo Gianrossi
Softeco Sismat S.p.A. - R&D Division
via De Marini 1, 16149 Genova
Tel: +39 010 6026 332 - Fax: +39 010 6026 350
E-Mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/