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

In my earlier post, I had created a function to read values from a configuration/INI file (and for those who contributed advice, thank you!). I want to use this function in multiple perl scripts. While I could just copy the code to each script, I don't want to have to change the code in every script when I make a change.

I realize I could make this into a package, but that seems like overkill for just one function. Is there anything in perl equivalent to #include in C other than making a module of it? Making a module looks a bit beyond me at this point.

Replies are listed 'Best First'.
Re: Including code without packaging?
by Masem (Monsignor) on May 16, 2001 at 00:40 UTC
    Use "require" as opposed to "use". You don't need to have any packages or the like with require, though if you look at it's man page, there are a few gotchas for how you call it. But this will do what you are looking for.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Including code without packaging?
by mikfire (Deacon) on May 16, 2001 at 00:54 UTC
    I could be slightly annoying and point out Config::IniFiles already exists. Then again, "rolling your own" can frequently be fun.

    As to your exact question, I would make a package anyway. It only adds a few lines to the file and I can almost promise you this code will grow. More functions will be added, maybe some different variables to control how errors are handled, etc. If you start with a package now, it will save you some hassle later.

    Of course, ymmv
    mikfire

      When I check cpan for this I can only access the README, the module gives a "Not Found". Is this a CPAN problem or has the module been removed?
        Go here and click on the Latest Release link. That worked for me
        mikfire
      I agree with your viewpoint..I personally have a module named MyUtilities where i just stuff algos i use over and over. One day a REAL module may just spontaneously jump out of it. Meanwhile i use it just like a junk drawer for code... :)
      _________________
      madams@scc.net
      (__) (\/) /-------\/ / | 666 || * ||----||
Re: Including code without packaging?
by traveler (Parson) on May 16, 2001 at 01:08 UTC
    Making a simple module is not difficult and does not require you to learn O-O programming or anything other than a basic formula. For a module with just the function 'foo' create Foo.pm with the following:
    package Foo; require Exporter; @ISA =qw(Exporter); @EXPORT =qw(foo); # List funcs to export here sub foo($){ print @_; }
    To use it:
    use Foo; foo("Hello\n");
    --traveler
Re: Including code without packaging?
by lachoy (Parson) on May 16, 2001 at 00:59 UTC

    AppConfig takes care of this. From the docs:

    Configuration files may be arranged in blocks as per the style of Win32 "INI" files. [file] site = kfs src = ~/websrc/docs/$site lib = ~/websrc/lib dest = ~/public_html/$site [page] header = $lib/header footer = $lib/footer
    HTH

    Chris
    M-x auto-bs-mode