in reply to Re: Where oh where should I initialize my vars?
in thread Where oh where should I initialize my vars?

    BTW, the split is more 'correctly' written as:
     my ($port, $sname, $rem) = split /\t/;

Uh... there is nothing wrong with split("\t"). To quote Camel 2 (note that this is different from our own docs for split):

  The /PATTERN/ argument may be replaced with an expression to specify patterns that vary at run-time. (To do run-time compilation only once, use /$variable/o.) As a special case, specifying a space " " will split on whitespace just as split with no argument does. Thus, split(" ") can be used to emulate awk's default behavior, whearas split(/ /) will give you as many null initial fields as there are leading spaces. (Other then this special case, if you supply a string instead of a regular expression, it'll be interpreted as a regular expression anyway.)  

My point, runrig, is TIMTOWTDI, and that sometimes the 'correct' way isn't the way the programmer wanted to do it. I, for one, like to use quotes around constants that I am splitting on, and slashes for things that look more like regular expressions. Yeah, I know they are the same, but quotes around strings looks better (to me) then slashes.

  • Comment on RE: Re: Where oh where should I initialize my vars?

Replies are listed 'Best First'.
RE: RE: Re: Where oh where should I initialize my vars?
by runrig (Abbot) on Oct 26, 2000 at 20:37 UTC
    Ok, point taken, but in my (perhaps pathetically weak) defense, if that is the official behavior, then it ought to be documented in perlfunc, not just in a book (even if it is The Camel book :-).

      I'd vote for using /\t/ as it is much more self-documenting. Seeing split "\t" makes me wonder what split '\t' does. Well, all of these are the same (they split on tab):

      split /\t/ split "\t" split '\t' split "\\t" split '\\t' split "\\\t"
      while all of these are the same (they split on backslash followed by "t"):
      split /\\t/ split "\\\\t" split '\\\\t' split '\\\t'

      Did that surprise you? I can certainly seeing that surprising a non-perfect Perl coder who takes over maintanence of your code.

      I think split $string would be more DWIM if it translated into split /\Q$string\E/, which it doesn't. So I think you should avoid using it as it makes your code harder to maintain. (Except for the special case of split " ", of course.)

              - tye (but my friends call me "Tye")