Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

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


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

Well, since you say it's only a warning--not a show stopper, I tried this at the top of that file:

our $DEBUG = $DEBUG || 0;

It didn't work. I still get a whole slew of $DEBUG related errors in the log. So much for attempting to declare them and just take the first error on the chin to save the rest.

Strict will just have to go this time...it seems to oppose all of what caused me to fall in love with Perl in the first place. I was sick of all the constraints of Pascal, and the strict types...and Perl just seemed to know when something should be a number, or a string, etc. without needing tedious conversion routines or such a strict (strait-jacket) approach.

Blessings,

~Polyglot~

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

Replies are listed 'Best First'.
Re^5: How to import "global" variables into sub-scripts from main script?
by choroba (Cardinal) on Mar 22, 2021 at 10:26 UTC
    > It didn't work

    our $DEBUG;
    could have worked, though.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Redeclaring a variable means its value is reset. This means a change to $DEBUG in the main script would have no effect in the companion file. I don't relish the idea of having to go and manually set debug variables in every one of the companion files (I currently have sixteen of them, and the list may grow yet more), save them, then reupload ALL of them to the server every time I want to debug something--then of course reverse the process to stop debugging. Joy! Doesn't that sound Perlish? ...all instead of just importing a variable. I truly have a hard time wrapping my mind around this major lapse in the Perl capabilities.

      Blessings,

      ~Polyglot~

        our is not a declaration. It's different to local and my. Yes, it's confusing.

        Have you tried it? For example:

        1.pl

        #!/usr/bin/perl use warnings; use strict; our $x = 12; require './2.pl'; print "Done\n";

        2.pl

        #!/usr/bin/perl use warnings; use strict; our $x; print $x, "\n";

        Calling 1.pl:

        12 Done

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

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

    No recent polls found