Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: a word of warning about /$pattern/

by vacant (Pilgrim)
on Dec 03, 2003 at 04:49 UTC ( [id://311827]=note: print w/replies, xml ) Need Help??


in reply to a word of warning about /$pattern/

I have once or twice been bewildered by this feature, too. In addition to perlop, there is Freidl, who says in the Owls Book: "If no regex is given, such as with m// ... Perl reuses the regular expression most recently used successfully with in the enclosing dynamic scope. This used to be useful for efficiency reasons, but is now obsolete with the advent of regex objects."

This kind of thing keeps me humble, because I know that somewhere there is someone who understands exactly why this was a good idea.

Replies are listed 'Best First'.
m/${\""}/ is for sed compatibility
by ambrus (Abbot) on Dec 03, 2003 at 13:07 UTC

    I think this "feature" is for sed compatibility. (By compatibility, I mean that the people using sed can get used to perl easier.)

    Maybe it would be better if this magic applied only if the regexp is empty before interpolation (and before overload qr transformation).

Re: Re: a word of warning about /$pattern/
by demerphq (Chancellor) on Dec 06, 2003 at 11:15 UTC

    This kind of thing keeps me humble, because I know that somewhere there is someone who understands exactly why this was a good idea

    Its still a good idea:

    if (/foo/ or /bop/ or /fnorble/) { s//baz/; }

    IOW, it means you can have several regexes try to match, and then without having to worry about which one did you can then replace the thing that was matched. I only recently came accross this feature, but sice I have its found its way into more code than I thought it would.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      Huh?
      s/foo|bop|fnorble/baz/;

      Makeshifts last the longest.

        Fair point. The example is not the best I've ever written.

        The point is that the empty regex behaviour is useful for allowing you to use m/.../ and then later s//.../ without having to match a second time. Maybe this is better

        if ($foo and /bar/) { print "Condition suceeds.\n"; if ($bloop and /bop/) { #... } # do other stuff, maybe even exit... s//baz/; # switch bar or bop with baz depending # on a bunch of things that have already happened. }

        Hopefully now you see the point? Sure we could probably rework the logic so this isnt needed, but I like the possibilities this opens up. :-)


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-18 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found