print function behavior

R

Rahul

Greetings,

In a loop if I use 'print "sth", next;', only next is executed and
"sth" is not printed. However when I use 'print ("sth"), next;', "sth"
is printed as expected but with a warning stating that print was
interpreted as a function. My question is : when i use the former style
of calling the print function why does it not print "sth"?

perldoc perlfunc mentioned that print returns a boolean value. Why does
the print "sth" statement fail to execute when I use a ',' and a 'next'
after it?

my test script is as follows

#!/usr/bin/perl

use strict;
use warnings;

my $result = 0;
for (1..10){
print "$result ";
#$result = print ("$_ "), next; #prints 1 1 1 2 1 3... as expected
$result = print "$_ ", next; #prints 0 0 0 0 0 0... why?
}

TIA.

regards,
rahul
 
X

xhoster

Rahul said:
Greetings,

In a loop if I use 'print "sth", next;', only next is executed and
"sth" is not printed. However when I use 'print ("sth"), next;', "sth"
is printed as expected but with a warning stating that print was
interpreted as a function. My question is : when i use the former style
of calling the print function why does it not print "sth"?

You are telling print to print the "sth", followed by the return value
of next. That means Perl needs to evaluate the next to get it's return
value. But of course next doesn't have a return value, because it doesn't
return--it causes flow control to go to the next iteration of the loop.

Xho
 
R

Rahul

You are telling print to print the "sth", followed by the return value
of next.


Yes but print does'nt print "sth" even if I use it in void context.
That means Perl needs to evaluate the next to get it's return
value. But of course next doesn't have a return value, because it doesn't
return--it causes flow control to go to the next iteration of the loop.

That may be possible but still why is next executed and why is the
print "sth" a no op?

regards,
rahul
 
X

xhoster

Rahul said:
Yes but print does'nt print "sth" even if I use it in void context.

I have no idea what that means.
That may be possible but still why is next executed

What to you expect next to do, other than to do what it is supposed to do?
and why is the
print "sth" a no op?

Let's say you have code that says:

die "Goner";
print "sth";

Do you understand why this doesn't print "sth"? Why do you think your
situation is meaningfully different than the above?

If you don't want next to be considered one of the terms in print's
argument list, then use ';' rather than ',' between them. Or use the
function-like print, as you discovered. Or to avoid the warnings, use
parenthesis around the entirety of print and its desired arguments, to
avoid the warning.

Xho
 
R

Rahul

I have no idea what that means.

Sorry. I read your earlier reply again and now I understand!
What to you expect next to do, other than to do what it is supposed to do?


Let's say you have code that says:

die "Goner";
print "sth";

Do you understand why this doesn't print "sth"? Why do you think your
situation is meaningfully different than the above?

If you don't want next to be considered one of the terms in print's
argument list, then use ';' rather than ',' between them. Or use the
function-like print, as you discovered. Or to avoid the warnings, use
parenthesis around the entirety of print and its desired arguments, to
avoid the warning.

Makes perfect sense! Thank you Xho!

regards
rahul
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top