The problem is that the author of the article to which you refer is slightly confused himself. He offers this code and explanation:
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.
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.
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.
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.
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
|