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.
:)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.