Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Calling a perl script(that returns an array) within another perl script.

by uday_sagar (Scribe)
on Aug 21, 2012 at 12:14 UTC ( #988712=perlquestion: print w/ replies, xml ) Need Help??
uday_sagar has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I have to receive an array from a perl code by another perl. I thought this way, but isn't working!

@array = `perl ./perlmonks_are_great.pl`; print @array;

Thanks

Comment on Calling a perl script(that returns an array) within another perl script.
Download Code
Re: Calling a perl script(that returns an array) withing a perl script.
by choroba (Abbot) on Aug 21, 2012 at 12:17 UTC
    Does your perlmonks_are_great.pl script print the array, one member per line? If not, your script can not work.
    Update:
    Using Data::Dumper, Storable or YAML is another way of moving data around. If your array can contain strings with newlines or references, the backquotes are not powerful enough.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Calling a perl script(that returns an array) withing a perl script.
by moritz (Cardinal) on Aug 21, 2012 at 12:18 UTC

    In what way is it not working? Are blue flames coming out of your computer when you try to run it? No? Then what happens?

    It can be helpful to refactor the perl script to expose all interesting functionality as a module and then use it. Then you don't have to care about the communication between the processes, the data serialization and deserialization and, and error handling becomes much easier.

Re: Calling a perl script(that returns an array) within another perl script.
by aitap (Deacon) on Aug 21, 2012 at 12:23 UTC
    What does the array contain? What it the record separator used? You may need to serialize your data before printing it (for example, using YAML, JSON or something else).
    Sorry if my advice was wrong.
Re: Calling a perl script(that returns an array) within another perl script.
by greengaroo (Hermit) on Aug 21, 2012 at 12:56 UTC

    It works fine for me! Since you didn't provide the code for your perlmonks_are_great.pl script, here is mine:

    # Simple array my @array = qw(test1 test2 test3 test4 test5); # Printing one-liner print join ("\n", @array), "\n";

    Note: if you just print @array; you end up with only one string when you "read" your script: test1test2test3test4test5. You must have a new line after each element of your array. Also, the new line character (\n) will be saved too so you need to chomp it if you don't need it.

    Take my advice. I don't use it anyway.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988712]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (18)
As of 2014-07-10 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (215 votes), past polls