The problem is that the author of the article to which you refer is slightly confused himself. He offers this code and explanation:

NOTE: In C++ there are cases where the calling code can "reach into" the function via the returned pointer or reference. This is appearantly not true of passed back scalars. Check out this co +de: $GlobalName = "Clinton"; sub getGlobalName     {     return($GlobalName);     } print "Before: " . &getGlobalName() . "\n"; $ref = \&getGlobalName(); $$ref = "Gore"; print "After: " . &getGlobalName() . "\n"; #All print statements printed "Clinton" I have been unable to hack into a subroutine via its scalar return. If you know of a way it can be done, please let me know, as this would + be a horrid violation of encapsulation.

The reason that

$$ref = "Gore";

doesn't appear to change anything, is because the author is confused about what he is taking a reference to when he does

$ref = \&getGlobalName();

He appears to think that this would give him reference to $GlobalName, whereas in fact it gives him a reference to a copy of $GlobalName! And when he assigns through that reference, he is modifying the copy, not the original. This can be seen clearly if you add a couple of lines to his code.

$GlobalName = "Clinton"; sub getGlobalName { return($GlobalName); } print "Before: " . &getGlobalName() . "\n"; $ref = \&getGlobalName(); print '>>', $$ref, "\n"; # Added $$ref = "Gore"; print '>>', $$ref, "\n"; # Added print "After: " . &getGlobalName() . "\n"; # Produces the output Before: Clinton >>Clinton >>Gore After: Clinton

As you can see, the function is returning a copy of $GlobalName, And the calling code takes a reference to this copy. It then modifies the copy indirectly through the reference, but goes on to print out the value of the original, by calling the function again and getting another copy. The first copy still exists and has been modified but was simply never being used.

To achieve what the author apparently thought he was trying to do, the subroutine would need to return a reference to the original variable.

$GlobalName = "Clinton"; sub getGlobalName { return($GlobalName); } sub getRefToGlobalName { return \$GlobalName; } print "Before: " . getGlobalName() . "\n"; $ref = getRefToGlobalName(); $$ref = "Gore"; print "After: " . getGlobalName() . "\n"; #prints Before: Clinton After: Gore

As you can see, when the new function is called, it returns a reference to $GlobalName, and when you assign via this reference, you modify that (original) variable. Then when you call the original getGlobalName() function, the second time, it does return (a copy of) the modifed value.

Of course, there is no need to have two functions.

$GlobalName = "Clinton"; sub getRefToGlobalName { return \$GlobalName; } print "Before: " . ${ getRefToGlobalName() } . "\n"; $ref = getRefToGlobalName(); $$ref = "Gore"; print "After: " . ${ getRefToGlobalName() } . "\n"; # Also prints Before: Clinton After: Gore

The syntax in the print statements has become slightly more complex, but it demonstrates the point. And this can be simplifed further as once you have the reference, there is no longer any need to call the function to access the variable.

$GlobalName = "Clinton"; sub getRefToGlobalName { return \$GlobalName; } $ref = getRefToGlobalName(); print "Before: " . $$ref . "\n"; $$ref = "Gore"; print "After: " . $$ref . "\n"; # Also prints Before: Clinton After: Gore

Overall, the authors statement about "reaching into a subroutine" is extremely confusing (not to say inaccurate:), and on the strength of that and several other bits I read in the section on subroutines, I couldn't recommend the material, though I have to say from my quick scan that he does seemed to have expended considerable effort on his content. Unfortunately, he seems to be suffering from a few misconseptions which spoil his efforts.

If your looking for good material to help you get started with perl, the material on this site is seen and reviewed by a large number of experienced and eagle-eyed people. Mistakes and misconceptions are not allowed to persist for more than a few minutes around here (as I know from (too frequent) experience:)

Rather than re-list all the good materials, I'll just recommend you read and following the links in this recent node by PodMaster. If you follow all those links and read the material therein, you'll get a very good start on learning perl.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


In reply to Re: Reference to a function by BrowserUk
in thread Reference to a function by Anonymous Monk

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.