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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.