How's my logic?

Discussion in 'Perl Misc' started by wana, Oct 27, 2004.

  1. wana

    wana Guest

    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!
    wana, Oct 27, 2004
    #1
    1. Advertising

  2. wana

    Henry Law Guest

    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
    Henry Law, Oct 27, 2004
    #2
    1. Advertising

  3. wana

    Paul Lalli Guest

    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
    Paul Lalli, Oct 27, 2004
    #3
  4. wana

    Henry Law Guest

    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
    Henry Law, Oct 27, 2004
    #4
  5. wana

    Henry Law Guest

    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
    Henry Law, Oct 27, 2004
    #5
  6. 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,
    Michele Dondi, Oct 28, 2004
    #6
  7. 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!
    Peter Hickman, Oct 28, 2004
    #7
  8. 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.
    Whitey Johnson, Oct 28, 2004
    #8
  9. wana

    Ben Morrow Guest

    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.'
    Ben Morrow, Oct 28, 2004
    #9
  10. 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,
    Michele Dondi, Oct 28, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Qunwei Chen
    Replies:
    6
    Views:
    3,624
    Renaud Pacalet
    Aug 22, 2003
  2. RZ
    Replies:
    2
    Views:
    502
  3. Cliff Cummings

    SystemVerilog: "logic" or "ulogic?"

    Cliff Cummings, Sep 16, 2003, in forum: VHDL
    Replies:
    7
    Views:
    4,120
    cc0139808
    Sep 20, 2003
  4. Richard B. Katz
    Replies:
    0
    Views:
    677
    Richard B. Katz
    Dec 4, 2003
  5. spike
    Replies:
    8
    Views:
    1,442
    Steve Holden
    Feb 9, 2010
Loading...

Share This Page