Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Escape Quote-Like Operators

by MentalAbsence (Initiate)
on Sep 11, 2013 at 17:00 UTC ( [id://1053530]=perlquestion: print w/replies, xml ) Need Help??

MentalAbsence has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to cause letters that begin quote-like operators such as m, s, qq, qr, and friends to be treated as barewords as opposed to the beginning of a quoted or regular expression string? If you look at the following nonsensical example:

$_ = '12'; s/(\d+)/($1 > 10)?m:n/e;

The intent is to replace the number in $_ with either 'm' or 'n', but Perl complains:

"Search pattern not terminated..."

because it thinks that 'm:' is the start of a regular expression match operator. Of course, I can quote the 'm' and everything works fine, but unfortunately this is for golf and I'd like to be able to leave off the quotes if possible to save characters. Is there a way to escape the 'm' or cause it not to be treated as an operator at a cost of fewer than the two characters it takes to quote it? Thanks for your thoughts!

Replies are listed 'Best First'.
Re: Escape Quote-Like Operators
by BrowserUk (Patriarch) on Sep 11, 2013 at 17:10 UTC

    With /e, the replacement is interpreted as code, so do what you'd normally do and quote the letters:

    s/(\d+)/($1 > 10)?'m':'n'/e;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Escape Quote-Like Operators
by SuicideJunkie (Vicar) on Sep 11, 2013 at 18:04 UTC

    You could have $m there and 'escape' with one character if you were able to set some one-letter variable to 'm'; earlier.

    Something like s/(\d+)/chr 110-($_>10)/e; could work but its a bit longer.

    s/(\d+)/qw(n m)[$_>10]/e; is another path.

    I'm no golfer, but maybe those might give you some ideas.

Re: Escape Quote-Like Operators
by tobyink (Canon) on Sep 11, 2013 at 17:50 UTC

    Could you use capital letters?

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Escape Quote-Like Operators
by kennethk (Abbot) on Sep 11, 2013 at 18:37 UTC
    What's your actual use case? You can save a bunch of characters by not branching (in your nonsensical example):
    $_ = '12'; s/\d/n/g;s/n+/m/;
    Update: Or, if you pay 3 more (still saving 2 from your 'ideal') you can get identical algorithmic behavior:
    $_ = '12'; s/\d\d+/m/||s/\d/n/;
    LTS FTW

    Thanks to Anonymous Monk, realized my brain misread the comparator. However, the real use case is still relevant. As penance, optimization of the nonsensical example:

    $_ = '12'; s/\d+/$&>10?'m':n/e;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Wouldn't that replace '10' with 'm' by mistake?

        It would; parse fail.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Escape Quote-Like Operators (fat comma)
by tye (Sage) on Sep 12, 2013 at 01:37 UTC
    # s/(\d+)/($1 > 10)?'m':n/e; s/\d+/(m=>n)[$&<11]/e;

    - tye        

      nice, but only $& and not fat comma leads to a reduction ... :)
      DB<108> $_=12;s/\d+/(m=>n)[$&<11]/e;$_ => "m" DB<109> $_=12;s/\d+/($&>10)?'m':n/e;$_ => "m"
Re: Escape Quote-Like Operators
by Not_a_Number (Prior) on Sep 11, 2013 at 19:52 UTC

    No idea what your context is, but just to replace a number higher than 10 by 'm', lower or equal by 'n', what's wrong with this?:

    $_=$_>10?'m':n;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found