Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: $/ is playing havoc with my script.

by chester (Hermit)
on Sep 26, 2005 at 13:29 UTC ( #495104=note: print w/replies, xml ) Need Help??

in reply to Re: $/ is playing havoc with my script.
in thread $/ is playing havoc with my script.

I believe TheDamian recommends in Perl Best Practices to use msx on every regex. Of course I don't know if that's what is going on here, but I find it to be good advice; it doesn't hurt anything to have them on there, and if you add a "." to your regex someday it'll work by default (assuming that "works" means ". matches a newline", which it does to me intuitively). I've been bitten by "." not matching a newline before.

According to Apocalypse 5, in Perl6 "." will always match newline, and ^ and $ will always have a single meaning, so we thankfully won't have this problem.

  • Comment on Re^2: $/ is playing havoc with my script.

Replies are listed 'Best First'.
Re^3: $/ is playing havoc with my script.
by sauoq (Abbot) on Sep 26, 2005 at 14:29 UTC

    Well, if that's what he actually recommends, I'll politely ignore his recommendation. Most of the time you don't need /s or /m. They're useless if you aren't working with multi-line strings. If you are working with multi-line strings, then you may very well want either the behavior you get with them or without them. In that case, you really should know exactly what you are trying to do and use them or not as is fitting, not because you read a book that said use them by default. IMHO, of course.

    "My two cents aren't worth a dime.";
      I think it's not so much a matter of "not knowing what you're doing", as wanting the regex engine to be consistent in that one symbol always means one thing.

      If you use /ms all the time, then \A always means "beginning of the string", ^ means "beginning of a line", \z means "end of the string", $ means "end of a line, . means "any character", and [^\n] means "any character but a newline".

      You lose no functionality, and you gain the benefit of consistency (and possibly intuitiveness, depending on your intuition). You also partially eliminate what the Apocalypse calls the "end-weight problem", which I assume means that you have no idea what your regex is doing until you scan all the way to the end to see the modifiers. Whether this is really a problem is debatable, and you still have i and g (etc.) to grapple with in any case.

      Of course you also lose a bit of conciseness, which is a minus. It's just a question of style. TMTOWTDI

        You lose no functionality, and you gain the benefit of consistency (and possibly intuitiveness, depending on your intuition).

        Consistency isn't exactly a hallmark of Perl. (Take context as an example. Or the wide variety of behavior you get from builtins. Or the whole philosophy of TMTOWTDI.) The tradeoff it often makes is consistency for flexibility. Perl's flexibility makes it great! But, it pushes the burden of consistency off to us, the developers.

        And, it's partially in the interest of consistency that I would reject the advice to use /msx all the time because I expect that the great volume of Perl code written so far does not follow that advice. I'm also fairly certain that I'm not the only one who has seen [^\n] in someone's regex and suggested that using a lone dot would be more Perlish.

        The more practical reason, however, is that when I see /m or /s or \A or \Z these are strong visual cues that immediately indicate the data a given regex is dealing with probably has embedded newlines. I'd really rather not give that up.

        "My two cents aren't worth a dime.";
Re^3: $/ is playing havoc with my script.
by Tanktalus (Canon) on Sep 26, 2005 at 19:09 UTC

    Just be careful you know why you're doing something, otherwise you're just Cargo cult programming. Which, I've noticed, can be quite frowned upon around here. Although I've never seen anyone accuse followers of TheDamian or merlyn's styles called for Cargo culting before.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://495104]
choroba mornings back
[Corion]: Hi choroba!
[Corion]: And Discipulus!
Corion arrived back from London. Always nice to meet people again, even if it's just once per year.
[Corion]: Also, dk contributed some cool stuff to App::ShaderToy, like menu settings and real fullscreen, eliminating many of my #TO-DO comments ;)
[Corion]: I hope you spent the four days well without me online ;-D
[Discipulus]: more or less Corion: the monastery was so calm, maybe too much! i feared you'v lost in VR
[Discipulus]: choroba can i ask why you used *DATA{IO}?

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2016-12-06 08:23 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (101 votes). Check out past polls.