Re: Fancy sub arg handling: ability to expand error message?
On Sat, Mar 28, 2015 at 2:53 PM, Moritz Lenz mor...@faui2k3.org wrote: On 28.03.2015 12:27, Tom Browder wrote: I like the subroutine arg handling in Perl 6. Is there any simple way to attach a short error msg in place of or additive to the default for, say, a missing arg? You can always use multi subs, and use a catch-all candidate which produces the error message. multi thingy($x) { $x + 42 } multi thingy(|c) { die Must call thingy with exactly one argument } Multis? I guess my mind went elsewhere: sidhekin@purplehat[00:25:02]~$ cat tom.p6 sub foo($x = (warn foo called without argument; using default (42); 42) ) { $x + 42; } sub bar($x = (die bar called without argument; failing) ) { $x + 42; } say foo(-2); say foo(); say bar(-2); say bar(); sidhekin@purplehat[00:25:21]~$ perl6 tom.p6 40 foo called without argument; using default (42) in sub foo at tom.p6:1 84 40 bar called without argument; failing in sub bar at tom.p6:5 in block at tom.p6:12 sidhekin@purplehat[00:25:27]~$ Eirik
Re: Example module and its use
On Mar 28, 2015 6:23 AM, Paul Cochrane p...@liekut.de wrote: BTW: please don't use the shortcut 'v6;': AFAIU it's been deprecated in favour of 'use v6;' Hope this helps a bit. It does, thanks! BTW, I think my fumbling in learning Perl 6 is giving me some ideas for the Coookbook, at least for p6 newbies. I am keeping track of my questions and resullting simple cases to show exactly how to do something with working code--not too advanced but definitely helpful I think. In that vein, the synopses could do a better job of showing real code--maybe that's part of the cookbook I haven't seen yet. Cheers! -Tom
Re: Example module and its use
Hi Tom, use Bar :DEFAULT; but this does not: use Bar foo; So is S11 in error!! That might not necessarily be the case (however S11 certainly isn't clear about exactly how to import selected routines while `use`-ing a module). All subs/methods that are marked with `is export` are exported by default, thus there's actually no need to import anything, hence: use Bar; will ensure that the foo() routine is available within the scope of the `doit.pl` script. From what I can gather from S11, is that if one wants to override the default behaviour of importing into the current namespace everything which is exported by the module, then an EXPORT sub needs to be defined. AFAICT this is what the error: no EXPORT sub, but you provided positional argument in the 'use' statement is trying to tell you. Nevertheless, I've not been able to work out how to create an EXPORT sub which does the Right Thing(TM). Reading 'Perl6/Grammar.nqp' in the Rakudo sources shows that a sub by the name of `EXPORT` is expected which returns an `EnumMap`, which I suspect contains the names of the subs/methods to be exported. I'm fairly sure this is getting much too low-level for the current use-case. Also, sharing this information might simply be muddying the waters somewhat, so in order that you can get further with what you're trying to do, your example will work with the following code: == Bar.pm == module Bar; use v6; sub foo($a, $b, $c) is export {} # vim: expandtab shiftwidth=4 ft=perl6 = doit.pl == use v6; use lib .; use Bar; my @t = foo(1, 2, 3); # vim: expandtab shiftwidth=4 ft=perl6 BTW: please don't use the shortcut 'v6;': AFAIU it's been deprecated in favour of 'use v6;' Hope this helps a bit. Cheers, Paul
Re: Fancy sub arg handling: ability to expand error message?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi, On 28.03.2015 12:27, Tom Browder wrote: I like the subroutine arg handling in Perl 6. Is there any simple way to attach a short error msg in place of or additive to the default for, say, a missing arg? You can always use multi subs, and use a catch-all candidate which produces the error message. multi thingy($x) { $x + 42 } multi thingy(|c) { die Must call thingy with exactly one argument } Though IMHO that's usually not worth the trouble; you get the error message only for programming errors, not for user errors; and programmers should be able to understand the error message (or we need to improve the error messages, if that's not the case). Also it makes it harder for others to extend your API by providing additional multi candidates. Cheers, Moritz -BEGIN PGP SIGNATURE- Version: GnuPG v1 iEYEARECAAYFAlUWsnQACgkQT81LMIj/VkTbAwCfTsQumtjPKj1lxXZlnQ+U+0Xz uTQAn287aU2xU7m6iMFGWD+j2R+Bouy6 =LWsI -END PGP SIGNATURE-
Fancy sub arg handling: ability to expand error message?
I like the subroutine arg handling in Perl 6. Is there any simple way to attach a short error msg in place of or additive to the default for, say, a missing arg? Thanks. Best, -Tom