[mercury-users] Perl shift || $defalt

Richard A. O'Keefe ok at atlas.otago.ac.nz
Fri Apr 27 09:08:33 AEST 2001


Terrence Brannon <princepawn at earthlink.net>
provided a textbook example of why Mercury-lovers are not terribly
eager to marry the demon Perl with the angel Mercury:

	In Perl, it is very easy to say the following:
	
	  the value of x is equal to the first data item in ARGV, 
	  or if ARGV is not defined then use the value 12:
	
	$x = shift || 12;

The problem is that the code fragment shown does NOT do what the
explanation says it does.

Here is the definition from "man perlfunc":
     shift ARRAY

     shift   Shifts the first value of the array off and returns
             it, shortening the array by 1 and moving everything
             down.  If there are no elements in the array,
             returns the undefined value.  If ARRAY is omitted,
             shifts the @_ array within the lexical scope of
             subroutines and formats, and the @ARGV array at file
             scopes or within the lexical scopes established by
             the eval '', BEGIN {}, END {}, and INIT {}
             constructs.  See also unshift(), push(), and pop().
             Shift() and unshift() do the same thing to the left
             end of an array that pop() and push() do to the
             right end.

Terrence Brannon did not mention that
 - there is no notion of doing something if ARGV is *undefined*,
   but rather if it is *empty*, which is a different thing in Perl.
 - the array is *changed* as a side effect
 - in most uses, shift accesses and changes *@_*, not *ARGV*.  Drop
   that code fragment into a subroutine and watch it stop working!

Let's unpack that code fragment (assume a global context):

    if ARGV is empty then
       (x,ARGV') <- (12, ARGV)
    else
       (x,ARGV') <- (first ARGV, rest ARGV)
    endif

or

    :- mode p(out, in, out).
    p(12, [], []).
    p(X, [X|R], R).

Terrence Brannon went on to say that

	Thus, a program can either take values on STDIN or use hardcoded
	defaults with relative ease.
	
However, this code fragment has nothing whatsoever to do with STDIN.
He's talking about command-line arguments.

Please don't think I'm picking on Terrence Brannon.  My point is simply
that someone who apparently *likes* Perl is confused by it.

Perl is hard because of the things it hides.

Mercury is hard because of the things it *shows*.

As for easily using hard-coded defaults or over-rides from the command line,
even Ada and Eiffel make that easy.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list