Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
After giving what I believed to be a complete answer yesterday, I realized that it still wasn't quite correct. It is true that subroutines return lists not arrays, but when called in scalar context they dont necessarily return the scalar value of that list.

The most obvious example of this is localtime() whose return values in scalar and list context have little to do with one another. Another example happens to be fetchrow_array()

From the DBI pod:

In a scalar context, fetchrow_array returns the value of the first field. An undef is returned if there are no more rows or if an error occurred. Since that undef can't be distinguished from an undef returned because the first field value was NULL, you should exercise some caution if you use fetchrow_array in a scalar context.
Using my logic, in scalar context we'd get the last field not the first.

The moral of the story is that the behavior of a subroutine in scalar context can't be determined from its list context return value.

Here are three subs that return the same values in list context, but behave differently in scalar context:

#!/usr/bin/perl -wT use strict; sub test1 { return @{[ qw(A B C) ]}; } sub test2 { return qw(A B C); } sub test3 { return wantarray ? qw(A B C) : 'I like jellybeans'; } print "List context\n", " test1: ", join(', ',test1()), "\n", " test2: ", join(', ',test2()), "\n", " test3: ", join(', ',test3()), "\n", "\nScalar context\n", " test1: ", scalar test1(), "\n", " test2: ", scalar test2(), "\n", " test3: ", scalar test3(), "\n"; =OUTPUT List context test1: A, B, C test2: A, B, C test3: A, B, C Scalar context test1: 3 test2: C test3: I like jellybeans

-Blake


In reply to Re4: MySQL / Perl Question by blakem
in thread MySQL / Perl Question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found