Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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) ;
}


In reply to Re: Calling variables from external file, with strict by bronto
in thread Calling variables from external file, with strict by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found