http://www.perlmonks.org?node_id=156435


in reply to What is the difference between 'local' and 'my'?

Hi.

When you type local variable name, the var is actually global. It can be seen by any part of your script. If you type my varname, the varname is private. It can only be seen by that 'namespace'. Example:

#!/usr/bin/perl -w use strict; my $name; print "Enter your name: "; chomp( $name = <STDIN> ); print "Hi $name\n"; &Greeting; sub Greeting { my $name; print "Hi $name!"; }

The Greeting subroutine has it's own scalar variable called $name. Whatever it contains has nothing to do with the outside 'world'. Using local is a little more complex than that, but hopefully this will help. A great explanation can be found in Learning Perl 3rd edition by O'Reilly publishing.

-DK