You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Gavin Spomer <ga...@solaris.ellensburg.com> on 2001/02/07 16:19:31 UTC

$& and other special variables

 Does Embperl use the variables $&, $`, $', $1, $2 etc for things other than 
pattern matching? I'm trying to take some text and find all occurances of a
 string in it and make those occurances in bold font, even if they are only part 
of a word (http://www.kvic.com/f.epl):

         [-
         $a = "Bill, make sure you take care of the billing and send Bill a 
bill";
         @words = split(/ /, $a);
         -]
         [$ foreach $word (@words) $]
                 [$ if $word =~ /bill/i $]
                         [+ $` +]<b>[+ $& +]</b>[+ "$' " +]
                 [$ else $]
                         [+ "$word " +]
                 [$ endif $]
         [$ endforeach $]

 This code outputs:

         make sure you take care of the and send a

 ... which means the pattern is getting matched, but $`, $& and $' have no 
values. When implimenting a standard (non-CGI) version of the above code:

         $a = "Bill, make sure you take care of the billing and send Bill a 
bill";
         @words = split(/ /, $a);

         foreach $word (@words)
         {
                 if($word =~ /bill/i)
                 {
                         print "$`<b>$&</b>$' ";
                 }
                 else
                 {
                         print "$word ";
                 }
         }

 ... it outputs my desired result:

         <b>Bill</b>, make sure you take care of the <b>bill</b>ing and send 
<b>Bill</b> a <b>bill</b>

 Anyone know what Embperl does with these special variables?

- Gavin

"People are the worst drivers in the world" 
http://www.singlespeedsanonymous.com/

Join PayPal and get FIVE FREE DOLLARS (no joke!):
https://secure.paypal.x.com/refer/pal=gavinspomer%40hotmail.com
... and it's a very usefull account to have!




Re: $& and other special variables

Posted by Gerald Richter <ri...@ecos.de>.

> Does Embperl use the variables $&, $`, $', $1, $2 etc for things other
than
> pattern matching?

Embperl doesn't use them in any way.

But they are local, so you have to use them in the same block or assign them
to a variable first.

BTW $&, $`, $' are very expensive in Perl, it's better to use $1 $2 etc. or
assign them directly into a list e.g.

                  [$ if ($pre, $match, $post) = $word =~
/^(.*?)(bill)(.*?)$/i $]
                          [+ $pre +]<b>[+ $match +]</b>[+ $post +]
                  [$ else $]
                          [+ "$word " +]
                  [$ endif $]

Gerald

-------------------------------------------------------------
Gerald Richter    ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post:       Tulpenstrasse 5         D-55276 Dienheim b. Mainz
E-Mail:     richter@ecos.de         Voice:    +49 6133 925131
WWW:        http://www.ecos.de      Fax:      +49 6133 925152
-------------------------------------------------------------