Home > Archive > PERL CGI Beginners > November 2005 > Need help with making a Modules
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Need help with making a Modules
|
|
| Lou Hernsen 2005-11-27, 3:55 am |
| I am studying modules.. I am s ing a mentor to help.
I have very simple questions like ...
Do I need to pass vars into the mod?
Do i need to declare vars in the mod?
What is "our"? something like "my" and "local"?
Do I need to return vars?
The code I am writing creates part of a web page,
so I can print it in the mod or in the main.
(I wold prefer to print it in the mod.. if possible)
I have been reading for 2 w s now and can't find a simple working model
to disect that I can understand all the parts of.
I have read what CPAN has, and a CGI101 and Visuals Perl book and O'Rielys
Mods and objects(or something like that) .. and I can't figure it out.
Please reply
Thanks
Lou
| |
| David Dorward 2005-11-27, 3:55 am |
| On Sat, 2005-11-26 at 19:48 -0800, Lou Hernsen wrote:
> I am studying modules..
perldoc perlmod (and perlmodlib, and perlmodstyle)
> Do I need to pass vars into the mod?
No, but you might need to pass them into subroutines you define in the
module (depending on what you want to do).
> Do i need to declare vars in the mod?
That depends on what you want the module to do.
> What is "our"? something like "my" and "local"?
perldoc -f our
> Do I need to return vars?
The module itself needs to return a true value, this is usually achieved
by sticking "1;" at the end of it.
Your subroutines may or may not return variables depending on what you
want them to do.
> The code I am writing creates part of a web page,
> so I can print it in the mod or in the main.
> (I wold prefer to print it in the mod.. if possible)
Its possible, but I find that building a data structure that represents
the page, then dropping it into a template (using Template-Toolkit of
HTML::Template) makes things more manageable.
--
David Dorward <http://dorward.me.uk/>
"Anybody remotely interesting is mad, in some way or another."
-- The Greatest Show in the Galaxy
| |
| Charles K. Clarkson 2005-11-27, 3:55 am |
| Lou Hernsen <mailto:lhernsen1015@wowway.com> wrote:
: I am studying modules.. I am s ing a mentor to help.
What do you mean by "module"? I ask because many people have very
different ideas about what one is.
For example, some people think about a library.
require 'my_perl_module.pl";
When I say "module", I am usually thinking of an object.
use TemplateVariables;
my $vars = TemplateVariables
-> new()
-> select_categories();
Others think of a library of subroutines and variables.
use CGI qw(:standard center);
print
header(),
start_html('foo'),
center(h1('foo')),
end_html();
In your own words, define "module."
: I have very simple questions like ...
: Do I need to pass vars into the mod?
That depends on what you are attempting to do. It also depends
on what you want the module to do. The CGI example above passed
variables into the module to tell it which subroutines should be
exported. An object may require variables to be added when it is
constructed.
: Do i need to declare vars in the mod?
It is often very difficult to write an entire module without
declaring a variable, but it is not impossible.
: What is "our"? something like "my" and "local"?
Yes. Have you read perlfunc?
: Do I need to return vars?
Not usually. Most modules return values.
: The code I am writing creates part of a web page,
: so I can print it in the mod or in the main.
: (I wold prefer to print it in the mod.. if possible)
:
: I have been reading for 2 w s now and can't find a simple working
: model to disect that I can understand all the parts of.
Give us a more detailed idea of what you are attempting and
perhaps we could write an example for you.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
. . . With Liberty and Justice for all (heterosexuals).
| |
| Lou Hernsen 2005-11-27, 6:55 pm |
| This small piece of the Stats sub calculates the
speed at which you are traveling.
I have in the main program.
use stats
and I call on it just like a regular sub
Speed();
or should it be
stats::Speed;
????
When I call on the entire mod, i want the whole thing to run.
I could just put all the subs in the mod into one sub and call on that.. i
guess.
thanks
Lou
ps.. i know my coding sucks.. i'm a beginner....
its the best i can do given my understanding
########################
package stats;
sub BEGIN
{
use Exporter();
@ISA = qw(Exporter);
@EXPORT = qw(Speed)
}
sub Speed
{
$v0 = 1+$Army+$Hero+$Spy;
$v1 = 1+$Army+$Hero+$Spy+$Wagon;
if ($Wagon < 1 && $Horse >= $v0
&& $Saddle >= $v0 && $Blanket >= $v0
&& $Load <= 100)
{$Speed = "1.5";}
elsif ($Wagon == 0 && $Horse >= $v0 && $Load <= 100)
{$Speed = "2";}
elsif ($Wagon > 0 && $Horse >= $v1 && $Load <= 100)
{$Speed = "2.5";}
elsif ($Wagon == 0 && $Load <= 100)
{$Speed = "3";}
elsif ($Load > 100)
{$Speed = (int(.3 * $Load))/10;}
else {$Speed=3;}
}
return 1;
#####################3
| |
| Lou Hernsen 2005-11-27, 6:55 pm |
| I am trying to create a module that will print the Stats of the player of my
on line game.
all it should do is take the vars, do some calulations and print the HTML
since the Stats is used in every time i don't want to duplicate it in each
program.
The main program has all your players vars loaded...
the Stats subroutine (which I want to make into a mod)
puts the HTML code in the <BODY> of the web page...
from what I have read this should not be a hard things to do.
Module= a piece of code that is repeated in several programs.
Each location in the town is a separate program....
If I can't get this to work I'll just leave the sub Stats() in each program
and not worry about it.
Thanks
Lou
----- Original Message -----
From: "Charles K. Clarkson" <cclarkson@htcomp.net>
To: <beginners-cgi@perl.org>
Sent: Sunday, November 27, 2005 12:46 AM
Subject: RE: Need help with making a Modules
> Lou Hernsen <mailto:lhernsen1015@wowway.com> wrote:
>
> : I am studying modules.. I am s ing a mentor to help.
>
> What do you mean by "module"? I ask because many people have very
> different ideas about what one is.
>
> For example, some people think about a library.
>
> require 'my_perl_module.pl";
>
>
> When I say "module", I am usually thinking of an object.
>
> use TemplateVariables;
>
> my $vars = TemplateVariables
> -> new()
> -> select_categories();
>
>
> Others think of a library of subroutines and variables.
>
> use CGI qw(:standard center);
>
> print
> header(),
> start_html('foo'),
> center(h1('foo')),
> end_html();
>
> In your own words, define "module."
>
>
> : I have very simple questions like ...
> : Do I need to pass vars into the mod?
>
> That depends on what you are attempting to do. It also depends
> on what you want the module to do. The CGI example above passed
> variables into the module to tell it which subroutines should be
> exported. An object may require variables to be added when it is
> constructed.
>
>
> : Do i need to declare vars in the mod?
>
> It is often very difficult to write an entire module without
> declaring a variable, but it is not impossible.
>
>
> : What is "our"? something like "my" and "local"?
>
> Yes. Have you read perlfunc?
>
>
> : Do I need to return vars?
>
> Not usually. Most modules return values.
>
>
> : The code I am writing creates part of a web page,
> : so I can print it in the mod or in the main.
> : (I wold prefer to print it in the mod.. if possible)
> :
> : I have been reading for 2 w s now and can't find a simple working
> : model to disect that I can understand all the parts of.
>
> Give us a more detailed idea of what you are attempting and
> perhaps we could write an example for you.
>
>
> HTH,
>
> Charles K. Clarkson
> --
> Mobile Homes Specialist
> 254 968-8328
>
> . . . With Liberty and Justice for all (heterosexuals).
>
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Charles K. Clarkson 2005-11-27, 6:55 pm |
| Lou Hernsen <mailto:lhernsen1015@wowway.com> wrote:
: I am trying to create a module that will print the Stats of the
: player of my on line game.
: all it should do is take the vars, do some calulations and print the
: HTML
: since the Stats is used in every time i don't want to duplicate it in
: each program.
So there is just one subroutine.
: The main program has all your players vars loaded...
: the Stats subroutine (which I want to make into a mod)
: puts the HTML code in the <BODY> of the web page...
: from what I have read this should not be a hard things to do.
No. It is pretty easy.
: Module= a piece of code that is repeated in several programs.
That's what I thought you were after, but I wanted to be sure.
: Each location in the town is a separate program....
: If I can't get this to work I'll just leave the sub Stats() in
: each program and not worry about it.
Many people use copy and paste to reuse code. The problem is
you may need to make a change. Either you have to write a program
to make the changes in every file or you resign to not making the
change. A module is definitely the right solution for you.
package Stats;
# No need for BEGIN. Read the Exporter docs for details.
use Exporter 'import';
# Any element of this array will be imported into
# calling package namespace (probably main::).
@EXPORT = qw(Speed);
sub Speed
{
$v0 = 1+$Army+$Hero+$Spy;
$v1 = 1+$Army+$Hero+$Spy+$Wagon;
if ($Wagon < 1 && $Horse >= $v0
&& $Saddle >= $v0 && $Blanket >= $v0
&& $Load <= 100)
{$Speed = "1.5";}
elsif ($Wagon == 0 && $Horse >= $v0 && $Load <= 100)
{$Speed = "2";}
elsif ($Wagon > 0 && $Horse >= $v1 && $Load <= 100)
{$Speed = "2.5";}
elsif ($Wagon == 0 && $Load <= 100)
{$Speed = "3";}
elsif ($Load > 100)
{$Speed = (int(.3 * $Load))/10;}
else {$Speed=3;}
# Do your HTML stuff
}
# This is more common than using "return 1;"
1;
In your program do this.
use lib '.'; # Point to the directory where you placed
# your module.
#use Stats; # This is fine, but
use Stats 'Speed'; # this tells you where Speed() come from.
|
|
|
|
|