--- Jeff Pang <[EMAIL PROTECTED]> wrote:
> Hi,you can do it by passing the vars to the subroutine like:
> 
> my $var = 20;
> &routine($var);
> 
> sub routine {
>     my $var = shift;
>     print $var;
> }
> 

I'll second this recommendation because it makes the subroutines more
flexible (what if they want a different variable in the future?).  The
only change I would make is to strip the leading ampersand on the sub
call:

  routine($var);

Using a leading ampersand leads to very strange bugs if you call a
subroutine with a leading ampersand and no parentheses because the
current value of @_ is then passed to the calling subroutine.  It's
then very easy, when refactoring, to use &subname instead of
&subname().  Avoid that ampersand unless you know exactly why you're
using it :)

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to