Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How's my logic?

Reply
Thread Tools

How's my logic?

 
 
wana
Guest
Posts: n/a
 
      10-27-2004
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!

 
Reply With Quote
 
 
 
 
Henry Law
Guest
Posts: n/a
 
      10-27-2004
On Wed, 27 Oct 2004 15:55:00 -0400, wana <> 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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-27-2004
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
 
Reply With Quote
 
Henry Law
Guest
Posts: n/a
 
      10-27-2004
On Wed, 27 Oct 2004 22:55:50 +0100, Henry Law
<> wrote:

>On Wed, 27 Oct 2004 15:55:00 -0400, wana <> 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?


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
 
Reply With Quote
 
Henry Law
Guest
Posts: n/a
 
      10-27-2004
On Wed, 27 Oct 2004 18:05:05 -0400, Paul Lalli <>
wrote:

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


>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
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      10-28-2004
On Wed, 27 Oct 2004 15:55:00 -0400, wana <> 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{pop^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,
 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      10-28-2004
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!
 
Reply With Quote
 
Whitey Johnson
Guest
Posts: n/a
 
      10-28-2004
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.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-28-2004

Quoth :
> 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.'

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      10-28-2004
On Thu, 28 Oct 2004 08:45:48 +0200, Michele Dondi
<> wrote:

>> $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


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


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57