Re: Style question: map versus foreach

From: Ben Morrow (usenet_at_morrow.me.uk)
Date: 11/12/03


Date: Wed, 12 Nov 2003 04:45:50 +0000 (UTC)

Steve Grazzini <grazz@pobox.com> wrote:
> Ben Morrow <usenet@morrow.me.uk> wrote:
> > Steve Grazzini <grazz@pobox.com> wrote:
> >> I suppose it would also be possible to say that the subroutine
> >> returns the *result* of evaluating a list in void context. And
> >> likewise, that this one returns the result of evaluating an array
> >> in scalar context:
> >>
> >> % perl -Ds -le 'sub f { @INC } scalar f()'
> >>
> >> But I'd rather say that this returns an array.
> >
> > But that would be quite wrong. In Perl, a function *cannot* return
> > an array, only a list if in list context or a scalar if in scalar.
>
> That's true, of course. I think I got it right earlier: f() returns
> the result of evaluating @INC in scalar context when it's called in
> scalar context, and the result of evaluating @INC in list context when
> it's called in list context. Condensing this to "f() returns @INC" is
> less precise, but maybe it's close enough to be useful, assuming that
> we all know you can't *really* return an array from a subroutine and
> do "push foo(), $elt" or whatever.

OK.

>
> > For instance, what about this
> >
> > % perl -le'sub f { caller } print scalar f'
> >
> > ? Would you say this returns 'the caller function', which is then
> > evaluated in scalar context?
>
> Well, no. That would be silly. :-)

Well.... I don't quite see as it's sillier than saying the sub above
returns @INC. This returns the result of evaluating caller() in list
context when in list context, and the result of evaluating caller() in
scalar context when in scalar context.

Both are simply a constructive use of sloppy language.

> > according to you, the result of 'evaluating a list in scalar context'
> > is its last element, which is not what is returned.
>
> That's because (as you must know) caller() doesn't return a list in
> scalar context. Since there's no list, this doesn't have anything
> to do with the behavior of a list in scalar context.

But my point was that neither was there a list in the @INC
case. '@INC' doesn't return a list in scalar context either: it
returns the number of elements it contains.

And, to get back to the original argument :), neither does map()
return a list in void context. It returns nothing.

<snip pods about lists and scalar ,>
> But the bit about the "scalar comma operator" seems to be misguided.
> If any of the earlier elements of those lists were variables, a list
> really would be created at runtime.
>
> % perl -Dts -le 'scalar($$, 42, 43)'
>
> EXECUTING...
>
> =>
> (-e:0) enter
> =>
> (-e:0) nextstate
> =>
> (-e:1) pushmark
> => *
>
> Here's the list (note the missing "42").
>
> (-e:1) gvsv(main::$)
> => * IV(14592)
> (-e:1) const(IV(43))
> => * IV(14592) IV(43)
>
> Here's the op that evaluates the list in scalar context.
>
> (-e:1) list
>
> And here's the result.
>
> => IV(43)
> (-e:1) leave

Interesting.....

So a better statement of all this is perhaps:

There is no 'scalar comma operator'. The comma operator builds a
list. A list evaluated in scalar context evaluates each of its members
in void context; except the last which is evaluated in scalar context
and returned. This may be optimized away at compile time (as usual).

Hmmm. This applies at complie time, not at run time. In

sub c {...}

my $x = (c, c);

the elements of the list are 'call sub c', rather than the results of
those calls; rather like returning 'the caller function' above :).
Very functional, and not at all suitable as a way of explaining it to
people :).

Ben

-- 
   Although few may originate a policy, we are all able to judge it.
                                             - Pericles of Athens, c.430 B.C.
  ben@morrow.me.uk


Relevant Pages

  • Re: Smart assignment
    ... is a list slice so the regular expression is in list context but the slice is ... a single value so the expression is a scalar. ... The || operator will only work with scalar values, not with lists, so this ... works because the list has been converted to a scalar with the list slice. ...
    (perl.beginners)
  • Re: Smart assignment
    ... is a list slice so the regular expression is in list context but the slice ... a single value so the expression is a scalar. ... The || operator will only work with scalar values, not with lists, so this ... works because the list has been converted to a scalar with the list slice. ...
    (perl.beginners)
  • Re: populating a hash slice from a filehandle
    ... The returns a single line in scalar context and ... The first two lists are ... to ensure that the is evaluated in scalar context. ... so I'll pull another scalar off the list and assign it ...
    (perl.beginners)
  • Re: populating a hash slice from a filehandle
    ... a list of all lines in a list context. ... The first two lists are ... so I'll pull a scalar off the list and assign it to ... so I'll pull another scalar off the list and assign it ...
    (perl.beginners)
  • 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)