What's /o going to win you here?

I guess I could ask the same question about the use of quotemeta() in Re: return a word next to the word you give. What's that going to win you there? It's clear there are no characters causing special effects in "color: ", are there? But you're trying to teach a newbie some good idiom, even though it doesn't make any sense performance wise or other wise in this particular case.

The reason I mentioned /o was that it can be a severe performance penalty if the regexp needs to be compiled again and again if it is not necessary. So I too am trying to teach something to a newbie.

As running it with -Dr will show, Perl is not going to recompile the regex anyway.

Has Perl suddenly become psychic? Does Perl now know when and when not to recompile regular expressions with embedded variables? Then why isn't /o deprecated? Just for those few cases where you want to be able to change the variable and still keep the initial regex?

And what's with the $lineno++? Did $. break?

Good point. That could have been $..

And since the regex doesn't contain any /'s, why m## and not just //?

Well, in my experience, I find myself trying match "/" much more often than I find myself trying to match "#". And I like to be consistent as much as I can, so I prefer doing m## almost all of the time over doing // most of the time.

Furthermore, I prefer a visual feedback about whether I'm trying to match or replace a string, so even with // I personally prefer to write m//.

Even though this slipped in without much thought from my end (as it sort of is engrained in my fingers nowadays), it also teaches the newbie that there is more than one way to do it.

Liz


In reply to Re: Re: return a word next to the word you give by liz
in thread return a word next to the word you give by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.