#!/usr/bin/perl -w # EXAMPLE06 -- this script demonstrates passing arguments to subroutines. # (bmy, 6/8/09) #------------------------------------------------------------------------------ # REFERENCE SECTION #------------------------------------------------------------------------------ require 5.003; # Need this version of Perl or newer use strict; # "IMPLICIT NONE" -- force all declarations to be explicit #------------------------------------------------------------------------------ # SUBROUTINE SECTION #------------------------------------------------------------------------------ sub oneArgument($) { # Perl passes arguments to subroutines in one long array which is named # @_. Here we have to split the arguments off from the @_ input array. my ( $arg1 ) = @_; # Print arguments print "\#\#\# In routine oneArgument! \#\#\#\n"; print "The 1st argument is $arg1.\n"; # Return normally return(0); } #------------------------------------------------------------------------------ sub twoArguments($$) { # Extract individual arguments from @_ input array my ( $arg1, $arg2 ) = @_; # Print arguments print "\#\#\# In routine twoArguments! \#\#\#\n"; print "The 1st argument is $arg1.\n"; print "The 2nd argument is $arg2.\n"; # Return normally return(0); } #------------------------------------------------------------------------------ sub threeArguments($$$) { # Extract individual constituents from @_ input array my ( $arg1, $arg2, $arg3 ) = @_; # Print arguments print "\#\#\# In routine threeArguments! \#\#\#\n"; print "The 1st argument is $arg1.\n"; print "The 2nd argument is $arg2.\n"; print "The 3rd argument is $arg3.\n"; # Return normally return(0); } #------------------------------------------------------------------------------ sub main() { # Example of passing arguments to subroutines # Error check # of arguments if ( scalar( @ARGV ) == 1 ) { &oneArgument( @ARGV ); } elsif ( scalar( @ARGV ) == 2 ) { &twoArguments( @ARGV ); } elsif ( scalar( @ARGV ) == 3 ) { &threeArguments( @ARGV ); } else { print "Usage: example06 ARG1 [ ARG2 [ ARG3 ]]\n"; exit(1); } # Return normally return(0); } #------------------------------------------------------------------------------ # EXECUTABLE SECTION #------------------------------------------------------------------------------ # Call the main subroutine # User-written subroutines are denoted by "&" &main(); # NOTE: $? is the error code variable. It denote exit($?);