Re: populating a hash slice from a filehandle



From: Bryan R Harris <Bryan_R_Harris@xxxxxxxxxxxx>
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.

Almost. It still depends on the left hand side. Try this:

@a = (10,20,30);
@b = (40,50,60);

$s = (@a,@b);
print "$s\n";
#versus
($s) = (@a,@b);
print "$s\n";


In the first case the @a and @b are evaluated in scalar context. Even
though they are enclosed in braces.

Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

.



Relevant Pages

  • Re: About Lagrange in GR II
    ... Yes, but remember that's the classical Lagrangian, not the Lagrangian density of GR. ... local and coordinate dependant unit, ... The action itself is a scalar. ... I suspect you are confusing the context of GR with the context of Quantum Field Theory in which there are often "running coupling constants". ...
    (sci.physics.relativity)
  • Re: Is Perl still worth learning?
    ... the internal behavior of a function changes depending on whether you assign its return value into a scalar or an array. ... It depends on whether you evaluate the function a list context ... Getting back to Perl and its notion of scalar vs. list context, ... For example, in Perl, there is a "grep" function. ...
    (comp.programming)
  • Re: first post
    ... If I rewrite it as Perl suggests, ... context on the assignment while the scalar $headerforces scalar ...
    (perl.beginners)
  • Re: first post
    ... If I rewrite it as Perl suggests, ... The hash slice @headeracts the same as a list and enforces list context on the assignment while the scalar $headerforces scalar ...
    (perl.beginners)
  • Re: populating a hash slice from a filehandle
    ... John W. Krahn wrote: ... The left hand side of the assignment determines context so the @l2r ... so I'll pull another scalar off the list and assign it ... in list context instead of scalar context. ...
    (perl.beginners)