G'day pritesh,
It seems the answers you've already received cover what you're asking. However, I felt it was important to advise that this is a situation you should avoid in the first place.
Variables with file scope, regardless of whether they're lexical or dynamic, suffer all the same classic problems as global variables: action at a distance; unclear logic; hard to debug; problematic refactoring; and so on.
In the following demonstration script, I've reproduced all of the types of actions you're attempting, without any variables with file scope. Things to note in particular:
Here's the code:
#!/usr/bin/env perl -l use strict; use warnings; { my $val = 10; print "INIT: $val"; show_value($val); change_value(\$val, 200); show_value($val); unrelated_value(); show_value($val); print "LAST: $val"; } sub show_value { my ($val) = @_; print '* Show Value'; print "Value: $val"; return; } sub change_value { my ($val_ref, $new_value) = @_; print "* Change Value: $$val_ref to $new_value"; show_value($$val_ref); $$val_ref = $new_value; show_value($$val_ref); return; } sub unrelated_value { my $val = 42; print '* Unrelated Value'; change_value(\$val, 999); return; }
Output:
INIT: 10 * Show Value Value: 10 * Change Value: 10 to 200 * Show Value Value: 10 * Show Value Value: 200 * Show Value Value: 200 * Unrelated Value * Change Value: 42 to 999 * Show Value Value: 42 * Show Value Value: 999 * Show Value Value: 200 LAST: 200
— Ken
In reply to Re: Perl Variables storage location and scope question.
by kcott
in thread Perl Variables storage location and scope question.
by pritesh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |