Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Difference between $1 and \1.

by 2teez (Priest)
on Dec 27, 2012 at 12:04 UTC ( #1010506=note: print w/ replies, xml ) Need Help??


in reply to Difference between $1 and \1.

Checking the perl documentation, one could get glorious insight.

So, doing
perldoc -v $1 Update I: OR check perlvar
from the CLI gives:

$<*digits*> ($1, $2, ...):
Contains the subpattern from the corresponding set of capturing parentheses from the last successful pattern match, not counting patterns matched in nested blocks that have been exited already. These variables are read-only and dynamically-scoped.

While \1 :
is Backreference to a specific capture group or buffer.
Please also check perlre.

It is also a good thing to note this Warning on \1 Instead of $1
UPDATE I: I was only checking this on Win OS. Thanks to LanX comments below.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re: Difference between $1 and \1.
Download Code
Re^2: Difference between $1 and \1. (perldoc -v)
by LanX (Canon) on Dec 27, 2012 at 12:20 UTC
    > perldoc -v $1

    before people start complaining, this doesn't work with 5.10!

           -v   Describes search for the item in detail (verbosely).

    And when using Linux, better care about hiding $1 from bash-expansion.

    perldoc -v '$1'

    Cheers Rolf

    ) dunno when it was introduced...

    UPDATE:

    CHANGES

    Up to 3.14_05, the switch -v was used to produce verbose messages of perldoc operation, which is now enabled by -D.

    ehm 3?

      Thanks all! From your replies I understand that $1 , $2 type of variables will not contain any value or contain only, old match values of first or second paranthesis from any previous regex matching , unless the pattern matching is complete and if that matching is successful. Opposed to this is \1 which stores first paranthesis value even before the entire pattern is matched , so that it has a defined value in the matching pattern itself and hence provides expected results. Hope my understanding is correct. Correct me if any concerns for you...!!!
        correct!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-07-28 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls