I have a perl module with data definitions (hashes, arrays, etc.), is there any way I can access that data from inside a bash script? This isn't working for me...

#!/bin/bash

perl -e 'use Data'
tests=`perl -e "@tests"; `
echo "Perl tests = ${tests}"         # prints "Perl tests = "

The module looks something like this:

our @EXPORT_OK = qw( @tests );
our @tests = qw( 1 2 3 4 5 );
有帮助吗?

解决方案

If you have package variable @tests inside Data module,

perl -MData -e 'print "$_\n" for @Data::tests'

For perl 5.10 and above,

perl -MData -E 'say for @Data::tests'

其他提示

You can use a module from the command line with -M

perl -MData -e'print map {"$_\n"} @tests;'

In the code you give, you run one interpreter that loads Data. It exits. You then run a second interpreter, which prints @tests. As that's the only action the second interpreter has performed, it's uninitialized, and prints nothing.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top