Re: Loop controls

2002-05-02 Thread Damien Neil

On Wednesday, May 1, 2002, at 02:27  PM, Aaron Sherman wrote:
>   unless my $fh = $x.open {
>   die "Cannot open $x: $!";
>   } else while $fh.getline -> $_ {
>   print;
>   } else {
>   die "No lines to read in $x";
>   }

I think you need a better example--given that the unless clause throws 
an exception, the "else while" can just as easily be written as a 
"while", no "else".  (Which I, personally, would find far clearer.)

 - Damien




Re: Loop controls

2002-05-02 Thread Aaron Sherman

On Wed, 2002-05-01 at 18:47, Damien Neil wrote:
> On Wednesday, May 1, 2002, at 02:27  PM, Aaron Sherman wrote:
> > unless my $fh = $x.open {
> > die "Cannot open $x: $!";
> > } else while $fh.getline -> $_ {
> > print;
> > } else {
> > die "No lines to read in $x";
> > }
> 
> I think you need a better example--given that the unless clause throws 
> an exception, the "else while" can just as easily be written as a 
> "while", no "else".  (Which I, personally, would find far clearer.)

No, I write this sort of thing all the time:

if (foo) {
die $!
} else {
stuff
}

It's clearer, IMHO, to the reader and maintainer of the code, exactly
what is expected. It's taste and style and visual aesthetics, but we
wouldn't have a postfix "if" if those things didn't matter in Perl.





eval {} or carp "blah: $@"

2002-05-02 Thread Jim Cromie


with p5, Ive often written

eval {} or carp "$@ blah";

it seems to work, and it reads nicer (to my eye) than

eval {}; if ($@) {}

but I surmise that it works cuz the return-value from the block is non-zero,
for successful eval, and 0 or undef when block dies, not cuz of magical 
treatment of $@.

I gather that ';' is unneeded in p6, and given that $! is the 
'exception'al topicalizer,
is this construct no longer reliant on the last value in the eval block ?

put another way, does the 'topicalizer' reflect the exit condition of 
the closure ?




Re: eval {} or carp "blah: $@"

2002-05-02 Thread Luke Palmer

On Thu, 2 May 2002, Jim Cromie wrote:

> 
> with p5, Ive often written
> 
> eval {} or carp "$@ blah";
> 
> it seems to work, and it reads nicer (to my eye) than
> 
> eval {}; if ($@) {}
> 
> but I surmise that it works cuz the return-value from the block is non-zero,
> for successful eval, and 0 or undef when block dies, not cuz of magical 
> treatment of $@.

The first one works because the return-value from the eval is undef if the 
block fails.  The second one is because $@ is true if it failed, and false 
if it succeeded.

> I gather that ';' is unneeded in p6, and given that $! is the 
> 'exception'al topicalizer,
> is this construct no longer reliant on the last value in the eval block ?

I would imagine that you would do it this way:

try {
...
CATCH { }
}

Only inside the CATCH does $! topicalize; I believe $_ becomes aliased to 
that.  Remember, C is Perl 6's eval{}.

> put another way, does the 'topicalizer' reflect the exit condition of 
> the closure ?

It represents the exception that was thrown, and whatsoever might be 
contained in that exception.


Or am I wrong?

Luke




Re: eval {} or carp "blah: $@"

2002-05-02 Thread Dave Mitchell

On Thu, May 02, 2002 at 02:33:42PM -0600, Jim Cromie wrote:
> 
> with p5, Ive often written
> 
> eval {} or carp "$@ blah";

You generally Don't Want To Do That.
If the eval succeeds, but the last statement in the eval happens to come
out as false, then it'll still carp:

$a = 0; eval { 1 < $a } or carp ""
$a = 0; eval { 1 / $a } or carp ""

will both carp

-- 
My get-up-and-go just got up and went.



Re: Loop controls

2002-05-02 Thread Damian Conway

> Um... I know it's scary, but I can actually imagine using this (or
> something like this) in development. I'll occasionally work on a section
> of code I'm not ready to integrate yet. It would be nice to be able to
> syntax check it without uncommenting and re-commenting the whole thing.
> :)

Yes. That's what I do: produce modules that are the superposition of:

all('absurd', 'useful', 'scary')

;-)

Damian



Re: eval {} or carp "blah: $@"

2002-05-02 Thread Peter Scott

At 02:33 PM 5/2/02 -0600, Jim Cromie wrote:
>eval {} or carp "$@ blah";
>
>it seems to work, and it reads nicer (to my eye) than
>
>eval {}; if ($@) {}

% perl -le 'eval { print "No exceptions here"; 0 } or warn "$@ blah"'
No exceptions here
  blah at -e line 1.


--
Peter Scott
Pacific Systems Design Technologies




Re: eval {} or carp "blah: $@"

2002-05-02 Thread Damian Conway

Jim Cromie wrote:

> with p5, Ive often written
> 
> eval {} or carp "$@ blah";
> 
> it seems to work,

modulo any block that returns false :-(


> and it reads nicer (to my eye) than
> 
> eval {}; if ($@) {}
> 
> but I surmise that it works cuz the return-value from the block is non-zero,
> for successful eval, and 0 or undef when block dies, not cuz of magical
> treatment of $@.

No. C return the last evaluated value in the block, unless an
exception is thrown, in which case it returns C. The problem with
the C approach is that there are situations in
which the block can evaluate successfully but still return C.

On failure, C sets C<$@>, so careful programmers test it separately.
Of course, you *can* do that in a single statment if you want:

eval {} or @! and carp "$@ blah";


> I gather that ';' is unneeded in p6,

Err...no.


>  and given that $! is the 'exception'al topicalizer,

Only inside an explicit CATCH block.


> is this construct no longer reliant on the last value in the eval block ?

In Perl 6 C becomes C and you'll still be able to write:

try { ... } // carp $!;

but that will trip up if the final value of the C block is C.

What you want is:

try {
...
CATCH { carp $! }
}


> put another way, does the 'topicalizer' reflect the exit condition of
> the closure ?

Only in a CATCH block.

Damian