in reply to [Perl 6] List of length 2 or...

I've come around to the opinion that the desire to add extra adverbs or arguments should usually be taken as a design smell that you're trying to use the wrong tool for the job. In this case, I think Perl 5 programmers have a bit of unlearning to do, since split has become one of those all-you-have-is-a-hammers that gets used inappropriately on various everything-is-a-nails because of the absence of its figure/ground counterpart, comb. In Perl 6 you should use whichever one is more appropriate and readable. So if I didn't mind slurping the whole file, I'd probably just say:
my %hash = $fh.slurp.comb(/ ^^ (\T*) \t (\N*) /);
though perhaps a Perl 5 programmer would be more comfortable with the implicit iteration of a global regex:
my %hash = $fh.slurp ~~ m:g/ ^^ (\T*) \t (\N*) /;
But I think the explicit use of .comb is more readable.

If I didn't want to slurp the file, I'd use the convenient .lines method instead of the generic but cryptic prefix:<=> operator. I might write some kind of list comprehension:

my %hash = ($0,$1 if / ^^ (\T*) \t (\N*) / for $fh.lines);
or maybe just the same thing written as a normal for loop:
my %hash = do for $fh.lines { / ^^ (\T*) \t (\N*) / and $0,$1; }
Or if I wanted to emphasize the data flow, I might use an explicit gather/take construct:
my %hash = gather for $fh.lines { take / ^^ (\T*) \t (\N*) / || (); }
I guess there's still more than one way to do it in Perl 6. :)

Replies are listed 'Best First'.
Re^2: [Perl 6] List of length 2 or...
by Anonymous Monk on Oct 23, 2008 at 10:57 UTC
    Where might one read about .comb?