Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Example scriptpackage MyModule; use version; use warnings; use strict; use Carp; use CGI; use base qw( Exporter ); our $VERSION = qv ('0.0.1'); our @ISA = qw (Exporter); our @EXPORT_OK = qw (new); our %EXPORT_TAGS = ( DEFAULT => [qw (new) ], All => [qw (new bar browserEnv)] ); sub new{ my $class = shift; my $self = {}; my %arg = @_; $self->{ResourcePath} = $arg{ResourcePath}; $self->{AssignedVal} = $arg{AssignedVal}; bless ($self, $class); return $self; } sub bar{ my $self = shift; print "<p>Testing, called sub 'bar' within MyModule</p>"; print "<p>Testing, ResourcePath: ".$self->{ResourcePath}."</p>"; print "<p>Testing, AssignedVal: ".$self->{AssignedVal}."</p>"; return; } sub browserEnv(){ my $self = shift; my $cgi = new CGI; if($cgi->http('HTTP_REFERER')){ print "<p>Testing, there was a referer: ".$cgi->http('HTTP_REF +ERER')."</p>"; }else{ print "<p>Testing, there was <b>no</b> referer</p>"; } return; } 1;
#!/usr/bin/perl use strict; use warnings; use CGI; use MyModule; my $cgi = new CGI; print $cgi->header; print "<html>\n<body>"; print "<p>Testing module</p>"; my $foo = MyModule->new( ResourcePath => '/opt/app/res', AssignedVal => '50', ); $foo->bar(); print "<p>Testing we can access object, print value of module paramete +r: $foo->{AssignedVal}</p>"; print "<p>Testing we can change an value (AssignedVal=100):<p>"; $foo->{AssignedVal}=100; $foo->bar(); $foo->browserEnv(); print "</body>\n</html>";
My questions.
Basically, is this how things should be done? I would like to here if I am doing things the right way. Should the module need use CGI even if the calling script is using it already, does this load it twice? Am i altering the value of AssignedVal correctly?
Thank you for your time
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Question about making modules.
by dragonchild (Archbishop) on Jul 06, 2008 at 15:25 UTC | |
by tye (Sage) on Jul 06, 2008 at 18:07 UTC | |
by dragonchild (Archbishop) on Jul 06, 2008 at 21:07 UTC | |
by linuxer (Curate) on Jul 06, 2008 at 22:13 UTC | |
by dragonchild (Archbishop) on Jul 07, 2008 at 00:49 UTC | |
by linuxer (Curate) on Jul 07, 2008 at 09:45 UTC | |
by dragonchild (Archbishop) on Jul 07, 2008 at 12:56 UTC | |
| |
by ysth (Canon) on Jul 07, 2008 at 03:23 UTC | |
|
Re: Question about making modules.
by cdarke (Prior) on Jul 06, 2008 at 18:58 UTC | |
|
Re: Question about making modules.
by EvanCarroll (Chaplain) on Jul 06, 2008 at 19:30 UTC | |
|
Re: Question about making modules.
by pc88mxer (Vicar) on Jul 06, 2008 at 20:02 UTC | |
|
Re: Question about making modules.
by ysth (Canon) on Jul 06, 2008 at 19:31 UTC | |
|
Re: Question about making modules.
by toolic (Bishop) on Jul 07, 2008 at 13:58 UTC |