Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^6: Global variables in Perl

by taioba (Novice)
on Jun 04, 2010 at 21:46 UTC ( #843216=note: print w/ replies, xml ) Need Help??


in reply to Re^5: Global variables in Perl
in thread Global variables in Perl

OK, here you go. A few adjustments: ConfigThisJunk.pm is actually ExportScalar.pm and the script name is ImportTest.pl. The package is in the folder 'D:/Marcos/Perl/MSB/SS' in my Windows machine. Hence, your line got modified to:

 perl -wle"use lib 'D:/Marcos/Perl/MSB'; use SS::ExportScalar; print $INC{'SS/ExportScalar.pm'};"

and that resulted in

D:/Marcos/Perl/MSB/SS/ExportScalar.pm

And then the results of the type statements:

type ExportScalar.pm use strict; use warnings; use Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( DEBUG ); our $DEBUG = 1; sub DEBUG { if (@_) { $DEBUG = shift; } return $DEBUG; } 1;
type ImportTest.pl #!/usr/bin/perl -w use strict; use warnings; use lib 'D:/Marcos/Perl/MSB'; use SS::ExportScalar qw( DEBUG ); my $x = 123; print("debug: x is $x\n") if DEBUG;

Now I execute it:

perl ImportTest.pl Bareword "DEBUG" not allowed while "strict subs" in use at ImportTest. +pl line 7. Execution of ImportTest.pl aborted due to compilation errors.

Now, I change the strict statement in ImportTest.pl:

type ImportTest.pl #!/usr/bin/perl -w use strict qw (vars refs); use warnings; use lib 'D:/Marcos/Perl/MSB'; use SS::ExportScalar qw( DEBUG ); my $x = 123; print("debug: x is $x\n") if DEBUG;
perl ImportTest.pl Bareword found in conditional at ImportTest.pl line 7. debug: x is 123

X:^P


Comment on Re^6: Global variables in Perl
Select or Download Code
Re^7: Global variables in Perl
by ikegami (Pope) on Jun 04, 2010 at 21:51 UTC

    You're missing package SS::ExportScalar;, so the script never imports DEBUG, so DEBUG is not declared in the script, so it's a bareword, so strict throws an exception.

      Sorry, must have got garbled in the transfer to the website. See the whole thing below. Note that if this was the case, the script would not have run the second time after changing strict and execution would be aborted at compilation because Perl wouldn't find ExportScalar.

      type ExportScalar.pm package ExportScalar; use strict; use warnings; use Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( DEBUG ); our $DEBUG = 1; sub DEBUG { if (@_) { $DEBUG = shift; } return $DEBUG; } 1;

        Ooooooops, my bad! It has to be 'package SS::ExportScalar' instead of just 'package ExportScalar'! Thank you so much! My NPR donation will be diverted to the Perl Monks! ;o)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-10-25 17:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (146 votes), past polls