[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] [waffle] File::Sort



Sorry for the waffle below... I must have woken up the wrong side of bed
this afternoon, 'cos my brain's running a little slow and uneconomically.

At 11:31 +1100 on 20/12/97, Chris Nandor wrote:
>At 12.28 12/19/97, Brian L. Matthews wrote:
>>You can sort of already do that:

>>$r = sub { $a cmp $b };
>>sort {&$r} @array;

I got confused when reading this, in conjunction with perlref.pod... I just
couldn't "get" what was happening from looking at the code.

In perlref.pod, the part about referencing methods, it suggests using the
form &{$r} for code references. Any time I use "&$r" in my code, MacPerl
seems to execute the code referenced by $r.

So... to avoid getting me any more confused, I wrote up a little program...

  #!perl
  use strict;

  my $routine = sub { my ($a,$b,@c) = @_; ($a == $b)?1:0 };
	# Swallow up extranneous arguments in @c.

  sub check {
	my @args = @_;
	my $comparator = @args[0];	# The comparator routine
	my $first = @args[1];		# The values
	my $second = @args[2];		#  to compare

	return &{$comparator}($first,$second);
		# Straight out of perlref.pod
  }

  my $x = check( $routine, 1, 2);
  my $y = check( $routine, 1, 1);
  print "$x, $y\n";

When I run this, the result is (as expected) "0, 1".  The bit that really
confuses me (even after reading perlref.pod a couple of times) is the
difference between the use of {} in $hash{key} = $value, and &{$coderef}.
The nearest I can "get" it is that {} means "dereference" or "lookup". So
in a hash table, you "lookup" the key to get the value. For a code
reference, you "lookup" the anonymous code table to find the definition.
Does anyone out there want to share their cognitive meaning for {} ?

>Yes, that is what I want.  Matthias pointed it out to me; I don't know why
>I hadn't tried that.   ...

Chris (& Vicki?)... don't forget to mention this (passing code references
to subroutines) in your book :)  I haven't looked at the site for about two
months... so I haven't a clue where it would fit in. Yes, it *is* mentioned
in the perlref.pod file, but unless you see an application of it "in real
life", you may not "get" it, or figure out new uses for it.

Regards,
Alex Satrapa

Windows 95: n. 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can't stand 1 bit of competition.



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch