Re: Is Perl still worth learning?
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Sun, 22 Apr 2007 22:21:58 -0500
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, 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.
This ridiculous feature maintains backwards compatibility with idioms that should never have been used.
I'm not aware of any previous construct that the scalar/list context
distinction seeks to be backwards compatible with. There may be
one, but to me it's not really important what the history is. The
important thing is whether the construct is useful.
If scalars and arrays were first-class objects, we wouldn't need all this clutter.
Here we get to the real issue. If you believe that languages must
be stripped down to their bare essentials, containing only the things
that absolutely must be there in order to get the job done and doing
everything in the most regular, uniform manner possible, you will
absolutely hate Perl.
My view is that that is mostly an aesthetic preference. Sure, there
are languages that have purely bone-headed design elements. But
minimalism and clean lines aren't the only way to design something.
It's really a matter of whether you like ornament. This is evident
from looking at architecture: compare modern architecture (such
as the stereotypical glass box skyscraper) with designs that have
more of a historical continuity. For example, compare La Grande
Arche ( http://en.wikipedia.org/wiki/Grande_Arche ) with the
Arc de Triomphe ( http://en.wikipedia.org/wiki/Arc_de_Triomphe ).
One of them is definitely simpler and the other is more ornate.
Which one is "better"?
Getting back to Perl and its notion of scalar vs. list context, I
agree that it isn't strictly a necessary language feature. But
it really isn't that different from function overloading. Both
are mechanisms that allow you to choose different code to execute
despite using the same function name. Neither is as completely
transparent as a language like C where one function name equals
one function and one behavior. But both allow you to put similar
things under the same name.
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
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?
- Logan
.
- Follow-Ups:
- Re: Is Perl still worth learning?
- From: John W. Krahn
- Re: Is Perl still worth learning?
- References:
- Is Perl still worth learning?
- From: flifus
- Re: Is Perl still worth learning?
- From: Puckdropper
- Re: Is Perl still worth learning?
- From: Phlip
- Is Perl still worth learning?
- Prev by Date: Re: Python has truly triumphed
- Next by Date: South Florida Software Symposium - May 18 - 20, 2007
- Previous by thread: Re: Is Perl still worth learning?
- Next by thread: Re: Is Perl still worth learning?
- Index(es):
Relevant Pages
|