OT: catch the bash error?
Hi All, I wanted to do a mass rename of "Apple.*" to "Mac.*" with bash and I could not figure out the error. I eventually did find it and I have to blame Perl for it! Chuckle. for F in Apple*; do $G=$(echo $F | sed -e 's/^Apple/Mac/'); mv $F $G; echo "$F --> $G"; done Did you catch the error? I stared and stared at it for about ten minutes. -T It is "do G=" not "do $G="
Re: Catching exceptions in expressions
Yes; but then I think that something like infix: probably just ends up as a macro somehow. I just didn't know the state of macros in Perl 6 well enough to be able to head down that route. :) Pm On Fri, Aug 03, 2018 at 10:32:41PM +0200, Elizabeth Mattijsen wrote: > Sometimes I wish we could use Thunk as a type: > > sub infix:(Thunk:D $block, $otherwise) { } > > which would then allow you to do: > > my $sixdivzero = divide(6,0) rescue -1; # note absence of curlies > > > > One can wish, can’t one? > > > Liz > > > On 3 Aug 2018, at 22:18, Patrick R. Michaud wrote: > > > > Maybe something like...? > > > > $ cat t.p6 > > > > sub infix:(Callable $block, $otherwise) { > > CATCH { return $otherwise; } > > $block(); > > } > > > > sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } > > > > my $sixdivzero = { divide(6,0) } rescue -1; > > say "6/0 = ", $sixdivzero; > > > > my $sixdivtwo = { divide(6,2) } rescue -1; > > say "6/2 = ", $sixdivtwo; > > > > > > $ perl6 t.p6 > > 6/0 = -1 > > 6/2 = 3 > > > > > > Or if you prefer a prefix form, just declare "rescue" as a normal sub and > > then do: > > > > rescue { divide(6,2) }, -1; > > > > Pm > > > > On Fri, Aug 03, 2018 at 08:34:44PM +0100, Simon Proctor wrote: > >> Hi Sean. I hope my second answer in stackoverflow gets closer to what you > >> want. > >> > >> I am still trying to think of a more idiomatic way of handling to > >> situation. > >> > >> > >> > >> On Fri, 3 Aug 2018, 19:29 Sean McAfee, wrote: > >> > >>> I posted about this subject on Stack Overflow yesterday[1], but I chose a > >>> poor example of something that raises an exception (dividing by zero, > >>> which > >>> apparently doesn't necessarily do so) on which the answers have mostly > >>> focused. > >>> > >>> I was looking for a way to evaluate an expression, and if the expression > >>> threw an exception, for a default value to be provided instead. For > >>> example, in Ruby: > >>> > >>>quotient = begin; a / b; rescue; -1; end > >>> > >>> Or in Lisp: > >>> > >>>(setq quotient (condition-case nil (/ a b) (error -1))) > >>> > >>> Not having written much exception-related code in Perl 6, I hoped that > >>> this might work: > >>> > >>>sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } > >>>my $quotient = do { divide($a, $b); CATCH { default { -1 } } }; > >>> > >>> It doesn't, though. As far as I can tell, the value to which a CATCH > >>> block evaluates is ignored; the only useful things one can do in such a > >>> block are things with side effects. Long story short, I eventually came > >>> up > >>> with this: > >>> > >>>my $quotient = do { my $q; { $q = divide($a, $b); CATCH { default { $q > >>> = -1 } } }; $q }; > >>> > >>> That's far more verbose than I've come to expect from Perl 6. Is there > >>> some more concise way of expressing this logic? > >>> > >>> The doc page on exceptions mentions try, eg: > >>> > >>>my $quotient = try { divide($a, $b) } // -1; > >>> > >>> That works in this specific case, but it seems insufficient in general. > >>> The function might validly return an undefined value, and this > >>> construction > >>> can't distinguish between that and an exception. Also, it wouldn't let me > >>> distinguish among various exception cases. I'd have to do something like: > >>> > >>>class EA is Exception { } > >>>class EB is Exception { } > >>>sub divide($a, $b) { (EA, EB).pick.new.throw if $b == 0; $a / $b } > >>> > >>>my $quotient = do { my $q; { $q = divide($a, $b); CATCH { when EA { $q > >>> = -1 }; when EB { $q = -2 } } }; $q }; > >>> > >>> > >>> [1] > >>> https://stackoverflow.com/questions/51644197/returning-values-from-exception-handlers-in-perl-6/51670573 > >>> > >> -- > >> Simon Proctor > >> Cognoscite aliquid novum cotidie
Re: need regex help
On 08/03/2018 11:36 AM, Parrot Raiser wrote: If I've interpreted this https://docs.perl6.org/language/regexes#Enumerated_character_classes_and_ranges correctly, ^ is "start of string" +alnum means "in the alphanumeric set" -alpha means "not in the purely alphabetic set" i.e. <+alnum -alpha> means "alphanumeric but not a letter", i.e 0-9_ + is "one or more of the preceding set" $ is "end of string" Thank you!
Re: need regex help
On 08/03/2018 11:52 AM, Patrick R. Michaud wrote: The + essentially indicates that this is a character-class match. It's to distinguish things from <.alpha>, , , <-alpha>, and (among others). Thank you!
Re: need regex help
On 08/03/2018 11:48 AM, Timo Paulssen wrote: The + is required, perhaps because the first character after the opening < is supposed to determine exactly what thing it is? Not sure about that. The + and - is a bit like "start at nothing, add all alnums, then subtract all alphas". The + after the < > is just to match it any number of times, but at least once, and the $ at the end, together with the ^ at the start, ensures that every character in the string has to match, not just any character. Hope that makes sense - Timo Thank you!
Re: parsing in different modes
Hi all, My attempt at a solution below does not work. In larger examples the decr gets called before the actions within Sum are processed. Maybe my hunch that this would cause time order problems was correct. I need to do some more researching. I'll post my findings. best wishes, Theo van den Heuvel Theo van den Heuvel schreef op 2018-08-02 14:58: Hi Laurent, Here I set my example up along the lines of your second suggestion. grammar Sum { token TOP { ^ $ } rule Sum { + % } rule Expr { | { self.incr } '[' ~ ']' { self.decr } } token op { <[-+]> } token num { \d+ } token flag { } method incr { self.actions.incr } method decr { self.actions.decr } } class Act { has Int $.nest; method num ($/) { say ">>> $/ at nesting level $!nest" } method incr { $!nest++ } method decr { $!nest-- } } This works nicely and fulfils my requirements. Thanks, Theo Laurent Rosenfeld schreef op 2018-08-01 23:00: Hi Theo, You probably cannot use a grammar rule to change a dynamic variable (unless you include some action code into the rule), I agree, but I think you can use an action method attached to a rule to do it. I have actually done it recently in a real $work grammar that I intend to present at The Perl Conference in Glasgow in two weeks from now. Your use case and mine are admittedly very different, but it seems to me that they are syntactically very similar. As an alternative, you could use an action object with an attribute (a mutable attribute, _is rw_) for the mode. Basically, you instantiate an object of the actions class (with the desired attribute set to the proper initial mode at object creation), and then pass the object instead of the class to the grammar when calling the _parse_ method. It should be easy then to modify the attribute as needed. This is perhaps slightly cleaner than using a dynamic variable. I hope this helps. Laurent. ...
Re: Catching exceptions in expressions
Sometimes I wish we could use Thunk as a type: sub infix:(Thunk:D $block, $otherwise) { } which would then allow you to do: my $sixdivzero = divide(6,0) rescue -1; # note absence of curlies One can wish, can’t one? Liz > On 3 Aug 2018, at 22:18, Patrick R. Michaud wrote: > > Maybe something like...? > > $ cat t.p6 > > sub infix:(Callable $block, $otherwise) { > CATCH { return $otherwise; } > $block(); > } > > sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } > > my $sixdivzero = { divide(6,0) } rescue -1; > say "6/0 = ", $sixdivzero; > > my $sixdivtwo = { divide(6,2) } rescue -1; > say "6/2 = ", $sixdivtwo; > > > $ perl6 t.p6 > 6/0 = -1 > 6/2 = 3 > > > Or if you prefer a prefix form, just declare "rescue" as a normal sub and > then do: > > rescue { divide(6,2) }, -1; > > Pm > > On Fri, Aug 03, 2018 at 08:34:44PM +0100, Simon Proctor wrote: >> Hi Sean. I hope my second answer in stackoverflow gets closer to what you >> want. >> >> I am still trying to think of a more idiomatic way of handling to situation. >> >> >> >> On Fri, 3 Aug 2018, 19:29 Sean McAfee, wrote: >> >>> I posted about this subject on Stack Overflow yesterday[1], but I chose a >>> poor example of something that raises an exception (dividing by zero, which >>> apparently doesn't necessarily do so) on which the answers have mostly >>> focused. >>> >>> I was looking for a way to evaluate an expression, and if the expression >>> threw an exception, for a default value to be provided instead. For >>> example, in Ruby: >>> >>>quotient = begin; a / b; rescue; -1; end >>> >>> Or in Lisp: >>> >>>(setq quotient (condition-case nil (/ a b) (error -1))) >>> >>> Not having written much exception-related code in Perl 6, I hoped that >>> this might work: >>> >>>sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } >>>my $quotient = do { divide($a, $b); CATCH { default { -1 } } }; >>> >>> It doesn't, though. As far as I can tell, the value to which a CATCH >>> block evaluates is ignored; the only useful things one can do in such a >>> block are things with side effects. Long story short, I eventually came up >>> with this: >>> >>>my $quotient = do { my $q; { $q = divide($a, $b); CATCH { default { $q >>> = -1 } } }; $q }; >>> >>> That's far more verbose than I've come to expect from Perl 6. Is there >>> some more concise way of expressing this logic? >>> >>> The doc page on exceptions mentions try, eg: >>> >>>my $quotient = try { divide($a, $b) } // -1; >>> >>> That works in this specific case, but it seems insufficient in general. >>> The function might validly return an undefined value, and this construction >>> can't distinguish between that and an exception. Also, it wouldn't let me >>> distinguish among various exception cases. I'd have to do something like: >>> >>>class EA is Exception { } >>>class EB is Exception { } >>>sub divide($a, $b) { (EA, EB).pick.new.throw if $b == 0; $a / $b } >>> >>>my $quotient = do { my $q; { $q = divide($a, $b); CATCH { when EA { $q >>> = -1 }; when EB { $q = -2 } } }; $q }; >>> >>> >>> [1] >>> https://stackoverflow.com/questions/51644197/returning-values-from-exception-handlers-in-perl-6/51670573 >>> >> -- >> Simon Proctor >> Cognoscite aliquid novum cotidie
Re: Catching exceptions in expressions
Maybe something like...? $ cat t.p6 sub infix:(Callable $block, $otherwise) { CATCH { return $otherwise; } $block(); } sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } my $sixdivzero = { divide(6,0) } rescue -1; say "6/0 = ", $sixdivzero; my $sixdivtwo = { divide(6,2) } rescue -1; say "6/2 = ", $sixdivtwo; $ perl6 t.p6 6/0 = -1 6/2 = 3 Or if you prefer a prefix form, just declare "rescue" as a normal sub and then do: rescue { divide(6,2) }, -1; Pm On Fri, Aug 03, 2018 at 08:34:44PM +0100, Simon Proctor wrote: > Hi Sean. I hope my second answer in stackoverflow gets closer to what you > want. > > I am still trying to think of a more idiomatic way of handling to situation. > > > > On Fri, 3 Aug 2018, 19:29 Sean McAfee, wrote: > > > I posted about this subject on Stack Overflow yesterday[1], but I chose a > > poor example of something that raises an exception (dividing by zero, which > > apparently doesn't necessarily do so) on which the answers have mostly > > focused. > > > > I was looking for a way to evaluate an expression, and if the expression > > threw an exception, for a default value to be provided instead. For > > example, in Ruby: > > > > quotient = begin; a / b; rescue; -1; end > > > > Or in Lisp: > > > > (setq quotient (condition-case nil (/ a b) (error -1))) > > > > Not having written much exception-related code in Perl 6, I hoped that > > this might work: > > > > sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } > > my $quotient = do { divide($a, $b); CATCH { default { -1 } } }; > > > > It doesn't, though. As far as I can tell, the value to which a CATCH > > block evaluates is ignored; the only useful things one can do in such a > > block are things with side effects. Long story short, I eventually came up > > with this: > > > > my $quotient = do { my $q; { $q = divide($a, $b); CATCH { default { $q > > = -1 } } }; $q }; > > > > That's far more verbose than I've come to expect from Perl 6. Is there > > some more concise way of expressing this logic? > > > > The doc page on exceptions mentions try, eg: > > > > my $quotient = try { divide($a, $b) } // -1; > > > > That works in this specific case, but it seems insufficient in general. > > The function might validly return an undefined value, and this construction > > can't distinguish between that and an exception. Also, it wouldn't let me > > distinguish among various exception cases. I'd have to do something like: > > > > class EA is Exception { } > > class EB is Exception { } > > sub divide($a, $b) { (EA, EB).pick.new.throw if $b == 0; $a / $b } > > > > my $quotient = do { my $q; { $q = divide($a, $b); CATCH { when EA { $q > > = -1 }; when EB { $q = -2 } } }; $q }; > > > > > > [1] > > https://stackoverflow.com/questions/51644197/returning-values-from-exception-handlers-in-perl-6/51670573 > > > -- > Simon Proctor > Cognoscite aliquid novum cotidie
Re: Catching exceptions in expressions
Hi Sean. I hope my second answer in stackoverflow gets closer to what you want. I am still trying to think of a more idiomatic way of handling to situation. On Fri, 3 Aug 2018, 19:29 Sean McAfee, wrote: > I posted about this subject on Stack Overflow yesterday[1], but I chose a > poor example of something that raises an exception (dividing by zero, which > apparently doesn't necessarily do so) on which the answers have mostly > focused. > > I was looking for a way to evaluate an expression, and if the expression > threw an exception, for a default value to be provided instead. For > example, in Ruby: > > quotient = begin; a / b; rescue; -1; end > > Or in Lisp: > > (setq quotient (condition-case nil (/ a b) (error -1))) > > Not having written much exception-related code in Perl 6, I hoped that > this might work: > > sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } > my $quotient = do { divide($a, $b); CATCH { default { -1 } } }; > > It doesn't, though. As far as I can tell, the value to which a CATCH > block evaluates is ignored; the only useful things one can do in such a > block are things with side effects. Long story short, I eventually came up > with this: > > my $quotient = do { my $q; { $q = divide($a, $b); CATCH { default { $q > = -1 } } }; $q }; > > That's far more verbose than I've come to expect from Perl 6. Is there > some more concise way of expressing this logic? > > The doc page on exceptions mentions try, eg: > > my $quotient = try { divide($a, $b) } // -1; > > That works in this specific case, but it seems insufficient in general. > The function might validly return an undefined value, and this construction > can't distinguish between that and an exception. Also, it wouldn't let me > distinguish among various exception cases. I'd have to do something like: > > class EA is Exception { } > class EB is Exception { } > sub divide($a, $b) { (EA, EB).pick.new.throw if $b == 0; $a / $b } > > my $quotient = do { my $q; { $q = divide($a, $b); CATCH { when EA { $q > = -1 }; when EB { $q = -2 } } }; $q }; > > > [1] > https://stackoverflow.com/questions/51644197/returning-values-from-exception-handlers-in-perl-6/51670573 > -- Simon Proctor Cognoscite aliquid novum cotidie
Re: need regex help
The + essentially indicates that this is a character-class match. It's to distinguish things from <.alpha>, , , <-alpha>, and (among others). Pm On Fri, Aug 03, 2018 at 08:48:24PM +0200, Timo Paulssen wrote: > The + is required, perhaps because the first character after the opening > < is supposed to determine exactly what thing it is? Not sure about > that. The + and - is a bit like "start at nothing, add all alnums, then > subtract all alphas". The + after the < > is just to match it any number > of times, but at least once, and the $ at the end, together with the ^ > at the start, ensures that every character in the string has to match, > not just any character. > > Hope that makes sense > - Timo > > > On 03/08/18 20:04, ToddAndMargo wrote: > > On 08/02/2018 05:18 AM, Timo Paulssen wrote: > >> Is this what you want? > >> > >> perl6 -e 'say "12345" ~~ /^<+alnum -alpha>+$/' > >> 「12345」 > >> > >> perl6 -e 'say "123a45" ~~ /^<+alnum -alpha>+$/' > >> Nil > >> > >> HTH > >> - Timo > >> > > > > What does the following do? > > > > +alnum (why does it need the "+"?) > > -alpha (I presume "-" means negate?) > > +$ > > > > Many thanks, > > -T
Re: need regex help
The + is required, perhaps because the first character after the opening < is supposed to determine exactly what thing it is? Not sure about that. The + and - is a bit like "start at nothing, add all alnums, then subtract all alphas". The + after the < > is just to match it any number of times, but at least once, and the $ at the end, together with the ^ at the start, ensures that every character in the string has to match, not just any character. Hope that makes sense - Timo On 03/08/18 20:04, ToddAndMargo wrote: > On 08/02/2018 05:18 AM, Timo Paulssen wrote: >> Is this what you want? >> >> perl6 -e 'say "12345" ~~ /^<+alnum -alpha>+$/' >> 「12345」 >> >> perl6 -e 'say "123a45" ~~ /^<+alnum -alpha>+$/' >> Nil >> >> HTH >> - Timo >> > > What does the following do? > > +alnum (why does it need the "+"?) > -alpha (I presume "-" means negate?) > +$ > > Many thanks, > -T
Re: need regex help
That document also says that _ is considered a letter (that is, is matched by : https://docs.perl6.org/language/regexes#Predefined_Character_Classes), so that's the same thing as . I observed that earlier as well. On Fri, Aug 3, 2018 at 2:37 PM Parrot Raiser <1parr...@gmail.com> wrote: > If I've interpreted this > > https://docs.perl6.org/language/regexes#Enumerated_character_classes_and_ranges > correctly, > > ^ is "start of string" > +alnum means "in the alphanumeric set" > -alpha means "not in the purely alphabetic set" > i.e. <+alnum -alpha> means "alphanumeric but not a letter", i.e 0-9_ > + is "one or more of the preceding set" > $ is "end of string" > > On 8/3/18, ToddAndMargo wrote: > > On 08/02/2018 05:18 AM, Timo Paulssen wrote: > >> Is this what you want? > >> > >> perl6 -e 'say "12345" ~~ /^<+alnum -alpha>+$/' > >> 「12345」 > >> > >> perl6 -e 'say "123a45" ~~ /^<+alnum -alpha>+$/' > >> Nil > >> > >> HTH > >>- Timo > >> > > > > What does the following do? > > > > +alnum (why does it need the "+"?) > > -alpha (I presume "-" means negate?) > > +$ > > > > Many thanks, > > -T > > > -- brandon s allbery kf8nh allber...@gmail.com
Re: need regex help
If I've interpreted this https://docs.perl6.org/language/regexes#Enumerated_character_classes_and_ranges correctly, ^ is "start of string" +alnum means "in the alphanumeric set" -alpha means "not in the purely alphabetic set" i.e. <+alnum -alpha> means "alphanumeric but not a letter", i.e 0-9_ + is "one or more of the preceding set" $ is "end of string" On 8/3/18, ToddAndMargo wrote: > On 08/02/2018 05:18 AM, Timo Paulssen wrote: >> Is this what you want? >> >> perl6 -e 'say "12345" ~~ /^<+alnum -alpha>+$/' >> 「12345」 >> >> perl6 -e 'say "123a45" ~~ /^<+alnum -alpha>+$/' >> Nil >> >> HTH >> - Timo >> > > What does the following do? > > +alnum (why does it need the "+"?) > -alpha (I presume "-" means negate?) > +$ > > Many thanks, > -T >
Catching exceptions in expressions
I posted about this subject on Stack Overflow yesterday[1], but I chose a poor example of something that raises an exception (dividing by zero, which apparently doesn't necessarily do so) on which the answers have mostly focused. I was looking for a way to evaluate an expression, and if the expression threw an exception, for a default value to be provided instead. For example, in Ruby: quotient = begin; a / b; rescue; -1; end Or in Lisp: (setq quotient (condition-case nil (/ a b) (error -1))) Not having written much exception-related code in Perl 6, I hoped that this might work: sub divide($a, $b) { die "Zero denominator" if $b == 0; $a / $b } my $quotient = do { divide($a, $b); CATCH { default { -1 } } }; It doesn't, though. As far as I can tell, the value to which a CATCH block evaluates is ignored; the only useful things one can do in such a block are things with side effects. Long story short, I eventually came up with this: my $quotient = do { my $q; { $q = divide($a, $b); CATCH { default { $q = -1 } } }; $q }; That's far more verbose than I've come to expect from Perl 6. Is there some more concise way of expressing this logic? The doc page on exceptions mentions try, eg: my $quotient = try { divide($a, $b) } // -1; That works in this specific case, but it seems insufficient in general. The function might validly return an undefined value, and this construction can't distinguish between that and an exception. Also, it wouldn't let me distinguish among various exception cases. I'd have to do something like: class EA is Exception { } class EB is Exception { } sub divide($a, $b) { (EA, EB).pick.new.throw if $b == 0; $a / $b } my $quotient = do { my $q; { $q = divide($a, $b); CATCH { when EA { $q = -1 }; when EB { $q = -2 } } }; $q }; [1] https://stackoverflow.com/questions/51644197/returning-values-from-exception-handlers-in-perl-6/51670573
Re: need regex help
On 08/02/2018 05:18 AM, Timo Paulssen wrote: Is this what you want? perl6 -e 'say "12345" ~~ /^<+alnum -alpha>+$/' 「12345」 perl6 -e 'say "123a45" ~~ /^<+alnum -alpha>+$/' Nil HTH - Timo What does the following do? +alnum (why does it need the "+"?) -alpha (I presume "-" means negate?) +$ Many thanks, -T
Re: need regex help
On 08/02/2018 05:18 AM, Timo Paulssen wrote: Is this what you want? perl6 -e 'say "12345" ~~ /^<+alnum -alpha>+$/' 「12345」 perl6 -e 'say "123a45" ~~ /^<+alnum -alpha>+$/' Nil HTH - Timo A piece of art. Thank you!