Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to create a reusable routine ?

by bar10der (Beadle)
on Apr 02, 2004 at 11:55 UTC ( #341939=perlquestion: print w/replies, xml ) Need Help??

bar10der has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I want to know how I can create a reusable package/routine. Basically I want to write a user authentication rouitne which I would like to call in various perl scripts. To give an idea, I'd like the authentication package to accept two variables userId and UserPasswd. Authenticate id & password against LDAP (I know how to do this bit) and return success/failure flag.

Any help/guide will be greatly appreciated.

Replies are listed 'Best First'.
Re: How to create a reusable routine ?
by tinita (Parson) on Apr 02, 2004 at 12:03 UTC
    which part is it you have problems with? it would help if you told us what you already know/don't know yet.
    are you familiar with writing subroutines? if not, check perlsub, please.
    for creating a module, please look at perlnewmod.
    if your module will contain just a few subroutines at all, it's probably the best to use Exporter und export these subroutines; otherwise it's often the better approach to go for object orientation (perlboot etc.)
Re: How to create a reusable routine ?
by bronto (Priest) on Apr 02, 2004 at 12:03 UTC

    use Exporter; or create object-oriented classes (see perlboot, perltoot, perltooc).

    Ciao!
    --bronto

    Update: added links to doc


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz
Re: How to create a reusable routine ?
by bar10der (Beadle) on Apr 02, 2004 at 13:21 UTC
    Bronto,

    I think Exporter will do the trick for me. Since I am new to perl, please ignore my little ignorance, I am bit confused regarding where should I place my authenticate.pm (intented name of the package!) Ideally I'd like to keep it under cgi-bin/lib dir, no prob in that but where I am stuck is how do I call this routine from other directories under cgi-bin? So to give an example, if I am writing another script under cgi-bin/monthSale/mSale.pm and want to call authenticate.pm in mSale.pm, how do I do this?

      There's a special variable called @INC, it contains the paths in which perl looks for modules when you use one.
      An easy way to add directiories to @INC is the lib module.
      Just put these lines into the module/script that needs to call subroutines in authenticate.pm.
      use lib '/path/to/your/module_dir'; # can be relative, too use authenticate.pm;

      Well, I assume that you are calling your script mSale.pl, and not .pm. First of all, I strongly descourage you to put your libraries in any place where a web user can get. Said that, suppose you are putting your authenticate.pm in /usr/lib/cgi. Then you'll want to start your script with something such:

      #!/usr/bin/perl use strict ; # ...and... use warnings ; # always! # let your cgi find authenticate.pm in /usr/lib/cgi... use lib '/usr/lib/cgi' ; # ...and you are safe ;-) use authenticate.pm # your CGI goes here :-)

      I hope this helps

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
Re: How to create a reusable routine ?
by Fletch (Chancellor) on Apr 02, 2004 at 13:04 UTC

    Not to mention perldoc perlmod. And I seem to recall there being some really big site with all sorts of perl code with modules somewhere on the net that might have one or two relevant examples.

Re: How to create a reusable routine ?
by bar10der (Beadle) on Apr 02, 2004 at 15:39 UTC
    OK...I did try to write package and could compile without error.. but when I tried to run, I got

    Undefined subroutine &main::ret called at test.pl line 11.

    Here is my sample code -

    package nu; use Exporter; @ISA = qw( Exporter ); @EXPORT_OK = qw( &ret ); my $action = $ARGV[0]; sub ret{ my $str = $action ? $action : 'Nop'; return $str; } 1;
    Now I have another script where I want to call ret function from above package. Here is what I am doing -
    ---- --- use lib '/mydir/lib'; use nu; my $gat = ret('one4u'); print "Value is $gat \n"; ---- ---
    Can some one pointout where I am going wrong please?
      @EXPORT_OK just tell what can be imported from this package. If you want to set some default things to be exported by the package you should set the array @EXPORT too.

      Soo, @EXPORT_OK just tells what we can import explicity:

      use nu qw( &some_funtion $var $array %etc ) ;
      And @EXPORT set what the package export when you don't paste anything:
      use nu ;
      Take a look in the documentation of Export: http://www.perldoc.com/perl5.8.0/lib/Exporter.html

      Other thing, try to build a pure Object-Oriented module, soo you will be able to use it in any type of application (console, GUI, CGI, mod_perl, etc...). One bad thing that you are doing is the action variablr, dependent of the arguments paste when the applications is executed by console.

      Here's a simple example to start building an OO module:

      package FOO ; use strict qw(vars); use vars qw($VERSION) ; $VERSION = '0.01' ; sub new { ## The class name: my $class = shift ; ## Arguments when creating the object: my ( $arg1 , $arg2 , @rest ) = @_ ; ## Define from what package this object will be: my $this = bless({} , $class) ; ## Define some internal value/attribute in the object: $this->{FOO} = $arg1 ; ## Return the created object: return $this ; } sub methodx { ## The object reference: my $this = shift ; ## Arguments of this method: my ( $arg1 , $arg2 , @rest ) = @_ ; ... } 1;
      To use this module and create an object:
      use FOO ; my $foo = new FOO("some args") ; my @returned = $foo->methodx("other args") ;

      Graciliano M. P.
      "Creativity is the expression of the liberty".

Re: How to create a reusable routine ?
by bar10der (Beadle) on Apr 02, 2004 at 14:26 UTC
    Thanks to you all for your help. I think I got what I wanted! Let me go and put it in practice now..;)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-09-17 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?