View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Victor Marcelo Santillan > Fortran-Namelist > Fortran::Namelist



Annotate this POD

Related Modules

View/Report Bugs
Module Version: 0.10   Source  


Fortran::Namelist - Perl extension for Fortran namelists


use Fortran::Namelist;

my $nml_file = '/home/user/my_namelist.nml';

my $nml=Fortran::Namelist->new( file => $nml_file );

my %new_group = ( group_name => { grp_var1 => 'some_string', grp_var2 => [ 1 , 2 , 3 ], } );

 $nml->set( %group );

 $nml->print( file => 'another_file.nml' );


Namelist implements a basic handling of standard fortan namelists. Namelist files are ascii files that allow a fortran program to transfer a group of variables by referencing the name of the group which they belong. Example of a namelist file:


  vec(2) = 3, 4, 5
  c      = 'abc'
  i  = 3


A namelist record starts with the name of the group preceded by '$' or by '&', followed by a sequence of variables-values with the appropriate value separators ( blanks,tabs,newlines, or any of them with a single comma). the end of the group is indicated by '$end' or by '/'.


The goal of this module is to allow an easier handling of namelist files before they are used by a fortran program. Although creating a namelist file from scratch is an easy task in perl, once a file already exists, modifying a variable in it, is likely to end up in with a code that at least it is not reusable, may not be clean and also may fail in some cases due to the flexibility that fortran provides to write namelist files. (FORTRAN Namelist-directed I/O is like list directed I/O, i.e using *,instead of fmt='(...)' in the format statement).

This module is not a parser ( but almost ), and it may fail in some cases, however when the namelist file is 'fortran-readable' will handle it correctly and I had no problems so far.


new new(%hash) ^

Creates a new namelist object.

my $nml=Fortran::Namelist->new();

Create a namelist object from the file 'myfile.nml'.

my $nml=Fortran::Namelist->new( file => 'myfile.nml');

set set(%hash) ^

Its argument is a hash. The keys are the name of the groups to be set. The values of the hash are anonymous hashes and its keys are the name of the variables in the group. The values of this anonymous hashes can be: strings, reference to arrays, or references to anonymous hashes. Strings and references to arrays are used to provide one or multiple values depending on the variable. References to hashes are used to represent array indexes of variables that could be multidimensional. Example:

my $nml= Fortran::Namelist->new( file => 'my_file.nml');

# create groups using a new hash

my %groups=( group_a => { str_var => 'abcd efg', arr_var => [ 1, 2 , 4], }, group_b => { vec_var => { 1 => [ 4,5,6] }, mat_var => { 1 => { 1 => [ 7,8,9 ] } }, }, );

# a better way to define group_a and group_b

$groups{group_a}{str_var} = 'abcd efg';

$groups{group_a}{arr_var} = [1, 2, 4];

$groups{group_b}{vec_var}{1} = [4,5,6];

$groups{group_b}{mat_var}{1}{1} = [7,8,9];



# end



  str_var='abcd efg'
  arr_var= 1, 2, 3



  vec_var(1)= 4, 5, 6
  mat_var(1,1) = 7, 8, 9


print print(%hash) ^

Prints the namelist to a file or file-handle, default file-handle is STDOUT.


$nml->print( file => 'output.nml');

open( my $fh , "> nml_out.nml") or die "$!\n";

$nml->print( fh => $fh );






Victor Marcelo Santillan <>


Copyright (c) 2006 Victor Marcelo Santillan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: