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: Advanced:
setting up variables so that i can use them in any other scripts

 



mehaboob
New User

Dec 8, 2009, 11:31 PM

Post #1 of 4 (2515 views)
setting up variables so that i can use them in any other scripts Can't Post

hi,

i want to write a perl script which defines all the variables that i am going to use in my other perl scripts.

I am trying to avoid using hardcoded values in all of my scripts. I want to define all the varibles in one script and once i execute that script then rest of my scripts can use those varibles..


thanks in advance


FishMonger
Veteran / Moderator

Dec 9, 2009, 5:14 AM

Post #2 of 4 (2502 views)
Re: [mehaboob] setting up variables so that i can use them in any other scripts [In reply to] Can't Post

See:
perldoc -f require
perldoc -f do


shawnhcorey
Enthusiast


Dec 9, 2009, 6:12 AM

Post #3 of 4 (2497 views)
Re: [mehaboob] setting up variables so that i can use them in any other scripts [In reply to] Can't Post

Create a Perl module containing the variables as `our`, then `use` the module in your scripts.

See:
perldoc -f our
perldoc -f use
perldoc perlmod
perldoc perlmodlib
perldoc perlmodstyle
perldoc perlmodinstall

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


johnpc831
Novice

Dec 10, 2009, 3:38 PM

Post #4 of 4 (2410 views)
Re: [mehaboob] setting up variables so that i can use them in any other scripts [In reply to] Can't Post

Another popular way of doing this is creating a package that inherits from Exporter using


Code
package MyPackage; 

use Exporter;

@ISA = qw(Exporter);


Then, within the package, you can specify a bunch of variables and values that will be used throughout your programs.

You can then decide which variables and/or functions to export to any program using this package using the @EXPORT and @EXPORT_OK arrays. Example


Code
@EXPORT = qw($variable1 $variable2 @list1 @list2); 
@EXPORT_OK = qw($variable3 $variable4 @list3 &myfunction);


You can then use your package and have all the stuff specified in @EXPORT imported into your program automatically, or have all the stuff specified in @EXPORT_OK imported upon request.

So if you only wanted $variable4, you would say


Code
use MyPackage qw($variable4);


The Exporter module is pretty neat, and it can do even more. You can even set up export tags to import groups of things into your program.

 
 


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

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