Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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 "" 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 ""... why the output is:
Name "main::verbose" used only once: possible typo at ./ line +8.
thank you

Replies are listed 'Best First'.
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.

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://513671]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-02-17 19:42 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (249 votes). Check out past polls.