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

I never got a book on perl, i just picked it up and ran with it. the problem is now i have a large project to do in it and it needs to be somewhat professional. i know that i am reusing the same functions over and over but i dont know how to write a library for perl. i know what i need to do i just don't know the syntax or my options HELP!!!! whaledawg@collegeclub.com

Replies are listed 'Best First'.
Re: Stupid Question
by marcos (Scribe) on Jun 13, 2000 at 14:16 UTC
    I would suggest perlmod and perlmodlib manpages. perlmodlib has a section "Guidelines for Module Creation", which might help. Anyway IMHO the greatest suggestion is (from perlmodlib manpage itself) "try to reuse the existing modules either in whole or by inheriting useful features into a new class" ... it is very likely that a module that does what you need already exists.
    marcos
RE: Stupid Question
by t0mas (Priest) on Jun 13, 2000 at 14:21 UTC
    In perldoc perlmod there is a cut-and-paste running start of a new module called Some::Module.
    Try it out.

    /brother t0mas
RE: Stupid Question
by BigJoe (Curate) on Jun 13, 2000 at 20:15 UTC
    If you want it to look really professional you should go to http://search.cpan.org and search through the documentation because if you are using the same functions over and over, chances are so did someone else. The use of modules makes your code look clean and professional.

    --Joe
Re: Stupid Question
by Anonymous Monk on Jun 13, 2000 at 21:09 UTC
    i know that i am reusing the same functions over and over
    So what he (or she?) really needs is just a package, not really a fully-blown OO interface. Remember the KIS rule!

    Let's say you have a function for logging and a function for carrying out some kind of calculation. Make a file called whatever.pm, and in it include:

    package whatever; sub log { # do logging } sub calculate_something { # do calculations } return 1; # Perl insists on this :-)
    So in your scripts, you can just do:

    #!/usr/bin/perl # Tells Perl to look in current dir for modules, may not # be necessary. unshift @INC, "."; use whatever; # the file must have .pm ext for 'use' whatever::log("Log file entry"); $answer = whatever::calculate_something();
Re: Stupid Question
by cwest (Friar) on Jun 13, 2000 at 21:16 UTC
    There are a few options, you have to decide what works for you.

    First, build a library file, we'll call it functions.pl, it may look like this:

    sub add {
      my( $x, $y ) = @_;
      return $x + $y;
    }
    
    sub subtract {
      my( $x, $y ) = @_;
      return $x - $y;
    }
    
    1;
    
    Now, your program may look like this:
    #!/path/to/perl -w
    use strict;
    $|++;
    
    requre '/path/to/functions.pl';
    
    print subtract( add( 3, 2 ), 1 );
    
    Next, to build a Perl Module that @EXPORTs the functions:
    package Functions;
    
    use strict;
    use Exporter;
    use vars qw/@EXPORT @ISA/;
    
    @ISA    = qw/Exporter/;
    @EXPORT = qw/add subtract/;
    
    BEGIN {
      import Functions @EXPORT;
    }
    
    sub add {
      return $_[0] + $_[1];
    }
    
    sub subtract {
      return $_[0] - $_[1];
    }
    
    1;
    
    Our program may resemble this:
    #!/usr/local/bin/perl -w
    use strict;
    $|++;
    
    use lib '/path/to';
    use Functions;
    
    print subtract( add( 3, 2 ), 1 );
    
    An object oriented version of the Perl Module:
    package Functions;
    use strict;
    sub new { bless {}, shift }
    sub add { shift; $_[0] + $_[1] }
    sub subtract { shift; $_[0] - $_[1] }
    1;
    
    And our program:
    #!/path/to/perl -w
    use strict;
    $|++;
    
    use lib '/path/to';
    use Functions;
    
    my $f = Functions->new;
    
    print $f->subtract( $f->add( 3, 2 ), 1 );
    
    I would probably (in most cases) go with the OO Module, but you have to choose your path.

    Enjoy!

    --
    Casey
    
Re: Stupid Question
by cleen (Pilgrim) on Jun 13, 2000 at 21:20 UTC
    I think your wanting to look into OO (object orientated) programming, There are some great tutorials on this page and others, one of my favorite has to be
    this one I learned alot from it, its rather detailed, and easy to understand..