Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

main and external script

by spadacciniweb (Deacon)
on Dec 02, 2005 at 17:58 UTC ( #513671=perlquestion: print w/ replies, xml ) Need Help??
spadacciniweb has asked for the wisdom of the Perl Monks concerning the following question:

I have a main program named "mal.pl" as:
1 #!/usr/bin/perl -w 2 3 push @INC, "."; 4 require "config.conf"; 5..7 8 print $verbose;
and in the same directory, the file "config.conf" as
1 $verbose = 1;
Question: if run "mal.pl"... why the output is:
Name "main::verbose" used only once: possible typo at ./mal.pl line +8.
thank you

Comment on main and external script
Select or Download Code
Re: main and external script
by ikegami (Pope) on Dec 02, 2005 at 18:15 UTC

    Sorry, I don't have an answer to your question. However, I do have a tip:

    When including Perl files without a package, you should use do, not require or use.

Re: main and external script
by gu (Beadle) on Dec 02, 2005 at 18:52 UTC
    At compile time, $verbose is used only once...

    To fix your problem you can write :
    #!/usr/bin/perl -w BEGIN{require "config.conf"} ; print $verbose ;
    But as ikegami said do is better. Furthermore, you should write our $verbose = 1; in the required file to give it an appropriate scope.

    Gu
Re: main and external script
by psychotic (Beadle) on Dec 02, 2005 at 19:16 UTC
    If you are creating a module to include configuration information, you might find the following useful. Firstly, we create the module that holds the configuration information, making sure the proper things are exported. Source:

    package Testing; use strict; use Exporter; our @ISA = qw(Exporter); our (%EXPORT_TAGS); $EXPORT_TAGS{'config'} = [qw($verbose $debug)]; Exporter::export_ok_tags('config'); our ($verbose, $debug) = (1,1); 1;
    And the main script that exploits the configuration information:

    use strict; use warnings; use Testing qw(:config); print "I am ", $verbose ? "" : "not ", "verbose\n"; print "I should ", $debug ? "" : "not ", "debug\n";
    When the code directly above is run, it produces, as expected:
    I am verbose I should debug
    For instance, if we changed this line ( our ($verbose, $debug) = (1,1); ) in the configuration module, to ( our ($verbose, $debug) = (undef,undef); ) we would get:
    I am not verbose I should not debug
      Or you can use the great Damian Conways' Config::Std. Really good one.

      Gu

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-08-20 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (120 votes), past polls