Perl subroutines

Jason Holtzapple ml at bitflip.net
Fri Jul 9 10:04:15 MST 2010


On 07/09/2010 09:54 AM, Paul Mooring wrote:
> I'm working on a perl script, and I have a lot more experience in ruby
> than I do in perl.  As far as I can tell there is no way to explicitly
> define arguments for a subroutine in perl, for example
> 
> sub addNums(number1, number2) {
>   sum = number1 + number2;
> 
>   return sum;
> }

perl does support prototypes for functions, probably more loosely than
you are used to though.

> sub addNums {
>   number1 = shift;
>   number2 = shift;
>   sum = number1 + number2;
> 
>   return sum;
> }
> 
> is there anyway to do this, and if not does anyone have some advice on
> a way to make this a bit more readable in perl?

You could write it like

sub addNums {
    sum = $_[0] + $_[1];

    return sum;
}

I'm not sure how much more readable that is. There are also ways to
raise an exception if you have the wrong number of arguments.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 554 bytes
Desc: OpenPGP digital signature
URL: <http://lists.PLUG.phoenix.az.us/pipermail/plug-discuss/attachments/20100709/1f7f8a45/attachment.pgp>


More information about the PLUG-discuss mailing list