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 ( [id://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

Replies are listed 'Best First'.
Re: Calling a perl script(that returns an array) withing a perl script.
by choroba (Cardinal) 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 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.
Re: Calling a perl script(that returns an array) within another perl script.
by aitap (Curate) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found