Re: Is Perl still worth learning?
- From: "John W. Krahn" <someone@xxxxxxxxxxx>
- Date: Mon, 23 Apr 2007 06:07:49 GMT
Logan Shaw wrote:
Phlip wrote:
Perl is needlessly complex for the simplest situations. For example
(IIRC), the internal behavior of a function changes depending on
whether you assign its return value into a scalar or an array.
That gets the gist of it, although the actual behavior is slightly
different:
(1) It depends on whether you evaluate the function a list context
or a scalar context,
or in void context.
$ perl -le'
sub test { print defined wantarray ? wantarray ? "LIST" : "SCALAR" : "VOID" }
test;
my $x = test;
my @x = test;
'
VOID
SCALAR
LIST
not what you assign it to (that's just a
special case of list or scalar context).
(2) The context does not influence the internal behavior of the
function in any underhanded, sneaky way. Instead, the function
can call "wantarray" and find out what context it is being
call in.
[ snip ]
For example, in Perl, there is a "grep" function. This function^^^^^
filters a list, creating a new list with only certain members of
the original list. In list context, "grep" returns the list.
In array context, "grep" instead returns the number of elements
scalar
that would have been in the new list had it been built.
This is nice because it's a useful thing to do. The "grep"
program (as opposed to the internal Perl "grep" function) has
a "-c" argument for doing the exactly analogous thing, because
it's nice to be able to say
grep -c pattern filename
rather than
grep pattern filename | wc -l
So here, the distinction between list and scalar contexts is
allowing you to put two similar and closely-related operations
under the same name. That makes Perl ornate, but if you don't
dislike ornament, so what?
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.
- References:
- Is Perl still worth learning?
- From: flifus
- Re: Is Perl still worth learning?
- From: Puckdropper
- Re: Is Perl still worth learning?
- From: Phlip
- Re: Is Perl still worth learning?
- From: Logan Shaw
- Is Perl still worth learning?
- Prev by Date: Re: Python has truly triumphed
- Next by Date: Re: a simple model checker
- Previous by thread: Re: Is Perl still worth learning?
- Next by thread: newbie help for ads search application
- Index(es):
Relevant Pages
|