Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Importing vars from one script to another

by edbewald (Sexton)
on Dec 11, 2012 at 20:23 UTC ( #1008391=perlquestion: print w/ replies, xml ) Need Help??
edbewald has asked for the wisdom of the Perl Monks concerning the following question:

I am not a new perl programmer and have learned most of what I know about PROGRAMMING IN GENERAL from this website. And though that may give me an edge, I am far from eloquent. Now I have a question.

I inherited a rather convoluted script from a former employee. He, unfortunately, did not believe in using warnings nor strict. He 'requires' a file that contains only 'constant' variables. My question is what is the easiest way to bring constants defined in one script into another.?

constants.pl:

$TRUE = 1; $FALSE = 0;

Main script:

require "constants.pl"; foreach $num ( 1...5 ) { if ($num < 3) { print $TRUE,"\n"; } else { print $FALSE,"\n"; } }

Desired output:

1 1 0 0 0

There are over 300 constants in the constants.pl file so this is just an example. I have tried making constants.pl into a package, using 'our' (not really sure when or if to use this), and 'use constant'. Searches on this site and the web have given me suggestions, but so far I can't make the main program import the 'constant' variables into my main script.

Comment on Importing vars from one script to another
Select or Download Code
Re: Importing vars from one script to another
by space_monk (Chaplain) on Dec 11, 2012 at 20:37 UTC
Re: Importing vars from one script to another
by Anonymous Monk on Dec 11, 2012 at 20:55 UTC
Re: Importing vars from one script to another
by BillKSmith (Chaplain) on Dec 11, 2012 at 23:03 UTC

    Your 'constants' are not constants at all, but normal global variables. They should work as shown. In order to satisify warnings, I had to add a use Lib '.'; to your main script to let perl find the constants.pl file. I also had to add a final 1 to the constants.pl file.

    I recommend using the Readonly module to make all the constants truly constant.

    Bill

      That did it! I thought I had tried everything but I was missing the '1'. Now it works.

      And I agree 100% that these are hardly constants. I usually use Readonly module for that and will do so with this.

      Thanks for all the answers everyone gave. I really appreciate it

        That did it! I thought I had tried everything but I was missing the '1'. Now it works.
        Whatever was the reason, this one can't be it.

        If you require a file, the last expression in the included file must return a value which evaluates to true, otherwise your program dies. Therefore: If you had a value which evaluated to false in your earlier version, your program would not have executed, and you would have got an error message pointing out the problem. But if it already evaluated to true in your earlier version, adding a 1 doesn't change anything.

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: Importing vars from one script to another
by ww (Bishop) on Dec 11, 2012 at 23:33 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-09-21 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (165 votes), past polls