> -----Original Message----- > From: John Doe [mailto:[EMAIL PROTECTED] > Sent: Friday, April 29, 2005 8:50 AM > To: beginners@perl.org > Subject: Re: REGEXP removing - il- - -b-f and - il- - - - f > > > Am Freitag, 29. April 2005 14.43 schrieb [EMAIL PROTECTED]: > > So which is safer more ideal to use : || , or > > > > > > Derek B. Smith > > OhioHealth IT > > UNIX / TSM / EDM Teams > [...] > > The only difference between "||" and "or" is the precedence, and the > precedence of "=" lies between them. > > To my understanding, in the "assign or die" special case, > > my $a=do_something_which_can_fail() or handle_exception();
I'm sure I'll be corrected if I am wrong, but I believe that Perl sees the above line of code like this: ( my $a = do_something_which_can_fail() ) or ( handle_exception() ); Because of the low precedence of the "or" operator. Therefore, Perl will try to assign the return value of "do_something_which_can_fail()" subroutine to $a, and if it can't, THEN perl will execute the "handle_exception()" subroutine. This is probably what the coder wanted. > > is more logic than > > my $a=do_something_which_can_fail() || handle_exception(); Perl sees THIS line of code like this: my $a = ( ( do_something_which_can_fail() ) || ( handle_exception() ) ); Because of the high precedence of the "||" operator. Therefore, perl will run the "do_something which can fail()" subroutine, and if it succeeds, assign the value returned to $a. OTHERWISE, if it fails, the return value from "handle_exception()" subroutine will be assigned to $a. This is probably NOT what you want to happen. > > because something should be assigned to $a, and if that > fails, the app should > e.g. die. This way, the exception handling is not part of the > assignement. > > On the other side, I would use > > my $a=do_something_which_can_fail() || provide_some_default(); > > because the exception handling consists of providing a value. > > Just my personal way to look at it :-) > > joe > --Errin Larsen -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>