#!/usr/local/bin/perl -w # A somewhat silly program, demonstrating the use of lists to pass # parameters. Also demonstrates a subroutine returning a value. # # The program adds the numbers given as command line arguments. The # arguments are passed via the list @ARGV. Note that parameters passed # to a subroutine are passed via the list @_. use strict; # Prototype sub add; # main { my $sum; my $count = $#ARGV + 1; $sum = add(@ARGV); print "\nThe sum of your $count numbers is $sum.\n"; exit 0; } # Adds the numbers in the list passed in and returns the sum. sub add { my $i; my $last = $#_; my $sum = 0; for ($i = 0; $i <= $last; ++$i) { $sum += $_[$i]; } return $sum; }