Please stop using my mail address when replying, I'm on the list and
don't want two copies of the same mail (it's not about you Mike).

Mike <te...@mflan.com>
> Why is my Perl not working on that command?
> 
> $ perl -e 'exit(10) if "aaa"=~/a{,2}/;'
> Unescaped left brace in regex is illegal here in regex; marked by <-- 
> HERE in m/a{ <-- HERE ,2}/ at -e line 1.
> $
> 
> But this works:
> $ perl -e 'exit(10) if "aaa"=~/a{0,2}/;'
> $
> 
> $ echo $?
> 10
> $

 On an old debian woody box I get:
$ perl -v | grep v5
This is perl, v5.6.1 built for i386-linux
$ perl -e 'exit(10) if "aaa"=~/a{,2}/;'; echo $?
0
$ perl -e 'exit(10) if "aaa"=~/a{0,2}/;'; echo $?
10

$ man perlre
...
       The following standard quantifiers are recognized:

           *      Match 0 or more times
           +      Match 1 or more times
           ?      Match 1 or 0 times
           {n}    Match exactly n times
           {n,}   Match at least n times
           {n,m}  Match at least n but not more than m times
...

 So, old perl versions don't have the {,m} quantifier, check your 
documentation for that. The easy way out is to always use {0,m} instead 
of {,m}, which is the same thing in modern perl, actually there is no
need ever to use the {,m} quantifier.

I don't know why I don't get a perl error message above, maybe a bug.

///

 On a more uptodate system I get:
$ perl -v | grep v5
This is perl 5, version 34, subversion 1 (v5.34.1) built for 
x86_64-linux-thread-multi
$ perl -e 'exit(10) if "aaa"=~/a{,2}/;'; echo $?
10
$ perl -e 'exit(10) if "aaa"=~/a{0,2}/;'; echo $?
10

///

 If you are interested of the syntax rules, check under "Simple 
statements" in:

 (perl 5.6.1)
$ man perlsyn
       Any simple statement may optionally be followed by a SIN-
       GLE modifier, just before the terminating semicolon (or
       block ending).  The possible modifiers are:

           if EXPR
           unless EXPR
           while EXPR
           until EXPR
           foreach EXPR

...

 (perl 5.34.1)
$ man perlsyn
...
   Statement Modifiers
       Any simple statement may optionally be followed by a SINGLE modifier,
       just before the terminating semicolon (or block ending).  The possible
       modifiers are:

           if EXPR
           unless EXPR
           while EXPR
           until EXPR
           for LIST
           foreach LIST
           when EXPR

...

So, modern perl also have "for" and "when".

///

Also note that in a compound statement you have to ()'ize the EXPR as in

 if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK

in contrast to for the modifier you don't need to:

 STATEMENT if EXPR;

I prefer to always to use ()' around the expression, since it makes it 
easier to convert between the two forms.

Regards,
/Karl Hammar



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to