Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Polyglot (Chaplain)
on Mar 22, 2021 at 10:25 UTC ( [id://11130094]=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?

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~

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

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:28 UTC
    > 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~

        > When one uses a module, naturally that module has its own autonomy with what it requires and uses.

        No idea what you mean, a use is basically a require + import at compile-time

        Like documented:

        BEGIN { require Module; Module->import( LIST ); }

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

Re^5: How to import "global" variables into sub-scripts from main script?
by haukex (Archbishop) on Mar 22, 2021 at 16:29 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found