r29454 - docs/Perl6/Spec
Author: moritz Date: 2010-01-04 11:49:21 +0100 (Mon, 04 Jan 2010) New Revision: 29454 Modified: docs/Perl6/Spec/S05-regex.pod Log: [S05] small clarification for .match and .subst Modified: docs/Perl6/Spec/S05-regex.pod === --- docs/Perl6/Spec/S05-regex.pod 2010-01-04 10:47:13 UTC (rev 29453) +++ docs/Perl6/Spec/S05-regex.pod 2010-01-04 10:49:21 UTC (rev 29454) @@ -3948,6 +3948,15 @@ $str.=subst(/pat/, replacement); $str.=subst(/pat/, {replacement}); +The C.match and C.subst methods support the adverbs of Cm// and +Cs/// as named arguments, so you can write + +$str.match(/pat/, :g) + +as an equivalent to + +$str.comb(/pat/, :match) + There is no syntactic sugar here, so in order to get deferred evaluation of the replacement you must put it into a closure. The syntactic sugar is provided only by the quotelike forms. First there @@ -3988,7 +3997,7 @@ into something like: -$target.subst(rx:g[pattern], { $() op expr }) +$target.subst(rx[pattern], { $() op expr }, :g) So, for example, you can multiply every dollar amount by 2 with:
Re: String to Regex
On Sun, Jan 3, 2010 at 8:30 PM, Moritz Lenz mor...@faui2k3.org wrote: But since $input can contain closures, arbitrary code can be executed. I'd like to propose a way to compile a string to a regex which doesn't allow code execution. So would I. I would also like it to be the default behaviour, since this is a place foot on Bouncing Betty thing. :) my $rx = Regex.new(:string('abc|d'), :safe); I think this is too complicated for something which is likely to be the most frequent use of regex strings. It would be better to enforce the more complicated syntax for the less frequent cases. (All IMO, of course.) -- Jan
Custom errors on subsets?
Given this code: subset Filename of Str where { $_ ~~ :f }; sub foo (Filename $name) { say Houston, we have a filename: $name; } my Filename $foo = $*EXECUTABLE_NAME; foo($foo); foo($*EXECUTABLE_NAME); foo('no_such_file'); We get this output: Houston, we have a filename: /Users/ovid/bin/perl6 Houston, we have a filename: /Users/ovid/bin/perl6 Constraint type check failed for parameter '$name' in Main (file src/gen_setting.pm, line 324) Obviously the error message can use some work, but how would I customize that error message (assuming such will be possible in the future)? Clearly there will be many cases where a custom error message for constraint failure could make life much easier for developers. Cheers, Ovid -- Buy the book - http://www.oreilly.com/catalog/perlhks/ Tech blog- http://use.perl.org/~Ovid/journal/ Twitter - http://twitter.com/OvidPerl Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
Re: Custom errors on subsets?
On Mon, Jan 4, 2010 at 5:15 AM, Ovid publiustemp-perl6langua...@yahoo.com wrote: Given this code: subset Filename of Str where { $_ ~~ :f }; sub foo (Filename $name) { say Houston, we have a filename: $name; } ... Obviously the error message can use some work, but how would I customize that error message (assuming such will be possible in the future)? Clearly there will be many cases where a custom error message for constraint failure could make life much easier for developers. How about multi sub foo(Any $name) { die Houston, we have a major malfunction.} -y
Re: Custom errors on subsets?
--- On Mon, 4/1/10, yary not@gmail.com wrote: From: yary not@gmail.com How about multi sub foo(Any $name) { die Houston, we have a major malfunction.} Looks like tha would work, but it forces the developer to remember to write this extra code every time they may have a constraint failure, if they forget, we're back to the old, cryptic message. It would be much nicer to be able to do this (psuedo-code, obviouly): subset Filename of Str where { $_ ~~ :f } :OnFail { No such file: '$_' } subset Celsius of Num where { $_ = -273.15 } :OnFail { Celsius temperature should be a Num = -273.15, not '$_' } With something akin to that, developers won't have to write extra boilerplate every time a constraint fails. Plus, the code is friendlier :) Cheers, Ovid -- Buy the book - http://www.oreilly.com/catalog/perlhks/ Tech blog- http://use.perl.org/~Ovid/journal/ Twitter - http://twitter.com/OvidPerl Official Perl 6 Wiki - http://www.perlfoundation.org/perl6