What's the reasoning behind that general recommendation? From your description, it appears that splitting on 'foo' and /foo/ (or even on $bar, if $bar eq 'foo') would be equivalent, so why prefer the pattern version?

Because the documentation says

split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/ split

In fact splitting on 'foo' and /foo/ are (currently) equivalent because perl converts the string to a regex:

perl -MO=Deparse -ne 'split "foo"' LINE: while (defined($_ = <ARGV>)) { split(/foo/, $_, 0); } -e syntax OK

Who knows? I doubt it, but the docs do not specify what should happen with generic strings other than ' ' and maybe one day special behaviour may be implemented for them along the lines of what is currently available for ' '.

(My current practice is to use 'foo' on the principle of never using regexes where a simple comparison is sufficient. On the surface, at least, it seems that splitting on /foo/ is the same manner of overkill as using if $bar =~ /^foo$/ instead of if $bar eq 'foo'.)

Your principle is based on an assumption that is simply not true for as we've seen before, your string is converted to a regex anyway. And OTOH matches like /foo/ are internally optimized to basically do an index. So no overkill. If you don't trust me, just trust B::Concise:

$ perl -MO=Concise -pe '$_=split "foo"' >1.txt -e syntax OK $ perl -MO=Concise -pe '$_=split /foo/' >2.txt -e syntax OK $ diff [12].txt 20c20 < 6 </> pushre(/"foo"/) s*/64 ->7 --- > 6 </> pushre(/"foo"/) s/64 ->7

Notice that the only difference is an asterisk, which means "Do something weird for this op".

I'm not saying that I don't agree with you on the $bar =~ /^foo$/ vs. $bar eq 'foo' issue. Because I do agree. (Also because they're not strictly equivalent:

$ perl -le 'print "bar\n" =~ /^bar$/ ? "ok" : "not ok"' ok

and people happen to use the former whereas they really want the latter.) Simply, that's not the same for split's first argument.


In reply to Re^3: Split ( ) is Giving Me a Splitting Headache by blazar
in thread Split ( ) is Giving Me a Splitting Headache by HughBris

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.