Ihave some questions. I want to make some basic module that could one day go on cpan.
Please review my example module, which is not the actual code, but a cut down example of how i do things.
package 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;
Example script
#!/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