Re: Is Perl still worth learning?



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
.



Relevant Pages

  • 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: 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: 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: Sorting and Writing Effecient Code
    ... date and extracts the file names that start with the current date, ... loops through the contents of those files to extract data in a scalar ... You seem to be mis-using the term "scalar context", ... Should I be reading the variables I have extracted into an array? ...
    (comp.lang.perl.misc)