Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

how to create Modules

by dwijew (Initiate)
on May 17, 2005 at 05:06 UTC ( #457664=perlquestion: print w/ replies, xml ) Need Help??
dwijew has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm new to perl and have had some problems trying to create a module. can someone please tell me how i can create a module and use the functions in a .pl file? or redirect me to a site which has an online tutorial for me to follow in order to understand how to do it.

below is my module so far

package FSClient; #file: MySequence.pm use strict; my $EcoRI = 'ggatcc'; use base 'Exporter'; my @EXPORT = qw(reversec seqlen); my @EXPORT_OK = qw($EcoRI) sub reversec { my $sequence = shift; $sequence = reverse $sequence; $sequence =~ tr/gatcGATC/ctagCTAG/; return $sequence; } sub seqlen { my $sequence = shift; $sequence =~ s/[^gatcnGATCN]//g; return length $sequence; } 1;

i have got a lot of compile errors in the .pl file which i use to call this module.

thanks
Wijew

20050520 Edit by Corion: Changed title from 'Modules'

Comment on how to create Modules
Download Code
Re: how to create Modules
by Zaxo (Archbishop) on May 17, 2005 at 05:15 UTC

    The Exporter variables @EXPORT and @EXPORT_OK and your exportable $EcoRI should be package globals, so declare them with our or use vars rather than my. You also need a package MySequence; declaration at the top to set up the namespace. [ I missed the initial package declaration before code tags were added. Why do you give the package a different name than the file? That my cause confusion.]

    There may be more, but that's what I catch at first reading. Code tags would make it easier to scan.

    After Compline,
    Zaxo

Re: how to create Modules
by gopalr (Priest) on May 17, 2005 at 05:20 UTC
Re: how to create Modules
by jbrugger (Parson) on May 17, 2005 at 05:25 UTC
    package FSClient; #file: MySequence.pm
    Please keep the filename and packagename the same, eg:
    package blah; #file: blah.pm
    Next, you don't have to create different files for packages, what i do a lot is something like this:
    package user # file = user.pm use strict; sub new { my $classname = shift; my $self={}; bless $self,$classname; my $UserData = user::Data->getUserData(); } package user::Data; # still in file user.pm # Database layer for package user. use strict; use DBI; sub getUserData(){ my $self=shift; #etc. } 1;
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: how to create Modules
by tphyahoo (Vicar) on May 17, 2005 at 09:00 UTC
    See also Dominus's "Very Very Short Tutorial About Modules". Linked from his home node. Very useful.
Re: how to create Modules
by polettix (Vicar) on May 17, 2005 at 09:15 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://457664]
Approved by chromatic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-07-22 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (113 votes), past polls