12.3. Tips for the FORTRAN Programmer

Perl code appears quite odd to a FORTRAN programer. FORTRAN uses ( ) (parentheses) exclusively for indices. An array element has the form ARRELM(1), a matrix element has the form MATELM(1,1), and a subroutine call takes the form CALL ASUB ( VALUE1, VALUE2 ), whereas Perl uses ( ) (parentheses), [ ] (brackets), and { } (braces) in various ways depending on data type and function. For instance, whereas $a[$i] refers to an element of an array, $a{$i} refers to a value in a hash. Forgetting the proper uses of these separators is a common error for the neophyte Perler experienced in FORTRAN.

If you use FORTRAN for its native handling of high-precision floating point numbers (and if you don't—what are you using it for?), then you'll find Perl's IEEE floating point too imprecise (just as if you were to program in C). You may find solace in the PDL module from CPAN or the Math::BigFloat module in the Perl core.

COMMON blocks are an evil way of passing around scads of global variables at the same time and are used far more often in FORTRAN than they should be. (If you want to define common constants used by many FORTRAN files, use a preprocessor; this isn't the '60s, you know.) The way to make available a set of constants that share some common theme in Perl is to put them into a module, using the Exporter, and then import them where you want them:

					% cat myconsts.pm
package myconsts;
use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(PI E);
use constant PI => 3.14159265;
use constant E  => 2.71828183;
1;  # Modules must return a true value

% cat mytest
#!/usr/local/bin/perl
use myconsts qw(PI E);
print "PI*E = ", PI * E, "
";

% mytest
PI*E = 8.53973421775655

That's worth a Perl of Wisdom:

Put common sets of constants in their own modules using the Exporter, and use the modules where needed.


If you prefer to use EQUIVALENCE—well, it's time to break the habit. You can create aliases for the same variable in Perl, but it's rarely useful. The Alias module provides an easy way of doing this if you must. If you want to alias one variable to the storage space of many, or vice-versa, as you can with EQUIVALENCE, well, you can't. And you'll get no sympathy from us.

If you are a FORTRAN programmer who never uses, nor intends to use, EQUIVALENCE statements or COMMON blocks, good! God only knows the measure of misery and heartache that has been inflicted on humanity by these monstrosities.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset