Home > Archive > PERL Miscellaneous > October 2004 > How's my logic?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| 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!
| |
| Henry Law 2004-10-27, 8:56 pm |
| On Wed, 27 Oct 2004 15:55:00 -0400, wana <ioneabu@yahoo.com> wrote:
>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
| |
| Henry Law 2004-10-27, 8:56 pm |
| On Wed, 27 Oct 2004 22:55:50 +0100, Henry Law
<lawshouse.public@btconnect.com> wrote:
>On Wed, 27 Oct 2004 15:55:00 -0400, wana <ioneabu@yahoo.com> wrote:
>
>
>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
| |
| Paul Lalli 2004-10-27, 8:56 pm |
| wana wrote:
> 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
| |
| Henry Law 2004-10-27, 8:56 pm |
| On Wed, 27 Oct 2004 18:05:05 -0400, Paul Lalli <mritty@gmail.com>
wrote:
[color=darkred]
>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
| |
| Michele Dondi 2004-10-28, 3:56 am |
| On Wed, 27 Oct 2004 15:55:00 -0400, wana <ioneabu@yahoo.com> wrote:
>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,-p <your_script_here>
>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
| |
| Peter Hickman 2004-10-28, 8:56 am |
| Paul Lalli wrote:
> 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!
| |
| Whitey Johnson 2004-10-28, 3:59 pm |
| On Wed, 27 Oct 2004 15:55:00 -0400, wana muttered incoherently:
> 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.
| |
| Michele Dondi 2004-10-28, 3:59 pm |
| On Thu, 28 Oct 2004 08:45:48 +0200, Michele Dondi
<bik.mido@tiscalinet.it> wrote:
>
>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
| |
| Ben Morrow 2004-10-28, 8:56 pm |
|
Quoth ioneabu@yahoo.com:
> 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
--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
ben@morrow.me.uk
|
|
|
|
|