1

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 );
0

2 Answers 2

3

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'
Sign up to request clarification or add additional context in comments.

4 Comments

perl -MData -le'print for @Data::tests'
perl -MData -E'say for @Data::tests' (5.10+)
This works great thank you, is there a way I can access a hash in the Data module?
@user3487205 how should hash look like, ie. perl -MData -E 'say "$_:$tests{$_}" for keys %Data::tests' for key1:val1\nkey2:val2
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.