On Sat, Mar 19, 2005 at 11:03:40PM +1100, Andrew Savige wrote:
: --- Autrijus Tang wrote:
: > On Fri, Mar 18, 2005 at 09:35:59PM +1100, Andrew Savige wrote:
: > > What is the best way in Perl6/Pugs to slurp a file?
: > 
: > You use the slurp() primitive, implemented as r875. :)
: 
: Thanks. I tested these two forms and it's truly lovely:
: 
: my $x = slurp($fname) err die("slurp '$fname' failed");
: my @x = slurp($fname) err die("slurp '$fname' failed");
: 
: I have two more questions:
: 
: 1) Error Handling.
:    Is 'err' correct above? Or should it be 'or'? Or a try block?

Depends on what you want to catch.  What you have is probably most
correct, especially if we make undef smart about list context so that
the "err" actually sees an undef as the value of the list assignment.
In Perl 5, only the scalar assignment can produce an undef--there's no
way to pass an undef through a list assignment and get it to test as
undefined without explicitly testing the first returned value.  I hope
to fix that in Perl 6 so that your two example are both meaningful.
It not only involves undef knowing how to be () in list context, but
list assignment being smart enough that if you assign only an undefined
(), the array itself should probably be considered undefined, and in
any event the assignment should return the lazy undef for "err" to
evaluate.  (Presumably it also ends up in $! at some point, though
I'm not sure what the right point for that to happen is.)

An "or" would treat an empty file as false in either case.  Sometimes
that's what you want, I suppose.

A try {} would be necessary only if you expect slurp to throw an exception,
which it probably won't unless you've said "use fatal".

: 2) Documentation.
:    Where is the slurp built-in documented? I couldn't find it
:    in any of the Synopses (including Rod Adams' recent S29).
:    In desperation, I googled and found CPAN Perl6::Slurp.

The official place for documentation of built-ins is S29.  At the
rate I was going, I was going to get to S29 in two or three years,
so it's good that Rod is taking that on.  I don't recall offhand if
slurp was discussed much in p6l or whether it was mostly one of those
cabalistic things.

Larry

Reply via email to