[Chicago-talk] Arguments

Steven Lembark lembark at wrkhors.com
Sun Jan 18 23:40:40 CST 2004



-- "Randal L. Schwartz" <merlyn at stonehenge.com>

>>>>>> "petemar1" == petemar1  <petemar1 at perlmonk.org> writes:
>
> petemar1> Why not? What should I use?
>
> Nothing.
>
> sub foo {
>   my $first_arg = shift; # comment about first arg
>   my $second_arg = shift; # comment about second arg
>   my @remaining = @_; # comment about remaining
>
>   ....
>
> }

Bit of context:

The calling standard in Perl is based on LISP: call by
reference with a list. The list elements are scalars.
This is one of the things that makes Perl so flexable;
its price is that you have to sanity-check arguments
explicitly in the code. This takes away a heavily used
crutch of pseudo-lazy development: letting the compiler
catch bad calls for you. The catch in Perl is that the
calling standard is sufficiently flexable that the
compiler probably isn't the right level to have this
stuff caught at.

If you want a bit more boilerplate use:

sub foo
{
	@_ or croak "Bogus foo: called without arguments";

	my $this = shift or croak "Bogus foo: false 'this'";
	my $that = shift or croak "Bogus foo: false 'that'";

	$this > 0 or croak "Bogus foo: $this <= zero";

	...
}

If the cost of making these tests is too expensive (e.g.,
a tight loop in a job that runs more than 1728000 times
a day then look up Class::Contract (which can easily be
turned off at compile time for better performance with
full call-level checking during development).


--
Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                            +1 888 359 3508



More information about the Chicago-talk mailing list