eval, ARGV, hashes

Finally it is demonstrated how data are transfered between the scripts using hashes. This time we start with t1.pl:

 
#!/usr/bin/perl
#
# this is t1.pl
#
use strict;
{
    my @ARGV = qw( p1 val1 p2 val2);
    my $h =  eval `cat t2.pl`; 
    foreach my $key (sort keys %$h)
    {
	print "t1: $key " . $h->{$key} . "\n";
    }
}

In t1.pl @ARGV is filled with pairs of names and values. t2.pl copies @ARGV to a hash, preparing for a keyword/value lookup:

 
#
# this is t2.pl
#
my %params = @ARGV; 
foreach my $key (sort keys %params)
{
    print "t2: $key " . $params{ $key} . "\n";
}
return { k1 => "v1", k2 => "v2"};

t2.pl returns the reference of an anonymous hash.

The output reads:

 
$ ./t1.pl
t2: p1 val1
t2: p2 val2
t1: k1 v1
t1: k2 v2