How's my logic?

W

wana

Here is a piece of code from a simple program I wrote to do some FTPing. I
was wondering if the form of my statement is acceptable with respect to the
use of 'and' and 'or'. I did it, hoping for the best, and it seems to
work. I am always shaky on rules of precedence and usually use lots of
()'s to make sure I get it right, but I am trying to wean myself off of
them.

if ($user and $pass)
{
$ftp->login($user, $pass)
and print "successfully logged on to $server as $user\n"
or print "problem logging on to $server as $user\n";
}


Thanks!
 
H

Henry Law

if ($user and $pass)
{
$ftp->login($user, $pass)
and print "successfully logged on to $server as $user\n"
or print "problem logging on to $server as $user\n";
}

Can you post a complete program? Then someone (even me, a comparative
newbie) could run it, see what happens and try to help. I can't run
that fragment, though, and to try to erect a scaffolding around it to
enable it to run would (a) be a drag to do; and (b) distance the code
so far from yours as to be meaningless.

Henry Law <>< Manchester, England
 
P

Paul Lalli

wana said:
Here is a piece of code from a simple program I wrote to do some FTPing. I
was wondering if the form of my statement is acceptable with respect to the
use of 'and' and 'or'. I did it, hoping for the best, and it seems to
work. I am always shaky on rules of precedence and usually use lots of
()'s to make sure I get it right, but I am trying to wean myself off of
them.

if ($user and $pass)
{
$ftp->login($user, $pass)
and print "successfully logged on to $server as $user\n"
or print "problem logging on to $server as $user\n";
}

perldoc perlop has the precedence table, and will tell you that and has
a slightly higher precedence than or. Therefore, this statement is
equivalent to:
($ftp->login($user, $pass) and print "Success...") or print "problem...";

Note that this probably isn't exactly what you want to do. The failure
message will be printed if *either* the FTP login fails or the success
message fails to print. Now, granted it's rather unusual for a print
statement to fail, but this is generally not a good pattern to get into
the habbit of using.

Much prefered (IMHO, of course) would be to abandon the use of and/or as
control-flow operators in this case, and use the normal if/else syntax:
if ($ftp->login($user, $pass){
print "Success...";
} else {
print "Problem...";
}

Paul Lalli
 
H

Henry Law

Can you post a complete program?

OK so I thought I'd perservere as a learning exercise. Here's a test
program which you might have written (Activestate Perl, BTW)

# -------------------------------------------
#! /usr/bin/perl

use strict;
use warnings;

open (INFILE,"file.exists")
and print "File opened OK\n"
or print "File not there\n";

my $rec = <INFILE>; # Breaks when file not there but
# suppresses perl warning
# -------------------------------------------

Here's the console output
F:\$WIP>type file.exists
adfqsfsd

F:\$WIP>tryout.pl
File opened OK

F:\$WIP>erase file.exists

F:\$WIP>tryout.pl
File not there
readline() on closed filehandle INFILE at F:\$WIP\tryout.pl line 10.

F:\$WIP>

So conclusions: (1) Your logic looks OK, assuming that the FTP thing
you're doing returns TRUE only when it works; (2) My knowledge has
improved because I didn't know you could do that "...and...or.."
thing. Thanks!

Henry Law <>< Manchester, England
 
H

Henry Law

Note that this probably isn't exactly what you want to do. The failure
message will be printed if *either* the FTP login fails or the success
message fails to print.

Sheesh. That's why I'm a Perl floor-sweeper and Paul's a Transcendent
Master.

Well, I stand by my comments about posting an executable program at
any rate; and by my thanks.

Henry Law <>< Manchester, England
 
M

Michele Dondi

work. I am always shaky on rules of precedence and usually use lots of
()'s to make sure I get it right, but I am trying to wean myself off of
them.

Well, you can always check for yourself with

perl -MO=Deparse said:
if ($user and $pass)
{
$ftp->login($user, $pass)
and print "successfully logged on to $server as $user\n"
or print "problem logging on to $server as $user\n";
}

I don't know what the return value of $ftp->login() is, but as far as
only C<and> and C<or> are concerned, I'd say that it is ok. BTW: you
know that the info you need is at the very top of 'perldoc perlop',
don't you?


Michele
 
P

Peter Hickman

Paul said:
Much prefered (IMHO, of course) would be to abandon the use of and/or as
control-flow operators in this case, and use the normal if/else syntax:
if ($ftp->login($user, $pass){
print "Success...";
} else {
print "Problem...";
}

This is also much easier to read!
 
W

Whitey Johnson

Here is a piece of code from a simple program I wrote to do some FTPing. I
was wondering if the form of my statement is acceptable with respect to the
use of 'and' and 'or'. I did it, hoping for the best, and it seems to
work. I am always shaky on rules of precedence and usually use lots of
()'s to make sure I get it right, but I am trying to wean myself off of
them.

if ($user and $pass)
{
$ftp->login($user, $pass)
and print "successfully logged on to $server as $user\n"
or print "problem logging on to $server as $user\n";
}


Thanks!

I haven't tried anything with ftp yet but could you use an 'or die' here
so that you don't have to wait for the rest of the program to execute if
you can't log in?

$ftp->login($user, $pass) or die "Couldn't log in: $!\n;
print "logged into $server successfully\n";
....rest of prog.
 
B

Ben Morrow

Quoth (e-mail address removed):
Here is a piece of code from a simple program I wrote to do some FTPing. I
was wondering if the form of my statement is acceptable with respect to the
use of 'and' and 'or'. I did it, hoping for the best, and it seems to
work. I am always shaky on rules of precedence and usually use lots of
()'s to make sure I get it right, but I am trying to wean myself off of
them.

if ($user and $pass)
{
$ftp->login($user, $pass)
and print "successfully logged on to $server as $user\n"
or print "problem logging on to $server as $user\n";
}

I must say, I have always wanted a low-precedence version of ?:, perhaps
'... then ... else ...', for just this situation. :)

Ben
 
M

Michele Dondi

I don't know what the return value of $ftp->login() is, but as far as
only C<and> and C<or> are concerned, I'd say that it is ok. BTW: you

Also, you probably want the second print() to be a die() or a warn().


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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top