Pass a hash as command line argument in perl ..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthik baskar
    New Member
    • Sep 2010
    • 16

    Pass a hash as command line argument in perl ..

    Hi,
    I wanna pass a hash as a command line argument in perl. and I wanna call that perl program from another perl program using system() function. Please Leave a reply if you know. Thanks in advance.
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Simple enough, use Data::Dumper to serialize the data structure that you want to pass, and then use the array method of calling system to pass the data.

    The below scripts will demonstrate what I'm talking about:

    Code:
    # pass.pl: Pass Hash to second script
    
    use Data::Dumper;
    
    use strict;
    use warnings;
    
    my %hash = (
    	a => 1,
    	b => 2,
    	c => 3,
    );
    
    my $serialize = Dumper(\%hash);
    
    system('perl', 'read.pl', $serialize);
    Code:
    # read.pl
    
    use strict;
    use warnings;
    
    my $string = shift;
    
    my $hashref = do {
    	no strict;
    	eval $string
    };
    
    if ($@) {
    	warn "error in hashref: $@";
    }
    
    print "c = $hashref->{c}\n";
    - Miller

    Comment

    Working...