<?xml version="1.0" encoding="windows-1252"?>
<node id="738748" title="Re: Context tutorial" created="2009-01-24 23:09:05" updated="2009-01-24 23:09:05">
<type id="11">
note</type>
<author id="381608">
ikegami</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;p&gt;&lt;i&gt;To force a list context where there wouldn't be one otherwise, assign to an empty list.&lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;Then how come Example 4 is

&lt;c&gt;
my ($x) = localtime();     # Example 4
&lt;/c&gt;

&lt;p&gt;and not

&lt;c&gt;
my $x = () = localtime();
&lt;/c&gt;

&lt;p&gt;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).

&lt;p&gt;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.

&lt;p&gt;To force list context, one adds parens around the LHS of the assignment or use a list slice.

&lt;c&gt;
my ($x) = f();        # first element of list is assigned
my $x = ( f() )[0];   # same, but overkill
my $x = ( f() )[-1];  # last element of list is assigned
my $x =  ( f() )[0]   # logical-or of the first elements
      || ( g() )[0];  #    returned by f() and by g()
&lt;/c&gt;

&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: Added example.
</field>
<field name="root_node">
738558</field>
<field name="parent_node">
738558</field>
</data>
</node>
