How to Write Portable Perl Code

Writing portable Perl code has plenty of advantages. One main advantage is that you can write it once and run it on any platform. Therefore, the need to port the code to a new operating system is eliminated. Also, it's easier to maintain a single copy of one Perl script instead of having to maintain one written script per each operating system.

Instructions

    • 1

      Use the Perl unlink command instead of the native operating system commands for file deletion. The Perl unlink command will delete files for any operating system. Avoid using the Unix rm command or the Windows dir command.

    • 2

      Be certain that the command is in the path whenever you execute an external command. Try not to assume that an external program is already predefined in your path variable. Whenever you're not sure, make sure to set your path variable before you execute that external system call.

    • 3

      Take care with Unix forward slashes and Windows backslashes. Files on a Unix or Linux system use '/' in the path names, but files on a Windows/DOS system use '\' slashes. To write portable Perl code, your code should refer to the correct slashes for the respective operating system.

    • 4

      Use opendir, readdir, and closedir perl methods for directory processing. For portable Perl code, these methods are much better than operating system that relies on file globbing for directory. Any type of file globbing will not produce portable Perl code.

Related Searches:

Comments

You May Also Like

Related Ads

Featured