print function behavior

Discussion in 'Perl Misc' started by Rahul, Aug 10, 2005.

  1. Rahul

    Rahul Guest

    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
    Rahul, Aug 10, 2005
    #1
    1. Advertising

  2. Rahul

    Guest

    "Rahul" <> 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
    , Aug 11, 2005
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    wrote:
    > "Rahul" <> 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
    Rahul, Aug 11, 2005
    #3
  4. Rahul

    Guest

    "Rahul" <> wrote:
    > wrote:
    > > "Rahul" <> 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
    , Aug 11, 2005
    #4
  5. Rahul

    Rahul Guest

    wrote:
    > "Rahul" <> wrote:
    > > wrote:
    > > > "Rahul" <> 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
    Rahul, Aug 11, 2005
    #5
    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. Mantorok Redgormor
    Replies:
    70
    Views:
    1,724
    Dan Pop
    Feb 17, 2004
  2. keto
    Replies:
    0
    Views:
    897
  3. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    335
    David Cournapeau
    Dec 30, 2008
  4. Andreas Kraemer

    print stream behavior

    Andreas Kraemer, May 27, 2009, in forum: Python
    Replies:
    2
    Views:
    350
    Andreas Kraemer
    May 27, 2009
  5. Dave Slayton

    behavior of my print function call

    Dave Slayton, Feb 19, 2007, in forum: Perl Misc
    Replies:
    8
    Views:
    144
    Dave Slayton
    Feb 23, 2007
Loading...

Share This Page