Chas. Owens wrote:
On Sat, Jun 13, 2009 at 03:48, John W. Krahn<jwkr...@shaw.ca> wrote:
Chas. Owens wrote:
On Fri, Jun 12, 2009 at 20:24, Steve Bertrand<st...@ibctech.ca> wrote:
my $hours_used = ($hours_lookup->get_month_hours($dbh, $username,
$search_date, 'dialup'));
Why are you creating a list here? Just say
Do you mean the list ($dbh, $username, $search_date, 'dialup')? I think
that is required for the function to work correctly. Otherwise there is no
other list there, the external parentheses are superfluous and do not define
a list. A scalar rvalue enclosed in parentheses is just a scalar value.
No, it is a list of one item,
perldoc perlfunc
[ snip ]
Remember the following important rule: There is no rule that relates
the behavior of an expression in list context to its behavior in
scalar context, or vice versa. It might do two totally different
things. Each operator and function decides which sort of value it
would be most appropriate to return in scalar context. Some
operators return the length of the list that would have been
returned in list context. Some operators return the first value in
the list. Some operators return the last value in the list. Some
operators return a count of successful operations. In general, they
do what you want, unless you want consistency.
A named array in scalar context is quite different from what would
^^^^^^^^^^
at first glance appear to be a list in scalar context. You can’t
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
get a list like "(1,2,3)" into being in scalar context, because the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
compiler knows the context at compile time. It would generate the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
scalar comma operator there, not the list construction version of
the comma. That means it was never a list to start with.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
perldoc perlfaq4
[ snip ]
Data: Arrays
What is the difference between a list and an array?
An array has a changeable length. A list does not. An array is
something you can push or pop, while a list is a set of values.
Some people make the distinction that a list is a value while an
array is a variable. Subroutines are passed and return lists, you
put things into list context, you initialize arrays with lists, and
you foreach() across a list. "@" variables are arrays, anonymous
arrays are arrays, arrays in scalar context behave like the number
of elements in them, subroutines access their arguments through the
array @_, and push/pop/shift only work on arrays.
As a side note, there’s no such thing as a list in scalar context.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When you say
$scalar = (2, 5, 7, 9);
you’re using the comma operator in scalar context, so it uses the
scalar comma operator. There never was a list there at all! This
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
causes the last value to be returned: 9.
which in scalar context yields the last
There is no such thing as a list in scalar context (see above.)
item (which also happens to be the only item) just like any other
list. You are correct that they are superfluous, that is why I am
asking him why he is bothering to create the list with them.
Parentheses do not create a list, they are used to define precedence.
If the parentheses did not create a list then this code
my $undef = ();
Would produce an error because there is no scalar value to assign to
$undef. Instead, () is a list of no items which yields an undef in
scalar context. There is nothing magical about
There is no such thing as a list in scalar context (see above.)
my $scalar = (1);
that makes (1) not a list but a scalar instead, it follows the exact
same rules as all other lists: yield your last element in scalar
context. This is roughly the same as
There is no such thing as a list in scalar context (see above.)
my $s = @a[0];
It works, but is probably not what you meant to say.
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/