Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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...

In reply to RE (tilly) 4: access to my variables from other subs by tilly
in thread access to my variables from other subs by joe

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-05-27 21:57 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (98 votes). Check out past polls.