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

Hi,

can I just edit a module and append a function to it? I am guessing the function wont get found. How do I sorta "refresh the module" so that it is aware of my new functions? I have a module already done and I need to customize it (it was done by another programmer, it is not an official module)


thnx

Replies are listed 'Best First'.
Re: edit a module?
by moritz (Cardinal) on Jan 10, 2011 at 14:23 UTC
    can I just edit a module and append a function to it?

    You can, but if it's not one of your modules, it's a bad idea. It's too easy to lose overview over which files were changed, and it's hard to distribute those changes to others, who might need an unmodified version of the module to work for other scripts.

    I am guessing the function wont get found.

    Trying > guessing

Re: edit a module?
by Corion (Patriarch) on Jan 10, 2011 at 14:21 UTC

    Why guess?

    What happened when you added the function to the module?

    If you want to export the function from the module, add it to @EXPORT_OK or @EXPORT, see Exporter. If the module in question uses some other mechanism than Exporter, consult the relevant documentation.

      I searched for @EXPORT and @EXPORT arrays but they are not present in the module.... I am wondering if this is possible since I only got the *.pm files not the other ones required to build the module
      Well of course I tried not guessed.... it says that the function is not defined.

        Please reduce your code to the smallest self-contained code that still reproduces your problem. The main program and the "changed" module should each not be above 20 lines.

        Also tell us how things fail for you, as that helps us see how you are trying to call the new function.

Re: edit a module?
by Anonymous Monk on Jan 10, 2011 at 14:31 UTC
Re: edit a module?
by Anonymous Monk on Jan 10, 2011 at 15:33 UTC
    This is probably not what you're looking for, because it sounds like the module is developed by your organization so you could eventually directly contribute to its final shape, and it's probably not what you should be doing, because there are usually better ways to change how you interact with a module, but:

    In an old version of Path::Class, Path::Class::Dir had no "basename" method defined, while Path::Class::File did (and ::File didn't provide dir_list, etc). If you got passed some file object, there were multiple and mutually-exclusive code paths you could follow to get the file name. So I just added this to my module:

    sub Path::Class::Dir::basename { $_[0]->dir_list(-1) }

    This added that function, by its fully-qualified name, as "basename" into the "Path::Class::Dir" package, from within some other package! You could also do this with:

    { package Path::Class::Dir; sub basename { $_[0]->dir_list(-1) } }
    Which is like opening the package, defining subs or creating variable or importing symbols, and then closing it.

    You can also redefine existing subs in the same way (and surpress or ignore the resulting warning messages). But be very careful with this. Changing how an existing module works internally, especially when you redefine methods, without calling the originals, or change its internal data structures, can result in very confusing errors. This is especially prone to happen if the module is used in multiple places or ways in your program, eg indirectly by another module you're using.

Re: edit a module?
by tospo (Hermit) on Jan 10, 2011 at 16:29 UTC

    I'm guessing that when you ask how to 'refresh' the module you might have run into the following situation: you edited a copy of the module but when you 'use' it in some script it still isn't aware of the new function(?)

    Two possible problems come to mind:

    1. You haven't exported the new function or specifically requested in in the script that uses the module. There is a good write-up here on writing modules, where you can find out more about exporting.
    2. You have exported the function but it still isn't picked up because the original module is actually installed on your system and that is the version that will get loaded when you 'use' the module - not your local copy. You need to find out where it is installed so you can update the installed version. Check for example here for how to do that. If you aren't using some kind of version control system to trace changes I would strongly recommend you do so before updating installed modules.
Re: edit a module?
by cdarke (Prior) on Jan 10, 2011 at 16:07 UTC
    Taking note of what has already been mentioned (in particular exporting), find the documentation (POD) for the module and add your subroutine. If there is no documentation then write some (see perlpod).

    Now find the tests for the module. If there are none then this is a good time to write them. Add your new interface to the tests, and run them all together.
Re: edit a module?
by planetscape (Chancellor) on Jan 11, 2011 at 01:59 UTC
    I am guessing the function wont get found.

    Despite its somewhat unfortunate acronym, I prefer this approach: T.I.T.S. Or, Try It To See.

    HTH,

    planetscape