Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

main and external script

by spadacciniweb (Curate)
on Dec 02, 2005 at 17:58 UTC ( [id://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

Replies are listed 'Best First'.
Re: main and external script
by ikegami (Patriarch) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-20 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found