#!/usr/bin/perl -w
# Author: Matthew Evett
# Copyright 2004.
#
# This program demonstrates that Perl passes paramters by reference.
use strict;

#The initial value of the actual parameter is 1.
my($arg) = 1;
print "In main, the variable arg is $arg before any subroutine calls.\n";
change($arg);

#After the subroutine is called, however, the actual parameter's value is changed!
print "In main, the actual parameter, arg, is $arg after the subroutine call.\n";
print "Now we call a subroutine that retrieves its parameter into a local variable.\n";
change2($arg);

#After this subroutine call, $arg will remain unchanged.
print "In main, after that subroutine call, the actual parameter, arg, is $arg.\n";

#Notice that the subroutine accesses the parameter directly via the @_ variable.
#I.e., it does not place the value in a variable.
sub change {
    print "In change, the formal parameter is $_[0].\n";
    $_[0] += 1;
    print "In change, after adding, the parameter is $_[0].\n";
}

#In this subroutine, we stored the formal parameter in a variable.  The assignment
#statement makes a copy, so changes to this variable do not affect the formal 
#parameter, and thus don't affect the actual parameter either.
sub change2 {
    my($theArg) = @_ ;
    print "In change2, the formal parameter is $theArg.\n";
    $theArg += 1;
    print "In change2, after adding, the formal parameter is $theArg.\n";
}


