#!/usr/bin/perl
#use Mooix::Thing;
run sub {
	my $this=shift;
	%_=@_;
	my $session=$_{session};
	my $object=$_{direct_object};
	my $field=$_{field};
	my $fieldfile=$object->fieldfile($field);
	if (! $fieldfile) {
		fail "No such method.";
	}
	elsif (! -f $fieldfile) {
		fail "Cannot list that.";
	}
	my $opaque=".$field-opaque";
	if ($object->$opaque) {
		fail "Sorry, that is an opaque method and cannot me listed.";
	}
	
	open (METHOD, $fieldfile) || fail "Unable to read method.";
	my @lines=<METHOD>;
	close METHOD;
	
	# Guard against accidentially viewing a binary.
	if (-x $fieldfile && $lines[0] !~ /^#!.*\/(.+)/) {
		$fieldfile.=".c";
		if (-e $fieldfile) {
			open (METHOD, $fieldfile) || fail "Unable to read method.";
			@lines="That is a binary method, but this might be its source code:",
			push @lines, <METHOD>;
			close METHOD;
		}
		else {
			fail "That is a binary method, and the source is not available.";
		}
	}

	# Success.
	$session->page(map { s/\n$//; $_ } @lines);
}
