[Pdx-pm] evil @ARGV syntax

Austin Schutz tex at off.org
Tue Jul 19 13:45:35 PDT 2005

On Tue, Jul 19, 2005 at 12:14:03PM -0700, Michael G Schwern wrote:
> On Tue, Jul 19, 2005 at 11:05:05AM -0700, Randall Hansen wrote:
> >      if( $#ARGV + 1 < 1 || $#ARGV + 1 > 2 ) {
> > 
> > which seems like a complicated way of writing:
> > 
> >      if( @ARGV < 1 or @ARGV > 2 ) {
> > 
> > i've tested this and found it to be true.  my question is, why on  
> > earth would someone write this the first way?  it's one thing not to  
> > understand scalar context; it seems entirely insane to add those ones  
> > before the less|greater than tests.
> > 
> > am i missing an idiom?  historical reason?  does @ARGV behave oddly  
> > enough in certain contexts that these gymnastics are necessary?
> Lots of poorly written Perl books (which is to say most of them prior to 2000)
> never clearly teach $num_things = @things.  See 
> http://mungus.schwern.org/~schwern/litmus_test/index.html
> for some examples.
> Adding one to $#ARGV is to make the operation more like how people think.  

	I have to wonder what sort of people you've been associating with
that think this way. :-)
	IIRC it's some sort of a shell script holdover. I never use it
because imo it's heinously ugly syntax. Plopping a comment character in
the middle of the symbol to determine not the length, but the index of the
last entry? Yurgh.
	What happens if there are no entries in the array? -1? Ugh.
	How about the position of the last element of an anonymous array?
$#$array?  Triple yurgh.
	But... if you have a 2 dimensional array you can do cool things like
$last_element = $a->[$#$a]->[$#{${*}a->[$#$a]}]; Let those java weenies parse

	On the other hand, apparently the whole scalar vs. array context
can be confusing to newbies. Maybe it would be nice if the length() function
returned the number of elements in an array, matching other languages. It's
my understanding it's supposed to do just that in perl6.


More information about the Pdx-pm-list mailing list