Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Martin Brown book page 33

by mnooning (Beadle)
on May 16, 2006 at 09:47 UTC ( #549699=perlquestion: print w/replies, xml ) Need Help??
mnooning has asked for the wisdom of the Perl Monks concerning the following question:

I am reading DeBUGGING PERL by Martin Brown. Page 33
sub first {print 'First' } sub second {print 'Second' } print(2,3,first,second);
The result is


which I have checked. It really is. So where does that
last digit '1' come from? Why isn't it just


Replies are listed 'Best First'.
Re: Martin Brown book page 33
by davorg (Chancellor) on May 16, 2006 at 10:05 UTC

    Actually, I get "FirstSecond2311".

    The extra 1s are the return values from the functions first and second. As they don't have explicit return statements, they both return the result of the last expression evaluated. In both cases, that's a call to print. And print returns 1 (i.e. true) if it prints successfully.

    For extra credit, explain why it doesn't print "23First1Second1" - which would seem to be the most obvious result.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Actually, I get "FirstSecond2311".
      $ perl -le 'print 0' 0 $ perl -le 'print print 0' 0 1 $ perl -le 'print print print 0' 0 1 1 $ perl -le 'print print print print 0' 0 1 1 1

      Hint: print does have a return value!

      For extra credit, explain why it doesn't print "23First1Second1" - which would seem to be the most obvious result.

      Because the arguments to print are all evaluated first, then printed. ie, it's effectively:

      @args = (2,3,first,second); print @args;

      So 'first' and 'second' are run (generating output), before the print command w/ the list is performed.

      Similarly, foreach (&func1, &func2) { ... } will run both 'func1' and 'func2' before starting the loop:

      Yes, FirstSecond2311. Me, too.
      So THAT is where the extra 1s come from!

      To rephrase what blazar, jhourcle and friedo (and the book) pointed out,
      the terms in the main print statements have a higher precedence then
      the plain numbers, and hence get evaluated first. The
      said "terms" are subroutine calls that themselves do
      a print, and that is why we see First and then Second being printed.
      Thanks for the extra credit question, too, as well as
      the answer.

      To comment on adrianh suggesting other books instead,
      the fact is I've read a ton of perl books. The
      debugging book just happens to be one I haven't
      read. In spite of all the Perl books I've read, the
      I could not seem to figure out the answer.
      Thanks again
Re: Martin Brown book page 33
by adrianh (Chancellor) on May 16, 2006 at 10:07 UTC
    I am reading DeBUGGING PERL by Martin Brown

    Personally I'd recommend putting it in the bin and going and buying a good book on Perl instead :-) It's one I would never recommend. Try the Llama book, or Peter Scott's Perl Debugged.

    So where does that last digit '1' come from? Why isn't it just

    I think you'll find that there are two "1"s at the end. They are the result of calling the first() and second() subroutines. If you don't have a explicit return the last value returned in the subroutine is returned. The print in first() and second() both return "1" after a successful print. So:



Re: Martin Brown book page 33
by friedo (Prior) on May 16, 2006 at 10:06 UTC
    There's actually two extra 1's on there, the result is


    That's because the function print returns 1 on success, just like many other Perl functions. Lacking an explicit return, statement, the first and second subs return the result of their last statement, which is 1 in both cases.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://549699]
Approved by McDarren
[Lady_Aleena]: shmem, geany on Debian jessie.
[shmem]: some experience with vim?
[Lady_Aleena]: None
[shmem]: I don't know geany, and don't know whether it has support for ctags.
[Lady_Aleena]: ctags?
[shmem]: ctags is a program which (recursively) extracts the symbols from source and stores them in a one-file database. This allows you to query the locations where these symbols (e.g. a subroutine name) are used anywhere in the source code tree...
[shmem]: ...from inside the editor.
[shmem]: apt-get install exuberant-ctags
[Lady_Aleena]: I think I heard vim has a big learning curve.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-04-27 12:07 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (505 votes). Check out past polls.