To force a list context where there wouldn't be one otherwise, assign to an empty list.
Then how come Example 4 is
my ($x) = localtime(); # Example 4
my $x = () = localtime();
I've never see assigning to an empty list used to force a list context. Assigning to an empty list is used to count the number of elements in a list (which has nothing to do with context).
In the case of badly written code, assigning to the empty list can differentiate between void and list context, but it never makes sense to use it to differentiate between scalar and list context as you show.
To force list context, one adds parens around the LHS of the assignment or use a list slice.
my ($x) = f(); # first element of list is assigned
my $x = ( f() ); # same, but overkill
my $x = ( f() )[-1]; # last element of list is assigned
my $x = ( f() ) # logical-or of the first elements
|| ( g() ); # returned by f() and by g()
Update: Added example.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
Outside of code tags, you may need to use entities for some characters:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||