>> You're supposed to use the \1 format to match a current match, 
>> like a duplicated word 
>> $ echo "her here hear hear hop hip hip ho!" | perl  \
  -pe 's/(\w+)\s+\1\s+/double "${1}s" /g;' 
 
Barry B wrote:
> I am confused about this. I thought that a back-reference looks like 
"$1", not "\1". Is there a difference?
 
Yeah, mostly w/ placement. Back refs on the left hand side (LHS) of the 
subst:
s/(\w+)\s+\1\s+/

are backslash digit. Backreferences to the captured match on the RHS use 
$1 as they do outside the subst command.  This got made more concrete 
somewhere in early v5, I believe. As noted, warnings will tell you:
\1 better written as $1 at -e line 1

if you had tried:
$ echo "her here hear hear hop hip hip ho!" | perl  \
  -w  -pe 's/(\w+)\s+\1\s+/double "\1s" /g;' 


though it still works.  But the idea is the \1 version can be used during 
the course of the matching phase, but $1 version is used during the 
replacement phase. In a sense, the \1 'magic var' is supposed to be 
localized to the LHS:
s/ ... /

context, while $1 et alia are actual globals so you can do:
>  -pe 'if ( s/(\w+)\s+\1\s+/double "${1}s" / ) { warn "found a $1\n"; }' 

and have a value outside the subst command. Trying "\1" in the warn():
warn "found a \1\n"; 

would get you ... well you get the "001" char ;->

$  echo "her here hear hear hop hip hip ho" | perl -pe 'if ( 
s/(\w+)\s+\1\s+/double "${1}s" / ) { warn "found a $1\n" };'
found a hear
her here double "hears" hop hip hip ho

but (note, I dropped the "/g"):
$  echo "her here hear hear hop hip hip ho" | perl -pe 'if ( 
s/(\w+)\s+\1\s+/double "${1}s" / ) { warn "found a \1\n" };'
found a <unprintable>
her here double "hears" hop hip hip ho

Interesting, in a way, is how with the '/g' you get:
 echo "her here hear hear hop hip hip ho" | perl -pe 'if ( 
s/(\w+)\s+\1\s+/double "${1}s" /g ) { warn "found a $1\n" };'
found a h
her here double "hears" hop double "hips" ho

I think what happens here is the capture parens matched the 'h' of the 
final 'ho' but there's no match for the \1 part. So no subst is done. 
However, $1 keeps the captured value (it did match a \w+ char). Not 
exactly what I expected, to be honest  - I'd've thought if the LHS RE 
failed, $1 wouldn't be 'updated' but would keep the last full match (i.e. 
'hip').

Wrong again ... 

a
----------------------
Andy Bach
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738; 
Cell: (608) 658-1890

Civilization advances by the number of important operations
which we can perform without thinking about them.
--Alfred North Whitehead
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to