Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Passing hash to another script via commandline

Reply
Thread Tools

Passing hash to another script via commandline

 
 
IanW
Guest
Posts: n/a
 
      06-19-2007
Hi

How do I pass a hash to another script via the command line?

That is, if I do this

use strict;
my %data = (
field1 => 'f1val',
field2 => 'f2val'
);
my $result = `perl z:/interface.pl \%data`;

and in interface.pl I have the lines:

use strict;
my($dataref) = $ARGV[0];
print $dataref->{'field1'};

I get the error:

Can't use string ("%data") as a HASH ref while "strict refs" in use at
z:/interface.pl line ..

and if I comment out "use strict" then I get nothing.

What am I doing wrong?

Thanks
Ian


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-19-2007
IanW wrote:
> How do I pass a hash to another script via the command line?

[sample script snipped, thank you for providing it]
> What am I doing wrong?


The shell command line argument interface does not provide any means to pass
complex data structures like hashes or references. It can deal with simple
strings only. This is not a limitation of Perl but of the command shell.

In other words: you need to pass the actual keys and values of your hash (as
strings!) and then recompose them into a hash in the called program.

It might be easier to use Data:umper to convert the hash into a textual
representation that can readily loaded into perl again or to use some other
form of interprocess communication.

jue



 
Reply With Quote
 
 
 
 
IanW
Guest
Posts: n/a
 
      06-19-2007

"IanW" <> wrote in message
news:f58nbp$muc$1$...

>
> and if I comment out "use strict" then I get nothing.
>
> What am I doing wrong?


I should say I am doing a "print $result;" in the calling script!


 
Reply With Quote
 
mattsteel
Guest
Posts: n/a
 
      06-19-2007

IanW ha scritto:

> Hi
>
> How do I pass a hash to another script via the command line?
>


You can't.

> What am I doing wrong?
>
> Thanks
> Ian


Possible workaround: you can try to tie-untie your hash to a file so
you can pass the filename to the second script which can tie-untie the
same hash, then.

 
Reply With Quote
 
IanW
Guest
Posts: n/a
 
      06-19-2007

"Jürgen Exner" <> wrote in message
news:XoRdi.7443$u65.1117@trndny07...

> It might be easier to use Data:umper to convert the hash into a textual
> representation that can readily loaded into perl again


Hi Jue

Thanks for the reply.

If I understand it correctly, doesn't Data:umper just convert the hash
into a string representing the hashes structure? In which case, if I tried
to pass that via the command line, the spaces (amongst some other characters
like double quotes) would cause probs when the string is pulled into @ARGV
at the receiving script?

Eg:

use Data:umper;
my $d = Dumper(\%data);
my $result = `perl z:/interface.pl $d`;

That would result in only $ARGV[0] only containing "$VAR1". One could put $d
in double quotes, but if there were double quotes in one of the hash values
then it would presumably mess things up again.

Regards
Ian


 
Reply With Quote
 
IanW
Guest
Posts: n/a
 
      06-19-2007

"mattsteel" <> wrote in message
news: oups.com...

> Possible workaround: you can try to tie-untie your hash to a file so
> you can pass the filename to the second script which can tie-untie the
> same hash, then.


I was rather hoping avoiding passing data via a file but thanks anyway.

Regards
Ian


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-19-2007
IanW wrote:
> How do I pass a hash to another script via the command line?


As others have told you, you can't.

However, nothing prevents you from assigning a hashref to @ARGV
directly, i.e. instead of

> my $result = `perl z:/interface.pl \%data`;


you may want to try:

$ARGV[0] = \%data;
do 'z:/interface.pl';

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Tim Southerwood
Guest
Posts: n/a
 
      06-19-2007
IanW wrote:

>
> "Jürgen Exner" <> wrote in message
> news:XoRdi.7443$u65.1117@trndny07...
>
>> It might be easier to use Data:umper to convert the hash into a textual
>> representation that can readily loaded into perl again

>
> Hi Jue
>
> Thanks for the reply.
>
> If I understand it correctly, doesn't Data:umper just convert the hash
> into a string representing the hashes structure? In which case, if I tried
> to pass that via the command line, the spaces (amongst some other
> characters like double quotes) would cause probs when the string is pulled
> into @ARGV at the receiving script?
>
> Eg:
>
> use Data:umper;
> my $d = Dumper(\%data);
> my $result = `perl z:/interface.pl $d`;
>
> That would result in only $ARGV[0] only containing "$VAR1". One could put
> $d in double quotes, but if there were double quotes in one of the hash
> values then it would presumably mess things up again.
>
> Regards
> Ian


I think there is a simple point being missed here.

Why not use Data:umper to serialise the data (because that's what it
does), print the result to STDOUT, have the called program read it in from
STDIN to a scalar, then eval the scalar.

In other words, pipe the serialised version of the hash between the two
programs. Very much the "unix way" (TM).

Another way to do it would be to use IPC::Shareable and tie the hash to a
blob of shared memory. All that the callee program needs to know is the
shared memory key id which is trivially passed as an argument.

Probably not a lot efficiency wise as the latter method has to serialise the
data anyway, but it's neat, and potentially bi-directional - ie the callee
can modify the data and have the caller see it.

Cheers

Tim
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-19-2007
Purl Gurl <> wrote:
> IanW wrote:
>
>> How do I pass a hash to another script via the command line?


> Others have explained why you cannot do this.


> I would simply
> create an array, pass the array, then work with array @ARGV
> in my secondary script, without creating a new hash.
>
> PRIMARY:
>
> #!perl
>
> %Data = (field1 => 'f1val', field2 => 'f2val', field3 => 'f3val');



Try it with these values:

%Data = (field1 => 'f1 val', field2 => 'f2val', field3 => 'f3val');

then rethink your "solution".


> system ("perl test2.pl @{[%Data]}");



--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
IanW
Guest
Posts: n/a
 
      06-20-2007

"Gunnar Hjalmarsson" <> wrote in message
news:...
> IanW wrote:
>> How do I pass a hash to another script via the command line?

>
> As others have told you, you can't.
>
> However, nothing prevents you from assigning a hashref to @ARGV directly,
> i.e. instead of
>
>> my $result = `perl z:/interface.pl \%data`;

>
> you may want to try:
>
> $ARGV[0] = \%data;
> do 'z:/interface.pl';


I did want, I did try and it did work

I had actually found another way that seemed to work, just before I read
your post, and that was to use Data:umper to stringify the hash, then
MIME::Base64 to encode it, then remove the line breaks, and then pass the
resulting string via the command line to then decode it at the other end.

However your way it decidedly simpler thanks.

Thanks also for the other responses. One thing I noticed from the different
replies is that there seem to be 3 ways to call another Perl script - the
backticks that I used, 'system', and 'do' - are there any real differences
between them?

Ian


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Passing all extra commandline arguments to python program, Optparseraises exception sapsi Python 4 04-21-2009 07:59 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
[XPOST] Controlling the commandline via ASP/WSH Lorenzo Bolognini ASP General 0 08-15-2003 12:27 PM
Passing value from one script on one page to another script on another page. Robert Cohen ASP General 3 07-15-2003 01:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57