Hi Chris,
Chris Wagner <[EMAIL PROTECTED]> wrote:
> At 01:28 AM 5/3/2006 -0400, David Kaufman wrote:
>> my ($b, $c) = ($1, $2) if $a =~ /^(\D+)(\d+)/;
>
> U can't combine a my and an if. Perl will go schizo.
Sure "U" can :-) I use this type of construct all the time, even with strict
mode and warnings on. What you mean by "perl will go schizo"?
What *will* happen is that, if $a doesn't match the regex, $b and $c will be
undefined. But I use this in cases, like the original poster's, where the
regex is expected to always match. In such cases an error has already occurred
so any subsequent "Use of uninitialized value" warnings will warn me that the
string contained something it was never supposed to contain.
If you're not so trusting of the source string, and you want to be absolutely
sure that your string matched before continuing, or would just prefer a more
explicit error if it ever does not, you can test for the undefinedness of $b
and $c right after the assignment:
my ($b, $c) = ($1, $2) if $a =~ /^(\D+)(\d+)/;
unless (defined($b) and defined($c)) {
die '$a was not a series of letters followed by a series of numbers!!';
}
but I'd usually use the idiom above to *save* those extra three lines, as I
said, when I am reasonably confident that the string will indeed match. If I'm
not, thess three lines (mentioned elsewhere in the thread) would do the same
job, and be quite a bit more obvious to whoever reads the code in the future:
my ($b, $c);
if ($a =~ /^(\D+)(\d+)/) { ($b, $c) = ($1, $2) }
else {die '$a was not a series of letters followed by a series of numbers!!' }
-dave (not really always a code-compacting zealot, but sometimes less is more!)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs