On 8 May 2010 14:47, Harry Putnam <rea...@newsguy.com> wrote:
> 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>);

You are quite correct - there is no practical difference in this situation.

[nitpick: actually, if $/ has been set to something other than "\n",
$chosen may have a trailing "\n" character.]

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

Perl Best Practices, among others, considers \A and \z to be better
style than ^ and $ because you are almost always matching on string
boundaries rather than line boundaries and you should say what you
mean. ^$ can match in midstring under /m and this behaviour is
surprising to those who think that ^$ only ever match at string
boundaries, rather than line boundaries. As a result, the argument
goes, you should always use \A and \z to match on string boundary.

Using \z rather than \Z is just about being explicit with what you do
to your data. You can't accidentally ignore a trailing "\n" if you use
\z.

I'm not 100% convinced by these arguments, but I'm not against them
either. It's good to know about \A, \Z and \z just for those
situations when you come across code that uses it. But you can always
look it up in perlre if you forget.

Phil

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