Perl
  Home arrow Perl arrow Page 5 - Introduction to mod_perl (part 4): Per...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Introduction to mod_perl (part 4): Perl Basics
By: Stas Bekman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2003-01-03

    Table of Contents:
  • Introduction to mod_perl (part 4): Perl Basics
  • Using Global Variables and Sharing Them Between Modules/Packages
  • Making Variables Global With strict Pragma On
  • Using Exporter.pm to Share Global Variables
  • Using the Perl Aliasing Feature to Share Global Variables
  • Using Non-Hardcoded Configuration Module Names
  • The Scope of the Special Perl Variables
  • Compiled Regular Expressions
  • References

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    Introduction to mod_perl (part 4): Perl Basics - Using the Perl Aliasing Feature to Share Global Variables
    (Page 5 of 9 )

    As the title says you can import a variable into a script or modulewithout using Exporter.pm. I have found it useful to keep all theconfiguration variables in one module My::Config. But then I haveto export all the variables in order to use them in other modules,which is bad for two reasons: polluting other packages' name spaceswith extra tags which increases the memory requirements; and addingthe overhead of keeping track of what variables should be exportedfrom the configuration module and what imported, for some particularpackage. I solve this problem by keeping all the variables in onehash %c and exporting that. Here is an example of My::Config:

    package My::Config;
    use strict;
    use vars qw(%c);
    %c = (
    # All the configs go here
    scalar_var => 5,
    
    array_var  => [qw(foo bar)],
    hash_var   => {
    foo => 'Foo',
    bar => 'BARRR',
    },
    );
    1;

    Now in packages that want to use the configuration variables I haveeither to use the fully qualified names like $My::Config::test,which I dislike or import them as described in the previous section.But hey, since I have only one variable to handle, I can make thingseven simpler and save the loading of the Exporter.pm package. Iwill use the Perl aliasing feature for exporting and saving thekeystrokes:

    package My::HTML;
    use strict;
    use lib qw(.);
    # Global Configuration now aliased to global %c
    use My::Config (); # My/Config.pm in the same dir as script.pl
    use vars qw(%c);
    *c = \%My::Config::c;
    
    # Now you can access the variables from the My::Config
    print $c{scalar_var};
    print $c{array_var}[0];
    print $c{hash_var}{foo};

    Of course $c is global everywhere you use it as described above, andif you change it somewhere it will affect any other packages you havealiased $My::Config::c to.

    Note that aliases work either with global or local() vars - youcannot write:

    my *c = \%My::Config::c; # ERROR!

    Which is an error. But you can write:

    local *c = \%My::Config::c;

    For more information about aliasing, refer to the Camel book, secondedition, pages 51-52.

    More Perl Articles
    More By Stas Bekman


     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway