in reply to subroutine syntax

What if you want to use sub example not only for $xyz but also $abc, $etc, etc? When i first started perl i would just add another sub with $abc in place of $xyz and things got sloppy very quickly. Then i learned how to pass variables and write much more generalized subroutines.

Like this:

$xyz = 15; $abc = 30; &example($xyz); &example($abc); sub example { local $var = @_; $a = $var + 49; print $var; print $a; }
See how elegant that is? Whatever variable you pass to example gets assigned to $var and operated upon.

enjoy - epoptai

Replies are listed 'Best First'.
Re: Re: subroutine syntax
by marius (Hermit) on Dec 11, 2000 at 03:37 UTC
    For that matter, let's take a look at uses of the return function. (perldoc -f return)
    my $xyz = 15; my $var = &example($xyz); print &example($xyz); sub example { my $tmp = shift || 0; my $tmp += 49; return $tmp }
    Now, you can set the global $var to by equal to the return value of the subroutine. Also, you can just print the return of the subroutine if you like. return is a very handy tool when it comes to subroutine programming and cleaning up perl code.

    -marius