Custom Function Scope and Variable Lifetime

Julian Field MailScanner at
Thu Aug 6 09:24:39 IST 2009

On 05/08/2009 22:32, Blatter, Nicholas wrote:
> I have been working with writing custom function for MailScanner and have come across an issue with variables and subroutines in one custom function (module) stepping on those in another module.   I'm not terribly familiar with Perl, but I imagine this is a result of MailScanner loading all the modules into a single namespace/package/scope.
> Does this sound correct?
Yes, they are all loaded into the same namespace. However, there's 
nothing stopping you putting variables which are specific for your 
custom function into their own namespace, just refer to them with the 
full namespace information, e.g. "$MyCustomFunction::MyVariable = 5;" 
should work just fine.
>    When writing custom functions, is there anything you can do to keep the functions separate from each other so that you don't run into name collisions or other problems arising from a shared namespace?  Would it work (or even make sense) to have each custom function in it's own Perl package?  How would that work when telling MailScanner to load the function?
I would rather not go to the effort of writing that. You can code around 
it so easily in your Custom Function anyway that it isn't really a 
problem worth solving.
> I'm also wondering what the lifetime of 'global' variables (in the custom function module) looks like.  Is there just one instance of each module loaded for MailScanner or does each MailScanner child process load an instance of each module (this appears to be the case from watching the logs).
I think you are correct in your interpretation of the logs, the forking 
happens before the Custom Functions are "require"-d.
>   Are the global variables in each module shared between the MailScanner children processes or are they separate?
> Sorry for the noobish questions but I'd like to make sure I'm writing these modules for MailScanner as well as I can.  Any tips or suggestions on their organization are appreciated.
No problem. Just put your global variables in your own made-up 
namespace, as in my little example above.


Julian Field MEng CITP CEng
Buy the MailScanner book at

Need help customising MailScanner?
Contact me!
Need help fixing or optimising your systems?
Contact me!
Need help getting you started solving new requirements from your boss?
Contact me!

PGP footprint: EE81 D763 3DB0 0BFD E1DC 7222 11F6 5947 1415 B654
Follow me at and

This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

More information about the MailScanner mailing list