perlenvironment-modulesmodulefile

How to store output of module avail command in perl?


#!/depot/local/bin/perl5.8.0
my @data = `module avail icwbev_plus `;
print "Data Array : @data \n " ;

my $data1 = `module avail icwbev_plus `;
print "Data $data1 \n" ;

my $data2 = system (" module avail icwbev_plus ");
print "S Data $data2 " 

Output :

Data Array :  
 Data  
S Data -1

I am not getting why it is not storing output to a variable. Please help me to solve this. Thanks in advance.


Solution

  • The module command is a shell alias or a function. Thus it cannot be called directly via a `` or a system call.

    To get the output of an avail sub-command, you should call the modulecmd command which is called by the module shell alias/function.

    To get the location of modulecmd on your system, type in a regular shell session type module which exposes the command called by the module shell alias/function.

    The fully qualified path to the modulecmd command can then be used through a back-tick or a system call to get the result of an avail sub-command:

    To get the output of a module avail command (in terse format to simplify parsing):

    #!/depot/local/bin/perl5.8.0
    my $data1 = `/usr/share/Modules/libexec/modulecmd.tcl perl avail --terse icwbev_plus 2>&1`; 
    print "Data $data1 \n"
    

    Note the --terse format used to simplify result parsing. Also stderr is redirected to stdout to catch the actual output of the command (as modulecmd primarily uses stdout to output environment change commands).