Creating re-usable Perl modules

I have had a couple of questions recently regarding creating libraries of MaterialsScripts functions that you can call from another MaterialsScript. An example of this that is close to my heart is creating a Temperature Cycle function that I can call from any MaterialsScript I write without having to add it as a sub-routine etc.

This enables good code re-use and provides a central repository for subroutines that are used often.

The way to do this in Perl is to create a Perl Module. We already include many different Perl Modules in the Materials Studio Gateway but these are very generic.

From reading on the web it seems pretty tricky but, in reality, it is fairly easy to create a Perl Module and so I have attached a zip file with an example.

This zip file contains:

ClassicalSimsUtils.pm – a Perl module containing a temperature cycle subroutine that works with Forcite and Mesocite

forciteTempCycle.pl – a test Perl script that runs a Forcite Plus temperature cycle calculation using ClassicalSimsUtils.pm

mesociteTempCycle.pl – a test Perl Script that runs a Mesocite temperature cycle calculation using ClassicalSimsUtils.pm

water.xtd, Water_100box.xsd, MartAngle.off – test structures and forcefields required to run the test scripts.

How to use it:

  • Copy the
    ClassicalSimsUtils.pm
    to
    [Materials Studio Install Folder] \lib\perl\
    folder on the servers you want to use this on. It is a good idea to have this locally on your computer as well so that you can perform Check syntax etc on your scripts.

  • Add
    Use ClassicalSimsUtils;
    to the top of your Perl script

  • Invoke
    ClassicalSimsUtils::TemperatureCycle(arguments)
    from within your Perl script.

Caveats:

When you un-install Materials Studio on Windows, this folder is destroyed so you should always have a back-up copy of your Perl Modules.

How to tweak it:

Line 30: Contains the name of the Perl module or package. If you want to create your own package, you should set the name here. This should also be the same as the name of the .pm file.

Line 47, 48: These are the names of the subroutines that you want to have available from the Perl module.

Hope this is useful information.

Cheers

Stephen