Perl
  Home arrow Perl arrow Page 4 - Introduction to mod_perl (part 4): Perl Basics
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Introduction to mod_perl (part 4): Perl Basics - Using Exporter.pm to Share Global Variables
    ( Page 4 of 9 )

    Assume that you want to share the CGI.pm object (I will use $q) between your modules. For example, you create it in script.pl, but you want it to be visible in My::HTML. First, you make $q global.

      script.pl:
      ----------------
      use vars qw($q);
      use CGI;
      use lib qw(.); 
      use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
      $q = CGI->new;
    
    
      My::HTML::printmyheader();

    Note that I have imported $q from My::HTML. And My::HTML does the export of $q:

      My/HTML.pm
      ----------------
      package My::HTML;
      use strict;
    
    
      BEGIN {
        use Exporter ();
    
        @My::HTML::ISA         = qw(Exporter);
        @My::HTML::EXPORT      = qw();
        @My::HTML::EXPORT_OK   = qw($q);
    
      }
    
      use vars qw($q);
    
      sub printmyheader{
        # Whatever you want to do with $q... e.g.
        print $q->header();
      }
      1;

    So the $q is shared between the My::HTML package and script.pl. It will work vice versa as well, if you create the object in My::HTML but use it in script.pl. You have true sharing, since if you change $q in script.pl, it will be changed in My::HTML as well.

    What if you need to share $q between more than two packages? For example you want My::Doc to share $q as well.

    You leave My::HTML untouched, and modify script.pl to include:

     use My::Doc qw($q);

    Then you add the same Exporter code that I used in My::HTML, into My::Doc, so that it also exports $q.

    One possible pitfall is when you want to use My::Doc in both My::HTML and script.pl. Only if you add

      use My::Doc qw($q);

    into My::HTML will $q be shared. Otherwise My::Doc will not share $q any more. To make things clear here is the code:

      script.pl:
      ----------------
      use vars qw($q);
      use CGI;
      use lib qw(.); 
      use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
      use My::Doc  qw($q); # Ditto
      $q = new CGI;
    
    
      My::HTML::printmyheader();
      My/HTML.pm
      ----------------
      package My::HTML;
      use strict;
    
    
      BEGIN {
        use Exporter ();
    
        @My::HTML::ISA         = qw(Exporter);
        @My::HTML::EXPORT      = qw();
        @My::HTML::EXPORT_OK   = qw($q);
    
      }
    
      use vars     qw($q);
      use My::Doc  qw($q);
    
      sub printmyheader{
        # Whatever you want to do with $q... e.g.
        print $q->header();
    
        My::Doc::printtitle('Guide');
      }
      1;
      My/Doc.pm
      ----------------
      package My::Doc;
      use strict;
    
    
      BEGIN {
        use Exporter ();
    
        @My::Doc::ISA         = qw(Exporter);
        @My::Doc::EXPORT      = qw();
        @My::Doc::EXPORT_OK   = qw($q);
    
      }
    
      use vars qw($q);
    
      sub printtitle{
        my $title = shift || 'None';
    
        print $q->h1($title);
      }
      1;


     
     
    >>> More Perl Articles          >>> More By Stas Bekman
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT