Ron Savage [mailto:[EMAIL PROTECTED] wrote:
 
> Step 2: Now, when yet other code, let's call it 'sub last_insert_id()',
> wants to
> execute the aforesaid and understated conditional code, it can do this:
> 
>       if ($self -> db_vendor() =~ /(?:mysql|Pg)/)
>       {
>               ...
>       }
> 
> Neat, huh?
> 
> This uses non-capturing parentheses to emulate:
> 
>       if ( ($self -> db_vendor() eq 'mysql') || ($self -> db_vendor() eq
> 'Pg') )
>       {
>               ...
>       }

> That's the funny thing about Perl, there can be more than one way to do
> things.

The point you seem to be missing is that these two code snippets do not mean
precisely the same thing.

The second expression evaluates to true if and only if $self->db_vendor()
returns either 'mysql' or 'Pg'.

The first expression evaluates to true if $self->db_vendor() returns 'mysql'
or 'Pg', or any string that contains 'mysql' or 'Pg'.  For example, it would
match a vendor called 'NotPg', which may not be the desired behavior

In order to match the eq behavior, you could use the regex
/^(?:mysql|Pg)\z/, which is what Dr. Ruud was suggesting.


On the one hand, this level of caution is probably not necessary in this
case.  From your explanation, we know that the simpler regex is sufficient
given $db->vendor()'s possible return values.

On the other hand, this level of caution is certainly not harmful, so I'm
not sure why you were so annoyed by the suggestion.


Ronald


Reply via email to