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

Re^2: Evaluating user-entered captured groups during Perl substitution

by Polyglot (Pilgrim)
on Mar 16, 2020 at 09:03 UTC ( #11114329=note: print w/replies, xml ) Need Help??


in reply to Re: Evaluating user-entered captured groups during Perl substitution
in thread [SOLVED] Evaluating user-entered captured groups during Perl substitution

I appreciate seeing how you handled the eval for the entire regex expression, escaping two of the tokens to delay their evaluation (I presume). I don't remember having seen that kind of grammar before.

As for the application, my usage involves UTF8 text, and I just provided the example here to simulate the functionality I wish to have. In my own code, what you suggested doesn't seem to work. I'll have to play with it some more later, when I have time again, to see if I can do something more with the grammar you introduced.

The quotes seem to be fine at my end...at least, warnings doesn't indicate any special issue with them. Warnings just says there's an unrecognized escape in the line where I escaped the period and indicated a space with \s. As far as my eye can see, there should be no error there. Useless error messages is why I usually turn warnings off unless I specifically am watching the logs during trouble shooting. Otherwise, my logs just get fat without benefit. In the actual application, the text is coming in from a web form, and is not directly assigned in this manner.

Blessings,

~Polyglot~

Replies are listed 'Best First'.
Re^3: Evaluating user-entered captured groups during Perl substitution
by Marshall (Canon) on Mar 16, 2020 at 16:13 UTC
    The quotes seem to be fine at my end...at least, warnings doesn't indicate any special issue with them. Warnings just says there's an unrecognized escape in the line where I escaped the period and indicated a space with \s. As far as my eye can see, there should be no error there.
    Line under discussion:
    $query = "(St\.\s)(Mt\.\s)(?=Helens)";
    Unrecognized escape \s passed through at blah... line x

    Perl is saying that it figures you made a mistake with \s. It translated that into a single "s" character. It also translated \. into a literal single character of '.' but it knew about escaping a period and Perl didn't complain about that.
    Consider the following:

    #FROM INCOMING FORM INPUTS $query = '(St\.\s)(Mt\.\s)(?=Helens)'; #right way print "$query\n"; ##(St\.\s)(Mt\.\s)(?=Helens) $query = "(St\.\s)(Mt\.\s)(?=Helens)"; #your way print "",$query,"\n"; ##(St.s)(Mt.s)(?=Helens) print "$query\n"; ## same thing (St.s)(Mt.s)(?=Helens) $query = "(St\\.\\s)(Mt\\.\\s)(?=Helens)"; #ok, but confusing print "$query\n"; ## (St\.\s)(Mt\.\s)(?=Helens)
    Fixing the quoting has real consequences in terms of what $query winds up being!
    I always "use warnings;". I very rarely ignore a warning, with the possible exception of working with old code and the "deprecated syntax" warning. However, in all cases I do strive to understand what the heck is wrong that Perl is complaining about and then try to "make Perl happy". Sometimes with deprecated syntax, the error may be so pervasive that is not practical.

    I understand that in your production code, this string will come from elsewhere instead of an assignment statement like above. Be that as it may, I still strongly advise understanding what a Perl warning is telling you and fixing all test code so that it runs without any warnings. I have heard that Perl runs slightly slower with warnings enabled. I have never benchmarked that because this just hasn't been a significant factor in my work. I recommend leaving warnings enabled at all times.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2020-11-30 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?