On Mon, 2008-09-01 at 11:40 -0400, Mr. Shawn H. Corey wrote:
> On Mon, 2008-09-01 at 15:46 +0200, Paolo Gianrossi wrote:
> > 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>;
> 
> chomp( $rexp );
> # I take it that you want $rexp to be
> # of the form: s/random/final/
> my $replacement = '';
> if( $rexp =~ /^s(.)/ ){
>   my $delimiter = $1;

# Oops, make this:
my $delimiter = quotemeta( $1 );

>   ( undef, $rexp, $replacement ) = split /$delimiter/, $rexp;
> }else{
>   die "badly formatted substitution: $rexp";
> }
> 
> > while ($text =~ $rexp){ #here it isn't hardcoded anymore.
> 
> while( $text =~ s/$rexp/$replacement/g ){
> 
> >   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!
> 
> `perldoc -q` searches only the FAQ.  See http://perldoc.perl.org/ for
> search engine that searches all of the perldoc's.  Make sure it is
> searching the correct version; to see what version of Perl you are
> running, enter:
> 
>   perl -v
> 
> 
> -- 
> Just my 0.00000002 million dollars worth,
>   Shawn
> 
> "Where there's duct tape, there's hope."
>         Cross Time Cafe
> 
> "Perl is the duct tape of the Internet."
>         Hassan Schroeder, Sun's first webmaster
> 
> 
-- 
Just my 0.00000002 million dollars worth,
  Shawn

"Where there's duct tape, there's hope."
        Cross Time Cafe

"Perl is the duct tape of the Internet."
        Hassan Schroeder, Sun's first webmaster


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


Reply via email to