It has nothing to do with functions...
\() returns a list of references to the variables in the list, so in this case a list of three references to the scalars. (Think (\"one",\"two",\"three")). But you assign it to a scalar... (So it becomes the last entry the list, which is 'three')
Example 2's $ref2 is just a reference to @list, so it indeed will return ARRAY when you ask what is refers to...
And Example 3's \() will give you a list of references to what's in the parentheses. But @list in list context, gets it's list expanded, so it's a list of three scalars again and assigning it to a scalar will give you the last entry 'three' again, thus ref $ref3 still is 'SCALAR'.
use Data::Dumper;
print Dumper($ref1), "\n";
print Dumper($ref2), "\n";
print Dumper($ref3), "\n";
Should clear things up...
Update
In case of the $n = qw(one two three) makes $n eq 'three' not being clear, refer to chapter 2.3.4 of Programming Perl... It boils down to the list 'knowing' that it's being used in scalar context, and thus evaluating to the value of the last element, just like in C.
An array is slightly different from a list; an array returns it's size when evaluated in scalar context. Only in example 2 do you make the list an array by assigning it to @list.
-
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.
|