Home > Archive > PERL Beginners > October 2006 > how do i assign variables from a different file..
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how do i assign variables from a different file..
|
|
| Positive Mind 2006-10-09, 7:59 am |
| Greetings !!
i am beginer in Perl and have task in hand to do..i got to read lot of
variables and parameters which are defined in one file. I have to use these
variables in my perl script ..how do i do this...(how do i use the variables
in one file in my perl script...)
thnx,
pm
| |
| Jeff Pang 2006-10-09, 7:59 am |
|
>
>i am beginer in Perl and have task in hand to do..i got to read lot of
>variables and parameters which are defined in one file. I have to use these
>variables in my perl script ..how do i do this...(how do i use the variables
>in one file in my perl script...)
HELLO,
Simply,you can declare all the variables in that file to be the package variable.For example,in the template.file,
template.file
----
our $header = '...';
our $footer = '...';
.....
Then in the main script,you can get these variables' value by:
script.pl
----
require "/path/template.file";
our $header;
our $footer;
print $header; # it should get the variable's value you defined in the template.file
print $footer; # the same
The other way,you can define the codes in that file to be a package,then export the variables you needed.
Also about perl's variable scope,please read this article:
http://perl.plover.com/FAQs/Namespaces.html.en
--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
| |
| Ken Foskey 2006-10-12, 6:59 pm |
| On Mon, 2006-10-09 at 16:24 +0530, positive mind wrote:
> Greetings !!
>
> i am beginer in Perl and have task in hand to do..i got to read lot of
> variables and parameters which are defined in one file. I have to use these
> variables in my perl script ..how do i do this...(how do i use the variables
> in one file in my perl script...)
>
> thnx,
> pm
How about :
while( <$parm> ) {
s/\r//g;
s/\s*#.*//; # Strip comments.
next if( /^\s*$/ ); ## skip blank lines
chomp;
($key,$value) = split(/=/,$_,2);
$ref->{$key} = $value;
}
| |
| usenet@DavidFilmer.com 2006-10-12, 6:59 pm |
| Positive Mind wrote:
> i am beginer in Perl and have task in hand to do..i got to read lot of
> variables and parameters which are defined in one file.
You mean a config file? Aw, c'mon - there must be a hundred modules on
CPAN that do this for you. One that I use often is Config::IniFiles.
Have you even looked?
Or you may wish to consider Uri Guttman's famous (and simple and
elegant and very fast) config file parser, which doesn't have a lot of
bells and whistles but works great for most requirements:
my %conf = read_file( $filename ) =~ /^([^=]+)=(.+)$/mg ;
--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)
|
|
|
|
|