Adam W wrote:
> Hello All,

Hello,

> I'm using '-w' like any good hacker, but every time I try to use
> backreferences in my regexps, I get a warning "\1 better written as $1
> at...."
> 
> I'm confused because, according to perlretut:
>     "Although $1 and \1 represent the same thing, care should be taken
> to use matched variables $1, $2, ... only outside a regexp and
> backreferences \1, \2, ... only inside a regexp; not doing so may lead
> to surprising and/or undefined results."
> 
> The other source of relevant information seems to be here:
> <http://www.perl.com/doc/manual/html/pod/perlre.html#WARNING_on_1_vs_1>
> However, I'm having trouble understanding if it is referring to using
> backreferences in general, or to a particular case where using \1
> instead of $1 is a bad idea.
> 
> 
> Here is an example of one of my regexps that produces this warning:
> $text =~ s!(.*?)(\()(.*?)(\))!<a\ href=\"\3\" alt=\"\3\">\1<\/a>!g;
> 
> Should I be using $1 and $3 instead of \1 and \3 in this case, and if
> so, why?

The first part of s/// is a regular expression so you have to use \1, \2, \3,
etc. however the second part of s/// is a double quoted string so it is
preferred that you use $1, $2, $3, etc. because in a double quoted string the
escape sequences \1, \2, \3, etc. are usually used as octal codes for 
characters.

$ perl -le'print "\120\145\162\154"'
Perl


BTW, why capture $2 and $4 if you are not using them and why is everything
backslashed?

$text =~ s!(.*?)\((.*?)\)!<a href="$2" alt="$2">$1</a>!g;




John
-- 
use Perl;
program
fulfillment

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


Reply via email to