Please explain "and next"

Discussion in 'Perl Misc' started by Dave Saville, Jan 19, 2014.

  1. Dave Saville

    Dave Saville Guest

    Came across this today

    print "1 OK\n" and next unless (length $line[0]);

    Never seen that construct before. Is it just some perlish shorthand
    to get stuff on one line?

    TIA
    --
    Regards
    Dave Saville
    Dave Saville, Jan 19, 2014
    #1
    1. Advertising

  2. On 2014-01-19 15:02, Dave Saville <> wrote:
    > Came across this today
    >
    > print "1 OK\n" and next unless (length $line[0]);
    >
    > Never seen that construct before. Is it just some perlish shorthand
    > to get stuff on one line?


    See perldoc perlop "Logical And" and perldoc -f next.
    You should be able to figure it out.

    hp

    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
    Peter J. Holzer, Jan 19, 2014
    #2
    1. Advertising

  3. "Dave Saville" <> writes:
    > Came across this today
    >
    > print "1 OK\n" and next unless (length $line[0]);
    >
    > Never seen that construct before. Is it just some perlish shorthand
    > to get stuff on one line?


    In this case, it's just a verbose and somewhat byzantine way to express

    print("1 OK\n), next unless length($line[0]);

    Generally, the so-called 'logical operators', (||, &&, //, and, or)
    don't evaluate their right-hand arguments if the value of the combined
    expression is already known after evaluating the left-hand
    argument. This implies they can be used for flow-control in the sense
    that code making up the right-hand argument will only be executed when
    executeing the code of the left-hand argument wasn't sufficient to
    determine the result of the expression. The line you quoted is a bad
    example because the 'next' isn't really executed conditionally because
    the print-statement always returns true. A better example could be
    something like

    $age >= 18 or die("Won't sell this to you.");
    Rainer Weikusat, Jan 19, 2014
    #3
  4. * Rainer Weikusat wrote in comp.lang.perl.misc:
    >"Dave Saville" <> writes:
    >> Came across this today
    >>
    >> print "1 OK\n" and next unless (length $line[0]);
    >>
    >> Never seen that construct before. Is it just some perlish shorthand
    >> to get stuff on one line?

    >
    >In this case, it's just a verbose and somewhat byzantine way to express
    >
    >print("1 OK\n), next unless length($line[0]);


    >The line you quoted is a bad example because the 'next' isn't really
    >executed conditionally because the print-statement always returns true.
    >A better example could be something like
    >
    > $age >= 18 or die("Won't sell this to you.");


    Actually, `print` returns a true value only if successful and it might
    have failed for any number of reasons, like a closed socket, a full hard
    disk, and other IO problems like that.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
    25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
    Bjoern Hoehrmann, Jan 19, 2014
    #4
  5. Bjoern Hoehrmann <> writes:
    > * Rainer Weikusat wrote in comp.lang.perl.misc:
    >>"Dave Saville" <> writes:
    >>> Came across this today
    >>>
    >>> print "1 OK\n" and next unless (length $line[0]);
    >>>
    >>> Never seen that construct before. Is it just some perlish shorthand
    >>> to get stuff on one line?

    >>
    >>In this case, it's just a verbose and somewhat byzantine way to express
    >>
    >>print("1 OK\n), next unless length($line[0]);

    >
    >>The line you quoted is a bad example because the 'next' isn't really
    >>executed conditionally because the print-statement always returns true.
    >>A better example could be something like
    >>
    >> $age >= 18 or die("Won't sell this to you.");

    >
    > Actually, `print` returns a true value only if successful and it might
    > have failed for any number of reasons, like a closed socket, a full hard
    > disk, and other IO problems like that.


    But that's exceedingly unlikely because print just copies the output
    data into an internal buffer and I doubt that the intention behind the
    original code was to continue execution when print failed.
    Rainer Weikusat, Jan 19, 2014
    #5
  6. Στις 19/1/2014 17:02, ο/η Dave Saville έγÏαψε:
    > print "1 OK\n" and next unless (length $line[0]);







    use strict;
    use warnings;

    while (<DATA>) {
    chomp;
    my @line = split /,/;
    print "1 OK\n" and next unless (length $line[0]);
    print "id $line[1]\n";
    }

    __DATA__
    ,id00
    aaaaaaaaaa,id01
    bbbbbbbbb,id02
    cccccccc,id03
    ,id04
    ,id05
    George Mpouras, Jan 19, 2014
    #6
    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. suzy
    Replies:
    3
    Views:
    442
  2. BluDog

    Can Someone Please Explain...

    BluDog, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    476
    BluDog
    Oct 5, 2004
  3. Alan Silver
    Replies:
    1
    Views:
    478
    Alan Silver
    Jun 28, 2005
  4. KK
    Replies:
    2
    Views:
    546
    Big Brian
    Oct 14, 2003
  5. Kaye Ng
    Replies:
    8
    Views:
    175
    Josh Cheek
    Jun 8, 2010
Loading...

Share This Page