Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: why such an error happened?

by lightoverhead (Pilgrim)
on Oct 20, 2008 at 01:16 UTC ( [id://718114]=note: print w/replies, xml ) Need Help??


in reply to Re: why such an error happened?
in thread why such an error happened?

Thank you Darren. But I am still confused. $1 is matched to "this" or "that", so they will not be upper cased. $2 matched to other words other than "this" or "that" and change them to upper cases, in this example, the "and" was changed to "AND". It seems to me that the "and" was assigned to $2, why you thought that $2 will always be undefined? Thanks.

Replies are listed 'Best First'.
Re^3: why such an error happened?
by Lawliet (Curate) on Oct 20, 2008 at 01:21 UTC

    Because of the | in the middle of the regex there will never be a time where both parenthesis are used. That is, there will only be one set of parenthesis total. Ergo, $2 will never be defined.

    I'm so adjective, I verb nouns!

    chomp; # nom nom nom

      Thank you for your posts. Now I begin to understand the warning messages. But I thought $1 or $2 will not be defined at the same time,i.e. for any moment, only either of them is defined, the other is undefined. We can say sometime $1 is undefined, sometime $2 is undefined; but I still don't think that $2 will never be defined, since in this example, "\U$2" indeed change "and" to "AND". Is my understanding right? Thanks.
        $1 and $2 are statically assigned when the regex is being compiled. At that time the regex-engine does not yet know which --if any-- of the parentheses will match. So $1 will refer to what is between the first set of parentheses (counting from the left); $2 refers to the second set (counting from the left); etc. ...

        In an (nested) alternation pattern (|) only one of these will ever match and that causes the warning.

        In your case you can safely ignore it.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Uhh, what?

        I think you are confusing what $1 and $2 are. $1 represents what was matched in the first set of matched parenthesis. $2 represents what was matched in the second set of matched parenthesis. Seeing as there will never be two matched sets of parenthesis in your regex, $2 will never be defined.

        I'm so adjective, I verb nouns!

        chomp; # nom nom nom

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found