in reply to Use of uninitialized value message?

s{ ( [a-zA-Z]+ (?: ' [a-zA-Z]{1,2} )? ) }{ my $x = $1; $x =~ s/'s\z//; exists $dict{ lc( $x ) } ? $1 : "<@>$1"; }exg;

Okay, I understand that you have a basic s{}{} regex structure here, which means you're replacing something in the first {} group with the evaluated result of the second {} group. That's what the e modifier does. The g modifier means global replace, which means do each occurrence one by one. And the x modifier tells us to ignore spaces or comments within the regex.

The first part matches some word followed by (I'm not sure what (?: ' does and then I am not sure what )? does either. Does it mean match this group once or not at all?

Then you have another closing parentheses which is the end of the capture group. The second half of the regex s{}{} group contains Perl code that will be evaled when a match is found.

It looks like you have a regex within a regex. The embedded regex is also a replacement. It seems like you're removing something from the end of a word. I am not sure what the 's does. Or is this a literal letter "s" at the end of a word that makes the word possessive? That could be it. So, you are looking for a word in a dictionary.

The regex within the regex seems to make the problem. And even though you're not capturing anything with the embedded regex, the $1 still gets reset for some reason. Honestly, I don't understand the purpose of this whole code. I only understand that you're trying to replace some words with something else and you do a dictionary lookup. If the word is in the dictionary without the 's suffix (?), then you perform a replacement with that word, otherwise you put a <@> suffix in front of it, which I don't understand why. But since I don't understand what the input pattern might look like and what the desired output should look like, I cannot help. I just wrote this to show how I am trying to analyze your code, but I'm nowhere near this level of expert yet, so I cannot give you a solution. But it looks very complex code!

Replies are listed 'Best First'.
Re^2: Use of uninitialized value message?
by soonix (Chancellor) on Aug 26, 2024 at 08:42 UTC
    YAPE::Regex::Explain to the rescue.

    The outer "(?x-ims:" is what qr makes from the /x flag, and the question mark in the ")? end of grouping" is reflected in the "optional" at the comment at the start ot said group

    perl -wMstrict -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain-> +new(qr/( [a-zA-Z]+ (?: ' [a-zA-Z]{1,2} )? )/x)->explain;" The regular expression: (?x-ims:( [a-zA-Z]+ (?: ' [a-zA-Z]{1,2} )? )) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?x-ims: group, but do not capture (disregarding whitespace and comments) (case-sensitive) (with ^ and $ matching normally) (with . not matching \n): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [a-zA-Z]+ any character of: 'a' to 'z', 'A' to 'Z' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- (?: group, but do not capture (optional (matching the most amount possible)): ---------------------------------------------------------------------- ' '\'' ---------------------------------------------------------------------- [a-zA-Z]{1,2} any character of: 'a' to 'z', 'A' to 'Z' (between 1 and 2 times (matching the most amount possible)) ---------------------------------------------------------------------- )? end of grouping ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------