Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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:


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: 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.


20050520 Edit by Corion: Changed title from 'Modules'

Replies are listed 'Best First'.
Re: how to create Modules
by gopalr (Priest) on May 17, 2005 at 05:20 UTC
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 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,

Re: how to create Modules
by polettix (Vicar) on May 17, 2005 at 09:15 UTC
Re: how to create Modules
by jbrugger (Parson) on May 17, 2005 at 05:25 UTC
    package FSClient; #file:
    Please keep the filename and packagename the same, eg:
    package blah; #file:
    Next, you don't have to create different files for packages, what i do a lot is something like this:
    package user # file = use strict; sub new { my $classname = shift; my $self={}; bless $self,$classname; my $UserData = user::Data->getUserData(); } package user::Data; # still in file # 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://457664]
Approved by chromatic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-06-21 11:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.