On Fri, Jun 20, 2008 at 4:52 PM, Bryan R Harris <[EMAIL PROTECTED]> wrote: > >> Bryan R Harris wrote: >>> >>> John W. Krahn wrote: >>>> >>>> Bryan R Harris wrote: >>>>> >>>>> John W. Krahn wrote: >>>>>> >>>>>> The left hand side of the assignment determines context so the @l2r{...} >>>>>> part. >>>>> >>>>> That strikes me as odd... When perl goes to populate @l2r{"a","b"}, it >>>>> seems to me that it would go through this process: >>>>> >>>>> - I have a slice here, so I'll loop over the slice elements >>>>> - The first is "a", so I'll pull a scalar off the list and assign it to >>>>> $l2r{"a"} >>>>> - The second is "b", so I'll pull another scalar off the list and assign >>>>> it >>>>> to $l2r{"b"} >>>>> - Remaining scalars in the list are discarded >>>> >>>> Correct, except for the loop part. >>>> >>>>> Why would $l2r{"a"} here be considered list context? >>>> >>>> It isn't, unless it's written as ( $l2r{"a"} ), then it's a list with >>>> one element. >>> >>> So I still don't understand what about @l2r{"a","b"} makes it evaluate the >>> first (<FILE>... in list context instead of scalar context. >> >> The '@' sigil at the front of the variable name says that it is either >> an array or a slice and so it forces list context on the right hand side >> of the assignment. > > I think it finally clicked! > > It makes more sense to me that (<FILE>,<FILE>) is kind of the same thing as > saying (@a,@b). In list context @a returns the array as a list, but in > scalar context @a returns the number of elements. Obviously (@a,@b) returns > the union of the two lists, not two scalars. "<FILE>" is treated the same > way. >
Bryan, That isn't really as obvious as you think. There are probably situations where (@a, @b) would be a list of scalars. I'll say it again: the function of the parentheses in a particular situation (grouping, precedence, capturing, list assignment) is assign *after* context is determined. Context has to do with the result you are asking for, not the data you are inputting. Just because you are working with a list doesn't mean your expression will be evaluated in list context. The important thing to keep sight of is that it is what happens on the lefthand side of the equals sign that forces the context, here, and there is nothing on the righthand side to explicitly force a change. You can think of this as an "apples to apples" issue. Lists can be assigned to lists, and processed as lists. When you perform an operation on a list, you aren't always iterating over the elements in sequence. A list is a collection of scalars, true, but it's also a fundamental data type in its own right. A slice is a type of list, so Perl looks at the slice assignment and says "I need a list". It then evaluates the expression on the right with an eye toward making a list. *Then* it worries about how the elements of the list on the right will be affected by whatever has to happen to the list on the left. HTH, -- j -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!