Miguel has asked for the wisdom of the Perl Monks concerning the following question:
Since I don't see anyone (those authors that suggest good practices) sharing variables across modules this way, I wonder if I'm doing the right thing.
For example:
'test' is the main program;
'vars.pl' declares and initializes some variables I'll use across the modules;
'test.pm' is a module.
File: test ------------------------------------------ #!/usr/bin/perl -Tw $|++; use strict; BEGIN { require "./vars.pl"; } START: hello_world(); sub hello_world { print $::q->header, $::q->start_html, $::test->SayHelloTo({WHO=>"World!"}), $::q->end_html; } File: vars.pl ------------------------------------------ #!/usr/bin/perl -Tw; use strict; use lib './'; use CGI; our $q = CGI->new; use test(); our $test = test->new; File: test.pm ------------------------------------------ package test; use strict; use vars qw($VERSION); $VERSION = 1.0; sub new { my $class = shift; $class = ref($class) || $class; my $self = {}; bless($self, $class); return $self; } sub SayHelloTo { my $self = shift; $self->{PARAMS} = shift if @_; my $params = $self->{PARAMS} || ''; return $::q->p("Hello", $params->{WHO}); } 1;
Miguel
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sharing variables
by chromatic (Archbishop) on Feb 14, 2006 at 00:55 UTC |