Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

If/Else or Unless Not Triggering Correctly

by ljamison (Sexton)
on Apr 21, 2016 at 14:19 UTC ( [id://1161093]=perlquestion: print w/replies, xml ) Need Help??

ljamison has asked for the wisdom of the Perl Monks concerning the following question:

Hail Monks!

I have a module with a function which should only trigger if certain conditions are true/false (depending on if I use if or unless) but the way it is currently triggering it appears that the if/unless is being ignored. I'm under the impression it has to do with the priority order of the operators but I'm not certain.

Attempt using unless:

#/usr/bin/perl -w use strict; use warnings; ######################## ## unrelated code here # ######################## sub triggerNotification { my $data = @_; # data passed into function to be processed unless($data == 'ARCHITECH' || $data == 'CURR-LIB' || $data == 'MARKET_PL') { # send email notification } }

Attempt using if/else:

#/usr/bin/perl -w use strict; use warnings; ######################## ## unrelated code here # ######################## sub triggerNotification { my $data = @_; # data passed into function to be processed if(!($data == 'ARCHITECH') || !($data == 'CURR-LIB') || !($data == 'MARKET_PL')) { # send email notification } else { # do nothing } }

Both of these attempts failed to prevent the email notification from being sent out and I'm not sure what I have written wrong. Thank you in advance for any advice!

Replies are listed 'Best First'.
Re: If/Else or Unless Not Triggering Correctly
by stevieb (Canon) on Apr 21, 2016 at 14:25 UTC

    Use eq to compare strings. == is for digity-type things:

    unless( $data eq 'ARCHITECH' || $data eq 'CURR-LIB' || $data eq 'MARKET_PL +'){ ... }
      else { # do nothing }
      is exactly the same as
      # do nothing
      (THAT IS if an optimizer zapped this clause for you. Otherwise it's not quite the same because it wastes machine cycles)
Re: If/Else or Unless Not Triggering Correctly
by talexb (Chancellor) on Apr 21, 2016 at 17:21 UTC

    As a matter of style, I prefer to use a hash to figure out whether something is in a set, or not. So your code would be simplified to the following:

    sub triggerNotification { my ( $data ) = @_; my %message_classes = ( 'ARCHITECH' => 1, 'CURR-LIB' => 1, 'MARKET_PL' => 1, ); if ( not exists $message_classes{ $data } ) { # Send an E-Mail notification if the message type is not listed. } }
    Using 'data' has a variable name is amusing -- you can probably find a better name than that. :)

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: If/Else or Unless Not Triggering Correctly
by Laurent_R (Canon) on Apr 21, 2016 at 19:08 UTC
    In addition to the already-mentioned fact that you should use eq instead of == operator for string comparison (or a hash lookup as suggested by talexb), this code line is also not good:
    my $data = @_; # data passed into function to be processed
    Assigning a scalar to an array is not likely to give you what you want. Try instead:
    my $data = @_[0]; # data passed into function to be processed
    or:
    my ($data) = @_; # data passed into function to be processed
    or:
    my $data = shift; # data passed into function to be processed
Re: If/Else or Unless Not Triggering Correctly
by Cuhulain (Beadle) on Apr 22, 2016 at 15:55 UTC

    I think your Boolean logic for the "if" code is wrong.

    From https://en.wikipedia.org/wiki/De_Morgan%27s_laws:

    "not (A or B)" is the same as "(not A) and (not B)"

    Using your example, where a condition like $data eq 'ARCHITECH' is represented by letter A:

     unless( A or B or C )

    means the same as:

     if not ( A or B or C )

    which, by De Morgan's Law, is equivalent to :

     if  ( not-A and not-B and not-C )

    But you have written the "if" code as:

     if  ( not-A or not-B or not-C )

    which does definitely not match your "unless" code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1161093]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found