0
#!/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.

toolic
  • 57,801
  • 17
  • 75
  • 117
  • What do you get if you run `module avail icwbev_plus` on the command line? Also `system` returns the exit code of the process in the call and -1 usually means that it cant find the program you are trying to run. – JGNI Aug 16 '19 at 11:17

3 Answers3

1

To quote from the documentation for system (Emphasis added):

The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below). See also exec. This is not what you want to use to capture the output from a command; for that you should use merely backticks or qx//, as described in "`STRING`" in perlop. Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).

That combined with the blank output of the other attempts suggests that this module command isn't present in your path when you try to execute it. (I suspect that if you followed best practices and included use warnings; you'd get one about using an undefined value when you try to print $data1)


Anyways, if this module command is present on the computer you're running your perl code on, try using the absolute path to it (my $data1 = qx!/foo/bar/module avail icwbev_plus!), or put the directory it's in in your path before running the script.

Shawn
  • 47,241
  • 3
  • 26
  • 60
0

module outputs to stderr, not stdout, which is not captured by qx/backticks. You can try:

`LMOD_REDIRECT=yes module avail ...`

See https://lmod.readthedocs.io/en/latest/040_FAQ.html

ysth
  • 96,171
  • 6
  • 121
  • 214
0

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).