User Functions


Beginning | Previous | Next

Local Variables

C and C++ limits the scope of variables to the main() and user written functions. A variable "shoe_size" declared in a C/C++ function has a lifespan restricted to the invocation and exit of that function. By default, Perl variables and functions have visibility everywhere inside a package (to be covered later).

To limit the visibility of variables to functions, use the "local" and "my" qualifiers, such as:

sub func_name { my ($local1, $local2); local ($local3, @local4); # code that does useful things }

The difference between "local" and "my" is that "my" variables only have visibility in the function where there are declared, whereas "local" variables will continue to have visibility in functions that are called from the function they are defined in.

The formal definition says that "my" variables are lexically scoped and "local" variables are dynamically scoped. Use these terms to impress your friends.

As a simple example:

#!d:/usr/local/perl/bin/perl -w &test_scope; sub test_scope { my $my_var = 10; local $local_var = 20; &inc_vars; print "\$my_var = $my_var, \$local_var = $local_var\n"; } sub inc_vars { $my_var++; $local_var++; } produces this output: d:/home/khamsi/tmp> my.pl Identifier "main::my_var" used only once: possible typo at my.pl line 16. $my_var = 10, $local_var = 21 d:/home/khamsi/tmp>

General Syntax

The general format for writing your own functions is as follows: sub func_name { my ($arg1, $arg2, $arg3) = @_; # @_, a list, holds all passed vars my ($local1, $local2); # make some local variables # code that does useful things return ($local1, $local2); # return these two variables }

Function Invocation

The invocation for our example above looks like: $x = 1; $y = 2; $z = \*LOG_FILE; # a reference to LOG_FILE ($return1, $return2) = &func_name ($x, $y, $z);

Passing and Returning Non-Scalar Varaibles - The Wrong Way

If you did: $scalar = 10; @list = (20, 30, 40); &take_list_and_scalar (@list, $scalar); # other stuff sub take_list_and_scalar { my (@array, $num) = @_; print "\@array = @array, \$num = $num\n"; }

the result would be:

@array = 20 30 40 10, $num =

Bummer. The problem is the variable "@array" sucks up all the elements form the array "@_" leaving nothing for the poor scalar "$num". The same type of thing would happen on returning parameters from the function.

Passing and Returning Non-Scalar Varaibles - The Right Way

The right way is to use hard references. Not only do they work, but they sound better to management. References work better not only to fix this arguemnt passing problem, but they are more efficient since they do the pass by reference, not value, thing.

Rewriting our example above we get:

$scalar = 10; @list = (20, 30, 40); &take_list_and_scalar (\@list, $scalar); # other stuff sub take_list_and_scalar { my ($array_ref, $num) = @_; print "\@array = @$array_ref, \$num = $num\n"; }

the result would be:

@array = 20 30 40, $num = 10

as expected.

Beginning | Previous | Next
Last Modified: $Date: 1997/09/18 09:17:07 $