problem with 'or' statement

Discussion in 'Perl Misc' started by lucas, Jan 21, 2004.

  1. lucas

    lucas Guest

    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
    --
    lucas
    -------------------------
    Perl Coder since 2001
    shift || die;
    -------------------------
    lucas, Jan 21, 2004
    #1
    1. Advertising

  2. lucas wrote:
    > 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() };

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 21, 2004
    #2
    1. Advertising

  3. lucas <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 21, 2004
    #3
  4. lucas

    $_@_.%_ Guest

    > 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}
    $_@_.%_, Jan 21, 2004
    #4
  5. lucas wrote:
    > 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
    Chris Mattern, Jan 21, 2004
    #5
  6. $_@_.%_ <$_@_.%_> wrote:
    >> 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();



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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 21, 2004
    #6
  7. lucas

    lucas Guest

    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 ;)
    --
    lucas
    -------------------------
    Perl Coder since 2001
    shift || die;
    -------------------------
    lucas, Jan 21, 2004
    #7
  8. lucas

    Uri Guttman Guest

    >>>>> "l" == lucas <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 21, 2004
    #8
  9. lucas <> wrote:

    > Tad: || = or



    No it doesn't.

    You should perhaps go find out what is different between them:

    perldoc perlop


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 21, 2004
    #9
  10. lucas

    lucas Guest

    Michele Dondi wrote:

    > On Wed, 21 Jan 2004 12:17:59 -0500, lucas <> wrote:
    >
    >>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!
    >

    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

    --
    lucas
    -------------------------
    Perl Coder since 2001
    shift || die;
    -------------------------
    lucas, Jan 22, 2004
    #10
  11. On Wed, 21 Jan 2004 07:34:07 +0100, Gunnar Hjalmarsson
    <> wrote:

    >lucas wrote:
    >> 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() };


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


    Michele
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
    Michele Dondi, Jan 22, 2004
    #11
  12. On Wed, 21 Jan 2004 12:17:59 -0500, lucas <> wrote:

    >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
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
    Michele Dondi, Jan 22, 2004
    #12
  13. On Thu, 22 Jan 2004 01:14:47 -0500, lucas <> wrote:

    >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
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
    Michele Dondi, Jan 24, 2004
    #13
    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. Replies:
    3
    Views:
    804
  2. Jay McGavren
    Replies:
    11
    Views:
    1,132
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,081
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    463
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,055
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page