Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > print function behavior

Reply
Thread Tools

print function behavior

 
 
Rahul
Guest
Posts: n/a
 
      08-10-2005
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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-10-2005
"Rahul" <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      08-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> "Rahul" <(E-Mail Removed)> wrote:
> > 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.



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

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-11-2005
"Rahul" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > "Rahul" <(E-Mail Removed)> wrote:
> > > 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.

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


I have no idea what that means.

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


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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      08-11-2005
(E-Mail Removed) wrote:
> "Rahul" <(E-Mail Removed)> wrote:
> > (E-Mail Removed) wrote:
> > > "Rahul" <(E-Mail Removed)> wrote:
> > > > 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.

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

>
> I have no idea what that means.
>
> >
> > > 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.


Sorry. I read your earlier reply again and now I understand!

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


Makes perfect sense! Thank you Xho!

regards
rahul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
behavior of my print function call Dave Slayton Perl Misc 8 02-23-2007 03:49 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments