why doesn't "sort X" give an error

380 views
Skip to first unread message

Joe Wells

unread,
Apr 20, 1990, 8:50:05 PM4/20/90
to
(Perl 3.0 patchlevel 18)

Why does perl accept this short program without an error:

sort X

What does perl think "X" is? A filehandle? A format?

Puzzled,

--
Joe Wells <j...@bu.edu>
jbw%bucsf....@cs.bu.edu
...!harvard!bu-cs!bucsf!jbw

Larry Wall

unread,
Apr 23, 1990, 12:15:50 PM4/23/90
to
In article <JBW.90Ap...@bucsf.bu.edu> j...@bucsf.bu.edu (Joe Wells) writes:
: (Perl 3.0 patchlevel 18)

:
: Why does perl accept this short program without an error:
:
: sort X
:
: What does perl think "X" is? A filehandle? A format?

The short answer is that it is a subroutine name, and since you have it
a null array to sort, it never evaluates the subroutine name to find out
it doesn't exist. See the man page.

The long answer is that there are a class of operators in perl called
list operators, and any of them can take an optional word or scalar variable
before the list. Some of them, such as "print", take it to be a filehandle.
"sort" takes it to be a subroutine name. "exec" and "system" take it to
be a scalar holding the real name of the program to exec. Some list operators
such as "unlink" and "kill" ignore it entirely!

I know it's weird, but it does make it easier to write poetry in perl. :-)

Larry

Joe Wells

unread,
Apr 23, 1990, 5:48:41 PM4/23/90
to

In article <JBW.90Ap...@bucsf.bu.edu> j...@bucsf.bu.edu (Joe Wells) writes:
: sort X
:
: What does perl think "X" is? A filehandle? A format?

The long answer is that there are a class of operators in perl called


list operators, and any of them can take an optional word or scalar

^^^^^^^^^^^^^^^^^^^^^^^


variable before the list. Some of them, such as "print", take it to be

^^^^^^^^


a filehandle. "sort" takes it to be a subroutine name. "exec" and
"system" take it to be a scalar holding the real name of the program to
exec. Some list operators such as "unlink" and "kill" ignore it
entirely!

I'm confused. This seems like a highly ambiguous specification. For
example, how does perl know in the following examples whether an optional
word or *scalar variable* is present:

sort $X; # this could be optional variable followed by empty list, right?
print $X; # ditto
unlink $X; # this could be a noop according to the above

Larry Wall

unread,
Apr 23, 1990, 8:18:42 PM4/23/90
to
In article <JBW.90Ap...@bucsf.bu.edu> j...@bucsf.bu.edu (Joe Wells) writes:
: I'm confused. This seems like a highly ambiguous specification. For

: example, how does perl know in the following examples whether an optional
: word or *scalar variable* is present:
:
: sort $X; # this could be optional variable followed by empty list, right?
: print $X; # ditto
: unlink $X; # this could be a noop according to the above

I believe that's one of those places where a shift-reduce conflict
"does the right thing".

Larry

Reply all
Reply to author
Forward
0 new messages