Steve Bertrand wrote:
John W. Krahn wrote:
Steve Bertrand wrote:

+sub is_word ($) {
+    return unless defined $_[0] && $_[0] ne '';
+    return if $_[0] =~ /[\w\s]+/;
+    return 1;
+}
The other is_* functions in that module use anchors with their regular
expressions so you probably should as well:

+    return if $_[0] =~ /^[\w\s]+$/;

Note that my sub (is_word) is the only one that has a return based on
if() and not unless()...

Yes, sorry, that *should* be:

+    return unless $_[0] =~ /^[\w\s]+$/;

Or:

+    return if $_[0] !~ /^[\w\s]+$/;


You need the anchors because you want to verify that $_[0] contains *only* [\w\s] characters. Without the anchors $_[0] could contain any characters and return TRUE.

Your original expression "return if $_[0] =~ /[\w\s]+/" says "return FALSE if $_[0] contains a [\w\s] character".


For some reason, I just couldn't wrap my head around the negative aspect
of 'unless' compared to the straight-forward 'if'. Twisting the
expression was easier for me than twisting the regex.

In my Real Life, I guess my mentality is stuck regarding 'unless' etc.
My mind would have me write out five lines of code, so long as those
lines didn't include 'unless'. I'm here to learn better ways however ;)


if ($life != $death) {
    have_sex_be_friendly();
} else {
    die "It was worth it (I love you Tracy!): $!";
}

$life == $death and die "It was worth it (I love you Tracy!): $!";
have_sex_be_friendly();




John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

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