CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Using lexical variables from other files

 



naven8
Novice

Mar 14, 2012, 11:24 AM

Post #1 of 5 (816 views)
Using lexical variables from other files Can't Post

Hi,
I am trying to use lexical variable defined in other file in my file by using do/eval. I know we cannot get lexical variable by using "do". Please let me know how can i do that with eval.


Code
File1: 

#!/usr/bin/env perl

do "File2";
print "$name $lex";


File2:
our $name = "Print our";
my $lex = "Print lex";


o/p:
Print our


FishMonger
Veteran / Moderator

Mar 14, 2012, 1:02 PM

Post #2 of 5 (812 views)
Re: [naven8] Using lexical variables from other files [In reply to] Can't Post


Quote
I know we cannot get lexical variable by using "do".

Your own output tells you that your assertion is wrong.

A var declared with the 'our' keyword is a global var and would be accessible, but a var declared with the 'my' keyword is lexical and would not be accessible in the calling script.

However, if you add the strict and warnings pragmas, which should be in every Perl script you write, you'll find that even the global var won't be available, unless you also declare it in the calling script.

There are ways to achieve what you're attempting, but the question I ask is: "why would you want to do this"?

If you really do need lexical vars declared in other scripts, then the proper/best approach is to write a module that uses subroutines to return the values of the vars.


naven8
Novice

Mar 15, 2012, 1:23 AM

Post #3 of 5 (792 views)
Re: [FishMonger] Using lexical variables from other files [In reply to] Can't Post


Quote

why would you want to do this"?

I would like to have a config file which will have all the configuration setting.

Is there any other way of doing this? (I dont want to create a perl module for this.)


(This post was edited by naven8 on Mar 15, 2012, 1:24 AM)


rovf
Veteran

Mar 15, 2012, 3:58 AM

Post #4 of 5 (789 views)
Re: [naven8] Using lexical variables from other files [In reply to] Can't Post

If I understand you right, the syntax of the config file should be Perl code.

You could put the config file into its own package, and declare all the configuration variables (or constants) package global, i.e.


Code
# configuration variable (config file should be evaluated with 'do') 
our $param=4711;


or


Code
# configuration constant (config file should be evaluated with 'require') 
use constant param => 4711;


Assuming that the config file declarations end up in package MyProject::Config, you can access them as


Code
$MyProject::Config::param


or


Code
MyProject::Config::param


respectively.

Of course there are other possibilities. Let's say your config file looks like this:


Code
use strict; 
use warnings FATAL => 'all';
%MyProject::Config = (
param => 4711,
# other configuration parameters go here
);


you would access the configuration as $MyProject::Config{param}.

Then, of course, you also have the possibility to choose a different syntax for your config file (for example, INI-File, XML-File). This depends a lot on the people who are actually writing the config file, and there is no general "best" solution.


FishMonger
Veteran / Moderator

Mar 15, 2012, 7:09 AM

Post #5 of 5 (778 views)
Re: [naven8] Using lexical variables from other files [In reply to] Can't Post

rovf's statement "there is no general "best" solution" is correct, but the solution that you're attempting to use is probably one of the worst.

A much better approach would be to use one of the many config modules on cpan.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives