Re: Exception Handling, DivideByZero
.WHAT gives you the actual type, whereas .^name gives you a string that is the name. In Perl6 types are things you can pass around; which is good because you can have more than one with the same name. sub bar (){ my class Foo { } } sub baz (){ my class Foo { } } my $a = bar().new(); bar() === $a.^name; # False (one is a Foo type object, the other a string) bar() === $a.WHAT; # True (both are the Foo type object) bar().^name === baz().^name; # True (but they are different types) bar().WHAT === baz().WHAT; # False (both are type objects, but they are different types) "".WHAT =:= Str; # True (both are the Str type object) Basically think about .WHAT as being the Perl6 equivalent of `typeof()`. On Mon, Oct 29, 2018 at 12:12 PM Joseph Brenner wrote: > > Brandon Allbery wrote: > > Two issues: > > > > (1) all standard exceptions are in or under the X:: namespace. > > > > (2) .WHAT doesn't show names with their namespaces, whereas .^name does. > > > > pyanfar Z$ 6 'my $r = 4/0; say $r; CATCH {default {say .^name}}' > > X::Numeric::DivideByZero > > Thanks. I didn't get that the behavior of WHAT and ^name were that different. > > > > > On Mon, Oct 29, 2018 at 1:04 PM Joseph Brenner wrote: > > > >> I was just looking into doing some finer-grained exception handling, > >> so I tried this: > >> > >>use v6; > >>try { > >>my $result = 4/0; > >>say "result: $result"; > >>CATCH { > >>#when DivideByZero { say "Oh, you know."; } > >>default { say .WHAT; .Str.say } # (DivideByZero) Attempt > >> to divide 4 by zero using div > >>} > >>} > >> > >> The first time through, The .WHAT tells me I've got > >> "DivideByZero", and so I added the line that's commented out > >> here, at which point I got the error: > >> > >>===SORRY!=== > >>Function 'DivideByZero' needs parens to avoid gobbling block (or > >> perhaps it's a class that's not declared or available in this scope?) > >> > >> Putting parens around (DivideByZero) doesn't help: > >> > >>Undeclared name: > >> DivideByZero used at line 12 > >> > >> My impression was this would just work from looking > >> at the examples using things like X::AdHoc here: > >> > >> https://docs.perl6.org/language/exceptions > >> > > > > > > -- > > brandon s allbery kf8nh > > allber...@gmail.com > >
Re: Exception Handling, DivideByZero
Brandon Allbery wrote: > Two issues: > > (1) all standard exceptions are in or under the X:: namespace. > > (2) .WHAT doesn't show names with their namespaces, whereas .^name does. > > pyanfar Z$ 6 'my $r = 4/0; say $r; CATCH {default {say .^name}}' > X::Numeric::DivideByZero Thanks. I didn't get that the behavior of WHAT and ^name were that different. > On Mon, Oct 29, 2018 at 1:04 PM Joseph Brenner wrote: > >> I was just looking into doing some finer-grained exception handling, >> so I tried this: >> >>use v6; >>try { >>my $result = 4/0; >>say "result: $result"; >>CATCH { >>#when DivideByZero { say "Oh, you know."; } >>default { say .WHAT; .Str.say } # (DivideByZero) Attempt >> to divide 4 by zero using div >>} >>} >> >> The first time through, The .WHAT tells me I've got >> "DivideByZero", and so I added the line that's commented out >> here, at which point I got the error: >> >>===SORRY!=== >>Function 'DivideByZero' needs parens to avoid gobbling block (or >> perhaps it's a class that's not declared or available in this scope?) >> >> Putting parens around (DivideByZero) doesn't help: >> >>Undeclared name: >> DivideByZero used at line 12 >> >> My impression was this would just work from looking >> at the examples using things like X::AdHoc here: >> >> https://docs.perl6.org/language/exceptions >> > > > -- > brandon s allbery kf8nh > allber...@gmail.com >
Re: Exception Handling, DivideByZero
Two issues: (1) all standard exceptions are in or under the X:: namespace. (2) .WHAT doesn't show names with their namespaces, whereas .^name does. pyanfar Z$ 6 'my $r = 4/0; say $r; CATCH {default {say .^name}}' X::Numeric::DivideByZero On Mon, Oct 29, 2018 at 1:04 PM Joseph Brenner wrote: > I was just looking into doing some finer-grained exception handling, > so I tried this: > >use v6; >try { >my $result = 4/0; >say "result: $result"; >CATCH { >#when DivideByZero { say "Oh, you know."; } >default { say .WHAT; .Str.say } # (DivideByZero) Attempt > to divide 4 by zero using div >} >} > > The first time through, The .WHAT tells me I've got > "DivideByZero", and so I added the line that's commented out > here, at which point I got the error: > >===SORRY!=== >Function 'DivideByZero' needs parens to avoid gobbling block (or > perhaps it's a class that's not declared or available in this scope?) > > Putting parens around (DivideByZero) doesn't help: > >Undeclared name: > DivideByZero used at line 12 > > My impression was this would just work from looking > at the examples using things like X::AdHoc here: > > https://docs.perl6.org/language/exceptions > -- brandon s allbery kf8nh allber...@gmail.com
Exception Handling, DivideByZero
I was just looking into doing some finer-grained exception handling, so I tried this: use v6; try { my $result = 4/0; say "result: $result"; CATCH { #when DivideByZero { say "Oh, you know."; } default { say .WHAT; .Str.say } # (DivideByZero) Attempt to divide 4 by zero using div } } The first time through, The .WHAT tells me I've got "DivideByZero", and so I added the line that's commented out here, at which point I got the error: ===SORRY!=== Function 'DivideByZero' needs parens to avoid gobbling block (or perhaps it's a class that's not declared or available in this scope?) Putting parens around (DivideByZero) doesn't help: Undeclared name: DivideByZero used at line 12 My impression was this would just work from looking at the examples using things like X::AdHoc here: https://docs.perl6.org/language/exceptions