in reply to How to return Hashref from one script to another script?

G'day Sriram,

The built-in module Storable allows you to store Perl data structures and retrieve them later. This may be suitable for your use.

Here's a very quick-and-dirty example of a "parent script" (pm_1078041_parent.pl) calling a "child script" (pm_1078041_child.pl). The child creates a hashref, stores it and exits. The parent retrieves the hashref and uses it to create XML.

[XML::Simple was only used for demonstration purposes. Please don't take that as any sort of recommendation.]

The parent script (pm_1078041_parent.pl):

#!/usr/bin/env perl use strict; use warnings; use Storable; use XML::Simple; my $file = 'pm_1078041_store'; `pm_1078041_child.pl`; my $hashref = retrieve $file; my $xml = XML::Simple->new->XMLout($hashref); print $xml;

The child script (pm_1078041_child.pl):

#!/usr/bin/env perl use strict; use warnings; use Storable; my $file = 'pm_1078041_store'; my $hashref = { key1 => 'qwerty', key2 => 'asdfgh', key3 => { A => 1, B => 2, C => 3, }, key4 => [5, 6, 7], }; store $hashref => $file;

A sample run:

$ pm_1078041_parent.pl <opt key1="qwerty" key2="asdfgh"> <key3 A="1" B="2" C="3" /> <key4>5</key4> <key4>6</key4> <key4>7</key4> </opt>

-- Ken

Replies are listed 'Best First'.
Re^2: How to return Hashref from one script to another script?
by sriram83.life (Acolyte) on Mar 14, 2014 at 06:13 UTC
    Thanks Ken. I personally use LibXML.