FryingFinn has asked for the wisdom of the Perl Monks concerning the following question:

I trying to get an application to set a couple of Global (?) variable that will be used in package subroutines. I thought I could use  $main::DEBUG my test main is
#!/usr/bin/perl ## this is perl5.16 my $DEBUG = 1233; my $VERBOSE=5; use strict; use warnings; use Getopt::Long; my $Usage = "this is Usage"; Getopt::Long::GetOptions( 'd=i' => \$DEBUG, 'v=i' => \$VERBOSE ) or die "Incorrect u +sage! $Usage\n"; print "in MAIN DEBUG is $DEBUG \n"; print "In Main VERBOSE is $VERBOSE \n"; use lib "../lib"; use myApp; myApp::testprint(); myApp::testprint2();
The code for myApp.pm is
package myApp; my $DEBUG = $main::DEBUG; my $VERBOSE = $main::VERBOSE; print " myApp.pm has DEBUG [$DEBUG]\n"; print " myApp.pm has VERBOSE [$VERBOSE]\n"; 1; sub testprint { print "in myApp::testprint DEBUG is [$DEBUG]\n"; print "in myApp::testprint VERBOSE is [$VERBOSE]\n"; } sub testprint2 { my $DEBUG = $main::DEBUG; my $VERBOSE = $main::VERBOSE; print "in myApp::testprint2 DEBUG is [$DEBUG]\n"; print "in myApp::testprint2 VERBOSE is [$VERBOSE]\n"; }
The results.
./xVERBOSE myApp.pm has DEBUG [] myApp.pm has VERBOSE [] in MAIN DEBUG is 1233 In Main VERBOSE is 5 in myApp::testprint DEBUG is [] in myApp::testprint VERBOSE is [] in myApp::testprint2 DEBUG is [] in myApp::testprint2 VERBOSE is []
any help would be appreciated tks gerry

Replies are listed 'Best First'.
Re: variables passed from main to package
by AnomalousMonk (Archbishop) on Feb 22, 2018 at 02:06 UTC

    In what I think is your  xVERBOSE source file, change

    #!/usr/bin/perl my $DEBUG = 1233; my $VERBOSE=5; ...
    to
    #!/usr/bin/perl our $DEBUG = 1233; our $VERBOSE=5; ...
    (untested) and see what happens.

    The thing to understand is that a lexical my variable is not a package-global variable. You have code in  myApp.pm like
        my $DEBUG   = $main::DEBUG;
    but the package-global  $main::DEBUG is nowhere declared and is certainly never initialized | assigned any value.

    Update 1: To put it another way, the lexical (my) variable
        my $DEBUG = 1233;
    defined in (what I think is) the  xVERBOSE file is "global" only in the sense that it is in existence and "visible" from the point of declaration in  xVERBOSE until the end of that file. The lexical  $DEBUG symbol defined by the above statement is not visible in any other file or in any superior scope. Maybe take a look at some of the articles in Variables and Scoping, especially 'our' is not 'my' and Coping with Scoping.

    Update 2: Some code to consider:

    c:\@Work\Perl\monks>perl -wMstrict -le "{ package Foo; my $foo = 'pure lexical defined in ' . __PACKAGE__; our $foo = 'package global of ' . __PACKAGE__; } ;; print 'in package ', __PACKAGE__; ;; my $foo = 'pure lexical defined in ' . __PACKAGE__; my $ref_to_foo = \$foo; print qq{A: $foo}; ;; our $foo = 'package global of ' . __PACKAGE__; ;; print qq{B: $foo}; print qq{C: $main::foo}; print qq{D: $Foo::foo}; print qq{E: $$ref_to_foo}; " in package main A: pure lexical defined in main B: package global of main C: package global of main D: package global of Foo E: pure lexical defined in main
    The lexical  $foo symbol defined and initialized in package  Foo is inaccessible (except by reference or perhaps by Deep Magick) once the scope of the  Foo package ends. Similarly, the lexical  $foo symbol defined and initialized in package  main is inaccessible (except by reference) once the masking package-global  $foo (the our variable) is defined.


    Give a man a fish:  <%-{-{-{-<