Philip Potter <philip.g.pot...@gmail.com> writes:

> On 8 May 2010 13:35, Harry Putnam <rea...@newsguy.com> wrote:
>> I'm curious about one thing I see there:
>>
>>  `/\A\d+\z/ ' as opposed to `/^\d+$/'
>>
>> in this snippet:
>>
>>        if ( $chosen =~ /\A\d+\z/ && $chosen >= 1 && $chosen <= @h ) {
>>            print "Taking some action on $h[$chosen - 1]\n";
>>            last;
>>            }
>>         [...]
>>
>> Is the second formulation (`/^\d+$/' - the one I used) likely to miss under
>> certain conditions?
>
> It's all in perldoc perlre.

If you mean this (from perlre):

      [...]
       A word boundary ("\b") is a spot between two characters that has a "\w"
       on one side of it and a "\W" on the other side of it (in either order),
       counting the imaginary characters off the beginning and end of the
       string as matching a "\W".  (Within character classes "\b" represents
       backspace rather than a word boundary, just as it normally does in any
       double-quoted string.)  The "\A" and "\Z" are just like "^" and "$",
       except that they won't match multiple times when the "/m" modifier is
       used, while "^" and "$" will match at every internal line boundary.  To
       match the actual end of the string and not ignore an optional trailing
       newline, use "\z".

But there are no trailing new lines.. guaranteed by:

      chomp(my $chosen = <STDIN>);

Or do you mean its just better practice in general to use \A and \z,
just in case.


-- 
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