nexting out of loops via sub calls

rjbs on 2005-08-18T16:36:35

12:19  * rjbs wants macros in Perl.
12:19  rjbs: Lisp-style? :)
12:19  of course
12:20  * rjbs wants a simple expression that can evaluate to "warn and next"
12:20  (loop-assert cond message)
12:21  which would become (unless (cond) (warn message) (next))
12:22  sub `loop_assert { my ($cond,$warn) = @_; unless($cond) { warn $warn; next } }
12:22  heh
12:24  er... wait...
12:24  that works!?  next will propagate up the call stack?
12:24  how did I not ever know this?
12:24  That's what the ` is for :)
12:24  no, but it seems to do so already
12:25  * rjbs tests more thoroughly.
12:25  Yeah can use loop control in a subroutine but you will get a warning too.
12:26  hell, I'll use "no warnings" for that.


So, yes. If you call a sub inside a loop, and the sub calls next (without the next call being inside an interior loop) the next will propagate up the call stack. There's a warning, but "no warnings 'exiting'" will quash it.

Wow!


Carp

n1vux on 2005-08-18T21:55:09

warn in that Sub will give the sub loop_assert line #

this macro should use carp (or croak) instead so the message gives the line# of the loop_assert call.

Assert is an overly strong name for a warning. I'd call it "sub loop_nag" ...

Test::More

Dom2 on 2005-08-19T08:47:59

This is how skip() is implemented inside Test::More (or possible Test::Builder). I'm sure Schwern wrote about it somewhere...

-Dom