in reply to sanity check

That's insane! ;-)

Compare your script with this refactored version.

#!perl use strict; use warnings; use autodie qw( open close ); open my $input_fh, '<:encoding(UTF-8)', shift @ARGV; open my $output_fh, '>:encoding(UTF-8)', 'Output.txt'; while (my $line = <$input_fh>) { chomp $line; my @tokens = split ' ', $line; if ($tokens[0] eq 'object-group') { print {$output_fh} "set shared address-group $tokens[2]\n"; } } close $input_fh; close $output_fh; exit 0;

UPDATE: Changed « my @tokens = split m/ /, $line; » to « my @tokens = split ' ', $line; ».

Replies are listed 'Best First'.
Re^2: sanity check
by jwkrahn (Abbot) on Sep 21, 2011 at 00:21 UTC

    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; ...
      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.