Considering how many people get messed up over split's first argument, I figured the extra clarification of using // over "" is warranted,

I note that you are concentrating on split which I didn't use above, (but admit I do use in the way you are indicating), rather than the $scalar1 =~ $scalar2 which I rarely use, but did use above. Mostly because I forgot to change it when c&ping the OPs code. No matter, back to the split issue.

I have made the mistake of typing split '|', $string; a couple of times. A long while since I think, but again, no matter. When I did this it was not because I forgot that the first parameter to split was a regex, but because I forgot (momentarially) that | was a meta character. And in that way, using /|/ is (again) identical. With identical result.

If there is any pretence here, it is the pretence that using // is somehow magical. It isn't. In all cases, using a scalar constant or scalar variable, or even a function returning a scalar, as the RHS side of the =~ is identical to using // or m"" or m//. The same code is generated, and the same results (good or erroneous) are obtained. There is, as far as I have discovered, exactly one situation when using // (or preferably m""!) yields different results to "", and it has nothing to do with split.

[0] Perl> sub test{ print "'$_[0]' ", ref \$_[ 0] };; [0] Perl> test( '|' );; '|' SCALAR [0] Perl> test( m/|/ );; Use of uninitialized value in pattern match (m//) at (eval 9) line 1 '1' SCALAR [0] Perl> test( /|/ );; Use of uninitialized value in pattern match (m//) at (eval 10) line 1 '1' SCALAR

Ie. When used in the absence of =~, a "regex" is implicitly applied to $_. But, this is not the case when it is used as an argument to split. This is another of those built-in behaviours that is impossible to emulate with a user defined function.

Personally, I think it is far better to get bitten by the difference a time or two early on, learn to appreciate the reality and integrate it into your thinking than to blithely ignore that reality and get bitten by it later. I think that 'protecting' newbies, by hiding Perl's warts is counter-productive and comes back to haunt them later.

Lessons learned by making your own mistakes, penetrate deeper and last far longer than those learnt by rote. And far, far longer than those imposed by edict. Or those never learnt at all, because you were protected from ever encountering them in your early days, when such mistakes are considered par for the course and expected.

Those unlearnt or unappreciated lessons are the ones that come back to bite you later, at points in time when they do much more harm. Perl has warts. Hiding them does not make them go away. Better to get to know them and appreciate them--the good and bad--than pretend that they do not exist.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^7: substring search in an array of arrays by BrowserUk
in thread substring search in an array of arrays by jgatrell42

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.