Re: Style question: map versus foreach
From: Ben Morrow (usenet_at_morrow.me.uk)
Date: 11/12/03
- Next message: Shaun Clowes: "Re: Win32::OLE and CDO Message Filters"
- Previous message: Uri Guttman: "Re: simplify this if loop"
- In reply to: Steve Grazzini: "Re: Style question: map versus foreach"
- Next in thread: Abigail: "Re: Style question: map versus foreach"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Shaun Clowes: "Re: Win32::OLE and CDO Message Filters"
- Previous message: Uri Guttman: "Re: simplify this if loop"
- In reply to: Steve Grazzini: "Re: Style question: map versus foreach"
- Next in thread: Abigail: "Re: Style question: map versus foreach"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|