3

probably a weird question, but bear with me (-: when Perl loads a module using 'use', this is a compile time directive, and assuming this module doesn't use 'require' anywhere in this module, can I launch a script which 'uses' that module and while the script ruins erase the module from the HDD and be sure that the module is all loaded into the memory? (can call all of its methods and such...)

Thanks,

2
  • can you give examples reflecting your question?
    – tuxuday
    May 23, 2012 at 7:51
  • 1
    That may work for simple modules, but more complex one could load submodules on demand or read from the DATA file handle (though, Linux/Unix will not really delete the file while it is open and on Windows, IIRC, deletion of an open file is not allowed).
    – salva
    May 23, 2012 at 8:59

2 Answers 2

2

Yes, you can. Perl compiles sources into internal data structure (parse tree) before running it, and it doesn't work with .pm files at runtime. So removing modules from disk is safe after the script has been compiled and successfully launched.

1
  • 1
    "Perl compiles source into byte code" is not strictly true. Perl compiles the module into a series of internal data structures that do not resemble byte code in any way. There is (or was, I am not sure about its current state) a byte code generator for serializing/deserializing that internal structures into byte code but it is rarely used.
    – salva
    May 23, 2012 at 9:10
1

This will work for simple modules that are completely loaded on start. Do note however, that many modules use AUTOLOAD feature to defer loading their heavy parts until some specific function is really needed. Those modules may then pull required source from pretty much anywhere - pre-split by function files, their own __DATA__ section, etc. Naturally trying to load files you've already deleted will fail. Accessing own __DATA__ will still work however (though I can't say for sure if it is because it is loaded in memory or because perl holds open handle to script, preventing it to completely disappear until it finished).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.