Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found