i want pass hash reference argument 1 perl script (script1.pl) perl script (script2.pl). how code looks:
----------------------------script1.pl---------------------------------
#!/usr/bin/perl -w use strict; use warnings; %hash = ( 'a' => "harsha", 'b' => "manager" ); $ref = \%hash; system "perl script2.pl $ref"; ----------------------------script2.pl---------------------------------
#!/usr/bin/perl -w use strict; use warnings; %hash = %{$argv[0]}; $string = "a"; if (exists($hash{$string})){ print "$string = $hash{$string}\n"; } and output error:
sh: -c: line 0: syntax error near unexpected token `(' sh: -c: line 0: `perl script2.pl hash(0x8fbed0)' i can't figure out right way pass reference.
a hash in memory data structure. processes 'own' own memory space, , other processes can't access it. if think it, i'm sure you'll spot why quite quickly.
a hash reference address of memory location. if other process 'understand' it, still wouldn't able access memory space.
what we're talking here quite big concept - inter process communication or ipc - so there's whole chapter of documentation it, called perlipc.
the long , short of - can't you're trying do. sharing memory between processes more difficult imagine.
what can transfer data , forth - not reference, actual information contained.
i suggest example, tool job json, because can encode , decode hash:
#!/usr/bin/perl -w use strict; use warnings; use json; %hash = ( 'a' => "harsha", 'b' => "manager" ); $json_string = to_json( \%hash ); print $json_string; this gives:
{"b":"manager","a":"harsha"} then can 'pass' $json_string - either on command line, although bear in mind spaces in confuses @argv bit if you're not careful - or via stdin.
and decode in sub process:
use strict; use warnings; use json; $json_string = '{"b":"manager","a":"harsha"}'; $json = from_json ( $json_string ); $string = "a"; if (exists($json -> {$string} )){ print "$string = ",$json -> {$string},"\n"; } (you can make more similar code doing:
my $json = from_json ( $json_string ); %hash = %$json; other options be:
- use
storable- either freezing , thawing ( memory) or storing , retrieving (disk) - use
ipc::open2, send data onstdin.
there's variety of options - have @ perlipc. it's not simple matter 'just passing reference' unfortunately.
Comments
Post a Comment