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

> I don't have a script named "main"

But you have a package called "main" (unless you declare a package in your main script).

If you have no idea what Perl would do, try it. You already know how to create a Perl script and run it.

> That just looks tacky and ugly

Yes, it looks ugly, because it is ugly.

Note that Variable "$x" is not imported is not an error, it's a warning. It's explained in perldiag:

(S misc) With "use strict" in effect, you referred to a global variable that you apparently thought was imported from another module, because something else of the same name (usually a subroutine) is exported by that module. It usually means you put the wrong funny character on the front of your variable.

Which doesn't seem helpful, so your confusion is understandable. It seems the explanation needs an addition (volunteers?)

Update: The required sub-scripts should have failed to compile under strict. If you're using do instead of require, you should check $@ and $!.

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

Replies are listed 'Best First'.
Re^4: How to import "global" variables into sub-scripts from main script?
by Polyglot (Chaplain) on Mar 22, 2021 at 10:25 UTC

    Update: The required sub-scripts should have failed to compile under strict. If you're using do instead of require, you should check $@ and $!.

    Remember, I wasn't using strict in the sub-files before. I had assumed that when I said "use strict;" in the main file, and then was bringing in the other subroutines via "require subfile.pl;" that the "strict" had applied to the entire global context. Remember, the global variables are all correctly shared among the files--so why wouldn't strict be shared, too?

    In any case, I had no issues compiling until I added "strict" to the sub-file.

    I'm not using "do" anywhere in the entire script at this point.

    Blessings,

    ~Polyglot~

      > why wouldn't strict be shared

      It would make using any non-strict module from a strict script impossible.

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

        I'm not useing a module (.pm); I'm requireing a companion perl script (.pl). When one uses a module, naturally that module has its own autonomy with what it requires and uses.

        Blessings,

        ~Polyglot~

      the global variables are all correctly shared among the files--so why wouldn't strict be shared, too?

      Because strict, warnings, and many other pragmas have a lexical scope. If used at the very top of the file, their scope is limited to that file. do, require, and use create a new lexical scope for the files they execute. Only evaled code inherits the strict and other pragmas from the surrounding lexical scope.

        > Only evaled code inherits the strict and other pragmas from the surrounding lexical scope.

        and because of the lexical scoping he can also deactivate most pragmas like strict before eval to avoid that problem

        { no strict; eval $CODE; }

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^4: How to import "global" variables into sub-scripts from main script?
by Polyglot (Chaplain) on Mar 22, 2021 at 10:14 UTC

    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~

      > 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~