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


In reply to Question about making modules. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.