Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'm hoping for an easy, simple answer on this one!

I'm not working with modules--I'm just dividing my script into sections for organizational purposes. It really helps me find things. Declaring variables with our in the main script has always worked just fine. Using or requiring other modules from the main script has also never been a problem, with the possible exception of use utf8; which seems to address the specific file in which it is found. Having put

use strict; use warnings; use CGI;

...etc. all in the main script, and only

use utf8; use CGI qw(-utf8); binmode(STDOUT, ":utf8");

...in the sub-scripts, the sub-scripts have worked just fine. I assumed, as errors were few, that I had been going along just fine and that all was working well.

Until today.

After years of working on this script oblivious to any issue here (could a perl update have done something?), I just discovered that use strict; was ONLY working within the context of the main file, and the require'd files were running bare. This discovery came about as I was trying to debug some recent (rather major) changes I had made to the code, and traced it back to no help from strict.

So, like any good perl coder, I promptly added use strict; to my sub-file--only to cause total mayhem in the error logs, mostly in the form of:

#[Slightly sanitized and greatly truncated...] [Mon Mar 22 01:03:25.470815 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$DEBUG" is not imported at ../MyScript2 +021/MyScript_MySQL.pl line 6.: /var/www/[domain_name_redacted].com/cg +i/MyScript-2021.pl [Mon Mar 22 01:03:25.471028 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$DEBUG" is not imported at ../MyScript2 +021/MyScript_MySQL.pl line 556.: /var/www/[domain_name_redacted].com/ +cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471169 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$newuserpass" is not imported at ../MyS +cript2021/MyScript_MySQL.pl line 560.: /var/www/[domain_name_redacted +].com/cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471225 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$membertable" is not imported at ../MyS +cript2021/MyScript_MySQL.pl line 561.: /var/www/[domain_name_redacted +].com/cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471363 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$newuserpass" is not imported at ../MyS +cript2021/MyScript_MySQL.pl line 577.: /var/www/[domain_name_redacted +].com/cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471415 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$membertable" is not imported at ../MyS +cript2021/MyScript_MySQL.pl line 578.: /var/www/[domain_name_redacted +].com/cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471603 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$DEBUG" is not imported at ../MyScript2 +021/MyScript_MySQL.pl line 619.: /var/www/[domain_name_redacted].com/ +cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471690 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$username" is not imported at ../MyScri +pt2021/MyScript_MySQL.pl line 630.: /var/www/[domain_name_redacted].c +om/cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471733 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$digest" is not imported at ../MyScript +2021/MyScript_MySQL.pl line 631.: /var/www/[domain_name_redacted].com +/cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471776 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$table" is not imported at ../MyScript2 +021/MyScript_MySQL.pl line 632.: /var/www/[domain_name_redacted].com/ +cgi/MyScript-2021.pl [Mon Mar 22 01:03:25.471820 2021] [cgi:error] [pid 1602] [client x.x.9 +3.1:65279] AH01215: Variable "$language" is not imported at ../MyScri +pt2021/MyScript_MySQL.pl line 633.: /var/www/[domain_name_redacted].c +om/cgi/MyScript-2021.pl

As those are intended to be global variables which must pass to the various subroutines in the associated files of the same script, I thought it should be easy to just import them.

Think again!

There seems no way to do so--at least, I have yet to find one. I tried using an "import($DEBUG)" -- is this a "pragma"? -- but it did not work. And when I google "perl import variables", all the results talk about EXPORTING them, not importing.

What's a fellow supposed to do?

My sub-scripts just have grouped sets of subroutines, making it easier for me to locate a particular one and then, after any changes, only uploading that portion--saving bandwidth. For example, here's a piece of one of the require'd files:

use strict 'vars'; use utf8; use CGI qw(-utf8); binmode(STDOUT, ":utf8"); #import($DEBUG); # THIS DIDN'T WORK. #CURRENT SUBS IN THIS FILE sub usrupdatePassword; sub updatePassword; sub updateUserRank; sub connectdb; sub connectdb_login; sub create_login_table; sub query_login_table; sub login; 1; #---------------------------------------- sub usrupdatePassword { # A USER FUNCTION if (($DEBUG) && ($DEBUG<12)) { print qq|<p><span style="font-size: + 16pt;color: #008800">SUB:</span> usrupdatePassword\n</p>| }; my $user = shift @_; my $curpass = shift @_; my $newpass = shift @_; my $valid = &checkLogin_mysql($user,$curpass); my $salt = &getRandomSalt; if (($user) && ($newpass) && ($valid)) { $salt = crypt($newuserpass, $salt); my $statement = qq| UPDATE $membertable SET Password = '$salt' WHE +RE Username='$user'; |; &connectdb_login($statement,'updatePassword'); return "Password updated!"; } else { return "Sorry. Incorrect credentials."; } } #END SUB usrupdatePassword sub updatePassword { # AN ADMIN FUNCTION if (($DEBUG) && ($DEBUG<12)) { print qq|<p><span style="font-size: + 16pt;color: #008800">SUB:</span> updatePassword\n</p>| }; my $authuser = shift @_; my $newpass = shift @_; my $valid = &checkUserExists_mysql($authuser); my $salt = &getRandomSalt; if (($authuser) && ($newpass) && ($valid)) { $salt = crypt($newuserpass, $salt); my $statement = qq| UPDATE $membertable SET Password = '$salt' WHE +RE Username='$authuser'; |; &connectdb_login($statement,'updatePassword'); return "Password updated!"; } else { return "Sorry. Incorrect credentials."; } } #END SUB updatePassword # . . . [TRUNCATED]

How would I legitimately use the $DEBUG variable passed from the main portion of the script? And, honestly, why must a variable be imported that is already declared to be within the "global" scope, and which works just fine without "strict"?

(In my personal experience, "strict" is like proper electrical ground wires: It has generally hurt me more than it has helped. (Yes, I've had a UPS backup damaged by lightning only because it was well grounded--within weeks of when I'd installed the copper grounding rod myself. After that, I never used ground again, and never had another problem in years. Go figure.) I know everyone says you always must use it--just like they'd say about ground wires. But . . . I'm about ready to give up on strict. /rant)

Blessings,

~Polyglot~


In reply to How to import "global" variables into sub-scripts from main script? by Polyglot

Title:
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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

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

    No recent polls found