in reply to Re: sanity check
in thread sanity check

split m/ /, $line doesn't even nearly do the same thing as split(" ", $line).

That would be better as:

while ( <$input_fh> ) { my @tokens = split; ...

Replies are listed 'Best First'.
Re^3: sanity check
by ww (Archbishop) on Sep 21, 2011 at 02:38 UTC
    C:\>perl -e "use 5.012; my $foo='abc def';my @foo=split(/ /,$foo); for + (@foo) {say $_;} abc def C:\>perl -e "use 5.012; my $foo='abc def';my @foo=split m/ /,$foo; for + (@foo) {say $_;} abc def

    What did I miss?

      That split(/ /,$foo) and split m/ /,$foo are exactly the same.    I was pointing out the difference between split m/ /, $line and split(" ", $line)

      $ perl -le' use Data::Dumper; $Data::Dumper::Useqq = 1; my $foo = " abc\t\tdef\r\rghi\n"; print Dumper $_ for split / /, $foo; ' $VAR1 = ""; $VAR1 = ""; $VAR1 = "abc\t\tdef\r\rghi\n"; $ perl -le' use Data::Dumper; $Data::Dumper::Useqq = 1; my $foo = " abc\t\tdef\r\rghi\n"; print Dumper $_ for split " ", $foo; ' $VAR1 = "abc"; $VAR1 = "def"; $VAR1 = "ghi";
      What did I miss?

      If you use -E instead of -e, you can save yourself from typing use 5.012; :)

      C:\test>perl -E "my $foo='abc def';my @foo=split m/ /,$foo; for (@foo) + {say $_}" abc def

      A little more by omitting the $_:

      C:\test>perl -E "my $foo='abc def';my @foo=split m/ /,$foo; for (@foo) + {say}" abc def

      More still by using the modifier form of for:

      C:\test>perl -E "my $foo='abc def';my @foo=split m/ /,$foo; say for @f +oo" abc def

      A lot more by using the string constant directly in the only place it is used:

      C:\test>perl -E "my @foo=split m/ /,'abc def'; say for @foo" abc def

      And finally, do away with the variables completely:

      C:\test>perl -E "say for split m/ /,'abc def'" abc def

      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.