ralph wrote:

Other than the placeholder situation, I
don't understand when one could do the
'is given($_)' and not do the ($_ = $_).
Any time that the caller's topic isn't supposed to be
explicitly passed as an argument, but is still used within
the subroutine.

For example, the Class::Contract module provides a subroutine
like this:

	# Perl 5 code
	
	sub check(\%;$) {
		my $state = !$#_ ? 0 : $_[1] ? 0 : 1;
		my $forclass = $_;
		defined $forclass or croak;

		$_[0]->{$forclass} =
		       	bless { prev=>$no_opt{$forclass}, forclass=>$forclass},
			      'Class::Contract::FormerState';
		$no_opt{$forclass} = $state;
	}

It is passed a hash (that is used to store objects it generates) and a scalar
that indicates whether checking is to be enabled or disabled. It uses the
callers $_ (which is specifically *not* passed as an argument) to determine
what class to turn checking on or off for. That (slightly odd) interface
allows users of the module to write very tidy code:

	check %cache, $on_or_off
		for qw/Class1 Class2 Class3/;

C<check> needs the callers $_, but doesn't (want to) have it passed as an
argument. So a ($_ = $_) argument won't work.

And, yes, I could make it an optional argument, but them I have no way of
preserving my chosen interface. Or porting that code cleanly.

Besides all that, ($_ = $_) where $_ means something different from $_
is Just Plain Wrong. ;-)


Damian

Reply via email to