problem with 'or' statement

L

lucas

is there some way i can write:
if (some_routine()) { do_this(); and_that(); }

like so:
some_routine() || { do_this(); and_that(); }

the reason is, i have a bunch of code that would be a pain in the ass to mod
if i have to rewrite it as the former

thx
 
G

Gunnar Hjalmarsson

lucas said:
is there some way i can write:
if (some_routine()) { do_this(); and_that(); }

like so:
some_routine() || { do_this(); and_that(); }

This is valid code, equivalent to the first example:

some_routine() and do { do_this(); and_that() };
 
T

Tad McClellan

lucas said:
Subject: problem with 'or' statement


Did you mean to mention the "or" operator somewhere in your post?

If not, then why the misleading choice of Subject?
 
$

$_

is there some way i can write:
if (some_routine()) { do_this(); and_that(); }

like so:
some_routine() || { do_this(); and_that(); }

the reason is, i have a bunch of code that would be a pain in the ass to mod
if i have to rewrite it as the former

$var = &some_routine(); #make sure you use the return function in your subroutine.
if ($var == 1) { #do something or nothing }
else { #do something or nothing}
 
C

Chris Mattern

lucas said:
is there some way i can write:
if (some_routine()) { do_this(); and_that(); }

like so:
some_routine() || { do_this(); and_that(); }

Well, no. They don't do the same thing. The first does do_this()
and and_that() if some_routine() is true. The second does the
two functions only if some_routine() *isn't* true. Perhaps you
wanted this, instead:

some_routine() && { do_this(); and_that(); }

Chris Mattern
 
T

Tad McClellan

$_@_.%_ said:
$var = &some_routine();


Why are you using the ampersand?

$var = some_routine();

#make sure you use the return function in your subroutine.


Why do you need to make sure you use the return function in your subroutine?

if ($var == 1) { #do something or nothing }
else { #do something or nothing}


Is your followup somehow related to the question that was asked?

I'm not seeing it...
 
L

lucas

Thanks everyone for your posts


Gunner, Chris and Bernard have exactly what I need. I just never thought of
using the do {} to exectute the routines.

Tad: || = or

Thanks again ;)
 
U

Uri Guttman

l> Thanks everyone for your posts
l> Gunner, Chris and Bernard have exactly what I need. I just never thought of
l> using the do {} to exectute the routines.

l> Tad: || = or

no it doesn't. you will burn yourself if you think that.

uri
 
L

lucas

Michele said:
(1) || != or (see perldoc perlop)

$x = 0 or print 'cool!';
$y = 0 || print 'cool!';
print $x,$y;

(2) I bet you whatever you like Tad knows that || is "much like" 'or'.
He means that you didn't have a "problem with 'or' statement".

(3) I decided not to be fussy in my previuous post in this thread, but
your second snippet (notwithstanding the fact that it was incorrect -
as you knew, and this is the reason why you were asking here),
suggested a *working* form (e.g. that with 'do') that was not
equivalent to the first snippet: you should have asked about the 'and'
operator!
1:I actually forgot a ! in if (!some_routine()) {}
2:the reason I thought that || = or was because I've seen things like
open(FILE,"file") || die; and open(FILE,"file") or die;
3:thanks for clearing up the || != or thing for me
 
M

Michele Dondi

This is valid code, equivalent to the first example:

some_routine() and do { do_this(); and_that() };

Also some_routine() and do_this(), and_that();


Michele
 
M

Michele Dondi

Tad: || = or

(1) || != or (see perldoc perlop)

$x = 0 or print 'cool!';
$y = 0 || print 'cool!';
print $x,$y;

(2) I bet you whatever you like Tad knows that || is "much like" 'or'.
He means that you didn't have a "problem with 'or' statement".

(3) I decided not to be fussy in my previuous post in this thread, but
your second snippet (notwithstanding the fact that it was incorrect -
as you knew, and this is the reason why you were asking here),
suggested a *working* form (e.g. that with 'do') that was not
equivalent to the first snippet: you should have asked about the 'and'
operator!


Michele
 
M

Michele Dondi

1:I actually forgot a ! in if (!some_routine()) {}

Well, it's obvious (even) from your .sig that you're mad about the

something || something else

syntax. No wonder: that's cool! But &&/and's short circuiting
semantics is just as cool and IMHO more straightforward in most
situations (than using a "!", which is convoluted syntax, if not
really necessary, still IMHO).

You'll also notice that && and || and "and", and "or" respectively
have different precedences so that you can write unambiguously
statements like this one:

# fake example
-f and /\.txt/ or
warn "`$_': not a regular file or has an incorrect extension\n"
for @ARGV;


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top