Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Using require

by kidd (Curate)
on Jul 10, 2002 at 16:47 UTC ( #180795=perlquestion: print w/replies, xml ) Need Help??
kidd has asked for the wisdom of the Perl Monks concerning the following question:

I started using strict not so long ago, and now Im in front of a problem. When I create my scripts I like to make a so it would be the only thing a user changes(settings, option, that kind of things)...

So yesterday I made the first script this way, only that this time I used strict and -w...

I created my and added the require line, I test it and oopss...I don't seem to be using the variables inside config...

Could someone give me some light and tell me why it isnt working that is some example...

Let's say I have my "" with a variable:

#!/usr/bin/perl -w use strict; my $option = 1;

And my main code is like this:

#!/usr/bin/perl -w use strict; require "" print "Option = 1" if $option != 0;

For some reason $option is not "imported" from and I can't explain why...thanks

Replies are listed 'Best First'.
Re: Using require
by Abigail-II (Bishop) on Jul 10, 2002 at 16:56 UTC
    $option is a lexical variable inside A file is a lexical scope. Make $option a package variable.


      How do I create $option a package?
        By not creating it a lexical variable. Leave the my off. Of course, you either have to get rid of the strictness, use use vars, or use a fully qualified name.


Re: Using require
by perrin (Chancellor) on Jul 10, 2002 at 17:16 UTC
    package MyConfig; use strict; use vars qw($option); $option = 1;
    Then, elsewhere:

    use MyConfig; print "Option = 1" if $MyConfig::option != 0;
      Thanks Abigail and thanks Perrin a lot...I created a package, and it worked great...

      It's good to learn something new...I read the documentation on use vars and now I know what is that Im doing wrong....THANKS

Re: Using require
by zentara (Archbishop) on Jul 11, 2002 at 17:07 UTC
    Use 'our'.
    Let's say I have my "config" with a variable:

    our $option = 1;
    1; #must end with true return
    And my main code is like this: #!/usr/bin/perl -w use strict; our $option require "./" print "Option = 1" if $option != 0;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-06-21 01:53 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.