Please explain "and next"

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

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

Dave Saville

Re: Please explain "and next"

Quoted text here. Click to load it

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


   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   |         | die Satzbestandteile des Satzes nicht mehr
__/   | | zusammenpaßt. -- Ralph Babel

Re: Please explain "and next"

On 19/01/14 15:02, Dave Saville wrote:
Quoted text here. Click to load it

Well, in a sense.  It saves coding this

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

In fact you don't need those brackets round the test condition either.

Now to really understand why it works, do what Peter suggests and look  
up the "and" operator (and how it's different from &&).


Henry Law            Manchester, England

Re: Please explain "and next"

Quoted text here. Click to load it

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.");


Re: Please explain "and next"

* Rainer Weikusat wrote in comp.lang.perl.misc:
Quoted text here. Click to load it

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 · ·
Am Badedeich 7 · Telefon: +49(0)160/4415681 ·
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 ·

Re: Please explain "and next"

Quoted text here. Click to load it

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.

Re: Please explain "and next"

Thanks all.

Dave Saville

Re: Please explain "and next"

Στις 19/1/2014 17:02, ο/η Dave Saville έγραψε:
Quoted text here. Click to load it

use strict;
use warnings;

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


Site Timeline