eval should throw an exception on compile error

2011-05-06 Thread Michael G Schwern
I was just playing around with eval, trying to figure out if you can define an
operator overload at runtime (seems you can't, good) and noticed this in the
spec... [1]

"Returns whatever $code returns, or fails."

How does one get the compile error from an eval?  What's the equivalent to $@?
 I don't see anything in the eval tests [2] that's checking for the error,
just that it returned false.

In addition, I'm surprised that eval doesn't throw an error when it fails.

try { eval $code; CATCH { ... } }

Using eval STRING in Perl 5 is pretty rare, and should hopefully be rarer in
Perl 6 than Perl 5.  While it can be used as a "does this compile" check, it's
overwhelmingly used with the expectation that the code will compile and so it
is an exception if it does not.  More pragmatically, this resolves the problem
of $@ by putting it into an exception rather than a side global.

It also resolves this trap:

if eval $code {
say "code returned true";
}
else {
# this will also happen if $code fails to compile
say "code returned false";
}

While Perl 6 probably has some sort of magic "I failed" value you can check
for separate from boolean false, folks aren't going to go through the extra
convolutions.


[1] http://perlcabal.org/syn/S29.html
[2] https://github.com/perl6/roast/blob/master/S29-context/eval.t

-- 
31. Not allowed to let sock puppets take responsibility for any of my
actions.
-- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army
   http://skippyslist.com/list/



Base conversion: not enough rope

2011-05-06 Thread Carl Mäsak
S02:3185-3280 does a nice job of explaining what can and cannot be
done with the radix syntax (i.e. :2<1010> etc). I'm left with two
questions, however:

* If :2<1010> is the way to way to "interpret" a string as a number in
base two, giving the number 10 -- what's the way to go in the other
direction?

* What's the way to interpret a number of some *parameterized* base $r
? The syntax :$r<123> is legal, but doesn't have anything to do with
base conversion.

Both of these concerns have come up in the context of trying to solve
practical problems in Perl 6. The radix conversion syntax is nice, but
it feels like something is missing in the above two areas. Maybe a
generalized builtin or two?

// Carl