Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Calling variables from external file, with strict

by c (Hermit)
on Jun 28, 2002 at 00:33 UTC ( [id://177896]=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to call in variables from an external file, however with use strict; in place, I get the gong saying that the variables need to be declared. Is there an easy way to do this? I have a script that is getting to have a large number of user-definable variables that I would like to read from an external file, but

#!/usr/bin/perl -w use strict; require "script_options.txt";

just doesnt seem to be the way to go. I'm not up for digging in and reading from an xml config file, I just need something simple that lets a user define something like my $mtu = "1500"; in a seperate file.

thanks! -c

Replies are listed 'Best First'.
Re: Calling variables from external file, with strict
by danger (Priest) on Jun 28, 2002 at 01:11 UTC

    Do you really need the config file to define lexical variables --- or would populating a config hash suffice? If so there are various config modules on CPAN. A very simple and direct method is to define an anonymous hash in the config file and capture it via require --- as in this node. As erikharrison mentions, any code evaluation solution (as opposed to a parsing solution) means you trust the code in the config file.

Re: Calling variables from external file, with strict
by chromatic (Archbishop) on Jun 28, 2002 at 00:48 UTC
    There's no good way to access lexical variables outside of their scope. (There are ways to do it, but they involve XS and aren't recommended for production systems.) I've had good luck with Data::Denter, instead.
Re: Calling variables from external file, with strict
by erikharrison (Deacon) on Jun 28, 2002 at 00:53 UTC

    A solution might be to use our in the config file. Or, possibly, no strict. But be sure that the code in the file is safe (for some definition of safe, based on the environment the code will be used in).

    Cheers,
    Erik

    Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet

Re: Calling variables from external file, with strict
by bronto (Priest) on Jun 28, 2002 at 09:57 UTC

    You can't read lexical variables from an external file and see them in your file. With strict or without. That's one of the scope restrictions dictated by my.

    Anyway, you can't escape when use strict is in effect, but you shouldn't do without. Consider two alternative solutions:

    1: Create a module and export the variables you need from there. See perldoc perlmod and perldoc Exporter

    2: Use OOP. A quick-and-dirty solution could be to create a bare bones class with some attributes you can pull out via class or object methods, but it's not that elegant :-). I'll write some untested crap below here, but you should really look for something better that fits your needs

    package MyProgramConfiguration ; use strict ; use warnings ; # Take this as a starting point, # *NOT* as an example, *PLEASE*!!! my %config = ( color => 'yellow', beverage => 'chinotto', food => 'pizza', driver => 'Schumacher', ) ; my gimmeconf { # I don't care how you called me (class or object method) return %config ; }

    And your prog could do:

    use strict ; # don't leave it out use MyProgramConfiguration ; my %c = MyProgramConfiguration->gimmeconf() ;

    In case I didn't say that, I'll say it again: don't take this as production code!!! Read it, understand how it works and create your solution from scratch. You have been warned. Also, you should read about lexical variables and how they work.

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: Calling variables from external file, with strict
by krisahoch (Deacon) on Sep 06, 2002 at 18:21 UTC

    use Config::Properties. It is the easiest way that I know.

    Code first

    use Config::Properties; use Data::Dumper; my $propertyFile = new Config::Properties; open (FD, "property.properties") or die "Could not open file: !$"; $propertyFile->load( (*FD) ); close(FD); print Dumper $propertyFile;

    Now a external properties file. save as property.properties

    var = value one = 1 two = 2 oneandone = 2

    HTH - Kristofer

Re: Calling variables from external file, with strict
by zentara (Archbishop) on Jun 28, 2002 at 16:12 UTC
    I've done it using "our".
    In your config file, just declare each variable like:
    $a =1;
    $b=2;
    etc. Then in your script:
    #!/usr/bin/perl use strict; our ($a,$b); require config; print "$a\t$b\n";
      Oops, I forgot, the config file must have a 1; as the last line; to satisfy the Perl gods.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-16 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found