Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

RE (tilly) 4: access to my variables from other subs

by tilly (Archbishop)
on Oct 21, 2000 at 18:47 UTC ( #37797=note: print w/replies, xml ) Need Help??

in reply to RE: RE (tilly) 2: access to my variables from other subs
in thread access to my variables from other subs

God status? All too human I am afraid!

You remind me of one of the key differences between Christian and Buddhist monks. Both have key figures in their religion that they revere. But where Christians set out to worship their founder, Buddhists seek to match what theirs did. Of course a Buddhist monk believes that they have it easier than Siddartha Gautama because the monk has the descriptions of those who went before to work from while Siddartha was working blindly. (Likewise a Christian monk believes that they have it easier than Christ did, but for rather different reasons.)

The point is that (whatever your religious beliefs) it is better to emulate the Buddhist monk. Don't deify heros, seek to build on what they discovered. :-)

Anyways to your problem. You are asking to be able to break the encapsulation provided by my in some controlled way. You can't. (At least not without mucking in the internals.) And there are good reasons for that.

The key principle here is that allowing one programmer to make assumptions about another programmer's code leads to unmaintainable messes. For instance take your template idea. Suppose that everyone was using this template. I have some code that uses it, code in which I am using $x and $y for other reasons. (Those are generally bad names, but I have used them before when the parallel to Cartesian coordinates helped me keep straight the grid problem I was thinking about.)

Unknown to me someone decides to add an equation solver to your template. This will look for variables like $x, and $y, then try to solve equations. This programmer "knows" that those are bad variable names, and adds them assuming that nobody has used them. I know that code worked, and I have no reason to expect that it would suddenly change.

A couple of months later in testing it turns out that my grid code is horribly broken. Now I have to go back to my code and do diagnostics on the whole project before I track down how it worked, that it worked, and finally spiral outwards until I find the the template change which broke it. (Assuming that I ever think of looking there.) Once found the person who broke it now has a ton of code that depends on that template. So I have to go back and rename all of my variables, leading to further integration and testing delays.

This is not good from a scheduling point of view, nor from the episodes of ballistic programmers which the imagination can easily fill in.

A far better approach is to have my lexicals be truly private so that my code cannot be broken by the addition of something to the template, and have the template export an object (data structure, whatever) that nicely encapsulates its functionality in such a way that you can add neat stuff, I don't have to care about new potential functionality being added because it cannot come into conflict with me.

This is why there is a nice warning in perldoc about local not being what you want and my being better.

This is also the software engineering principle known as "Loose Coupling" that you see at the top of the page from time to time...

  • Comment on RE (tilly) 4: access to my variables from other subs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://37797]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2017-01-17 07:22 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (152 votes). Check out past polls.