Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: How to import "global" variables into sub-scripts from main script?

by Polyglot (Chaplain)
on Mar 22, 2021 at 15:16 UTC ( [id://11130129]=note: print w/replies, xml ) Need Help??


in reply to Re: How to import "global" variables into sub-scripts from main script?
in thread How to import "global" variables into sub-scripts from main script?

Just don't be surprised later when everything breaks and no one wants to touch the monster.

Whether it breaks or not, or even if I wrote it with the combined impeccable abilities of all you experts here, when I'm no longer able to maintain it, it is highly unlikely that anyone else will; not because they wouldn't want to, but because they don't know the first thing about programming, much less Perl. I honestly do not personally know another Perl programmer. I have never met one in my entire sojourn in this part of the world. My Perl books come from Amazon--from America; I don't recollect seeing any such in bookstores in my area. Perl may be a well-established fact in some parts, but not where I am. Here, people learn Java, or C++, or .NET, etc.

Your "horrible" possibility looks too scary for me. I don't dare try it.

Because I have never been able to grasp abstractions like OO, references, etc., I've never seen modules as an option for me. I can only do what I have ability for. If I don't understand something, even copy/pasting from someone else will almost never work for me, as I'm unable to adapt to my needs that which I do not understand in the first place. So I generally do all of my own programming, simple though it may be. For some blessed reason I do usually understand regex quite well--I guess it's not so abstract.

After a few minutes without strict I was able to find the troublespot and everything is working well again. Strict is still in use in my main script file, as it always was. It causes no problems there, but it throws a fit if brought into any of the sub-files.

...ACTUALLY, I just confirmed that redeclaring the globals in the sub-script still allows their values to pass from the main script, and that "strict" seems satisfied with that. Honestly, it smacks of overkill to me to have to declare variables repeatedly--if I were to add "strict" to my other files, the globals would have to be repeated in each of them too (I started with just the main MYSQL sub-script). AND, after adding all those global variables, strict had nothing at all to say--that was the only thing between the script running or not running. It was no idle "warning." It was a "kill script" level error to simply not have redeclared my global variables. I don't find this behavior helpful. I know, I'll probably get a ton of downvotes for speaking my mind, as I have with my other posts here, but I have never been a conformist for the sake of popularity. Now, if "strict" had actually said anything useful to me after I had gotten past the globals issue, i.e. if it had told me something about my code that had earlier escaped my notice, and that was an important weakness, I might have a different view. But, nope. It was just its own rigidity. Everything would have worked fine without it.

Blessings,

~Polyglot~

  • Comment on Re^2: How to import "global" variables into sub-scripts from main script?
  • Download Code

Replies are listed 'Best First'.
Re^3: How to import "global" variables into sub-scripts from main script?
by dsheroh (Monsignor) on Mar 23, 2021 at 08:31 UTC
    Honestly, it smacks of overkill to me to have to declare variables repeatedly
    One of the major functions of strict is to protect against typos in variable names. How can it detect a misspelled variable name if it doesn't know (via a declaration) what the correct name is?

    Your answer seems to be "it should know because I declared it in the master script", but what if I write a different master script which uses your auxiliary script without declaring that variable? Because you split things up into multiple scripts and incorporate them into the master using require, each one needs to be able to stand alone, and that includes making its own variable declarations, as well as setting its own pragmas (strict, warnings, etc.).

    if "strict" had actually said anything useful to me after I had gotten past the globals issue, i.e. if it had told me something about my code that had earlier escaped my notice
    Perl tries, but it can't read your mind. Judging by the additional diagnostic messages for the "not imported" errors, it seems that what Perl thinks went wrong is that you imported some_sub() from another module, then mistakenly referred to it as $some_sub. That's not what you were actually doing, of course, but, since it's what Perl thought you were doing, that's the problem it tried to help you solve.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130129]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found