Re: passing array reference from one perl script to another perl scirpt
Thanks a lot for helping me. :) On Jan 25, 2008 6:50 PM, Nagrale, Ajay <[EMAIL PROTECTED]> wrote: > Passing hashes between the scripts would be useful using GDBM > files..Hashes would be stored internally. You can directly load and change > the contents. > > It's easy to handle. But, gdbm files have their own disadvantages when you > keep on adding and deleting the data. > > Try this out. This might help. > > ~Ajay > > -Original Message- > From: Chas. Owens [mailto:[EMAIL PROTECTED] > Sent: Friday, January 25, 2008 6:32 PM > To: praveen mall > Cc: beginners@perl.org > Subject: Re: passing array reference from one perl script to another > perl scirpt > > > On Jan 25, 2008 4:45 AM, praveen mall <[EMAIL PROTECTED]> wrote: > snip > > There are two script. From first script I need to call the second > program > > and in second program I want to receive the hash. I have complete hash > in > > first program and calling second program by system call by passing hash > > reference as a parameter. > snip > > I believe you are placing constraints on yourself that do not actually > exist, but we will work with them for now. You need some form of > IPC*. The easiest to understand is the simple file method: script one > writes a file to disk and script two reads that file. Now that we > know how to get the two scripts talking, we need to know how to > transfer a hash along that conduit. We need to serialize it (turn it > into a string that contains all of the information we need). There > are many functions in Perl that can do this for us and which one is > best depends on the data structure to be serialized and your other > needs. For now, let's us one that is in Core Perl: Storable**. > > Here is the first script > #!/usr/bin/perl > > use strict; > use warnings; > use Storable; > > my %h = (a => 1, b => 2, c => 3); > > store(\%h, "/tmp/perlipc.$$") >or die "could not store hash in /tmp/perlipc.$$: $!"; > > system("perl", "second.pl", $$) == 0 >or die "could not run second.pl"; > > Here is the second script > #!/usr/bin/perl > > use strict; > use warnings; > use Storable; > use Data::Dumper; > > my $parentpid = shift; > my $href = retrieve("/tmp/perlipc.$parentpid") >or die "could not retrieve hash from /tmp/perlipc.$parentpid: $!"; > > print Dumper($href); > > > * inter process communication > ** http://perldoc.perl.org/Storable.html > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > http://learn.perl.org/ > > > -- Thanks and Regards, Praveen Kumar Mall Jr. SQA Engineer Pune Mo. No. 09850982204
Re: passing array reference from one perl script to another perl scirpt
I am just putting the code here that might be helpful to others. Thank you very much. Praveen Mall Program1: (sending hash reference) use strict; use warnings; use Storable; my %h = (a => 1, b => 2, c => 3); store(\%h, "c:\\perlipc.$$") or die "could not store hash in /tmp/perlipc.$$: $!"; system("perl", "2.pl", $$) == 0 or die "could not run second.pl"; Program2: (receiving hash reference) use strict; use warnings; use Storable; my $parentpid = shift; my $href = retrieve("c:\\perlipc.$parentpid") or die "could not retrieve hash from /tmp/perlipc.$parentpid: $!"; my %hash = %$href; foreach my $key(keys %hash){ print "$key=$hash{$key}\n"; On Jan 28, 2008 3:00 PM, praveen mall <[EMAIL PROTECTED]> wrote: > Thanks a lot for helping me. :) > > > > On Jan 25, 2008 6:50 PM, Nagrale, Ajay <[EMAIL PROTECTED]> wrote: > > > Passing hashes between the scripts would be useful using GDBM > > files..Hashes would be stored internally. You can directly load and change > > the contents. > > > > It's easy to handle. But, gdbm files have their own disadvantages when > > you keep on adding and deleting the data. > > > > Try this out. This might help. > > > > ~Ajay > > > > -Original Message- > > From: Chas. Owens [mailto:[EMAIL PROTECTED] > > Sent: Friday, January 25, 2008 6:32 PM > > To: praveen mall > > Cc: beginners@perl.org > > Subject: Re: passing array reference from one perl script to another > > perl scirpt > > > > > > On Jan 25, 2008 4:45 AM, praveen mall <[EMAIL PROTECTED]> wrote: > > snip > > > There are two script. From first script I need to call the second > > program > > > and in second program I want to receive the hash. I have complete hash > > in > > > first program and calling second program by system call by passing > > hash > > > reference as a parameter. > > snip > > > > I believe you are placing constraints on yourself that do not actually > > exist, but we will work with them for now. You need some form of > > IPC*. The easiest to understand is the simple file method: script one > > writes a file to disk and script two reads that file. Now that we > > know how to get the two scripts talking, we need to know how to > > transfer a hash along that conduit. We need to serialize it (turn it > > into a string that contains all of the information we need). There > > are many functions in Perl that can do this for us and which one is > > best depends on the data structure to be serialized and your other > > needs. For now, let's us one that is in Core Perl: Storable**. > > > > Here is the first script > > #!/usr/bin/perl > > > > use strict; > > use warnings; > > use Storable; > > > > my %h = (a => 1, b => 2, c => 3); > > > > store(\%h, "/tmp/perlipc.$$") > >or die "could not store hash in /tmp/perlipc.$$: $!"; > > > > system("perl", "second.pl", $$) == 0 > >or die "could not run second.pl"; > > > > Here is the second script > > #!/usr/bin/perl > > > > use strict; > > use warnings; > > use Storable; > > use Data::Dumper; > > > > my $parentpid = shift; > > my $href = retrieve("/tmp/perlipc.$parentpid") > >or die "could not retrieve hash from /tmp/perlipc.$parentpid: $!"; > > > > print Dumper($href); > > > > > > * inter process communication > > ** http://perldoc.perl.org/Storable.html > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > http://learn.perl.org/ > > > > > > > > > -- > Thanks and Regards, > Praveen Kumar Mall > Jr. SQA Engineer > Pune > Mo. No. 09850982204 > -- Thanks and Regards, Praveen Kumar Mall Jr. SQA Engineer Pune Mo. No. 09850982204
Re: passing array reference from one perl script to another perl scirpt
Idea is good to turn one script as a module. But I can not do anyhow. What I want to achieve is: There are two script. From first script I need to call the second program and in second program I want to receive the hash. I have complete hash in first program and calling second program by system call by passing hash reference as a parameter. If anyone has solution of this then please let me know. Thanks, Praveen Mall On Jan 25, 2008 12:11 AM, Chas. Owens <[EMAIL PROTECTED]> wrote: > On Jan 24, 2008 6:06 AM, <[EMAIL PROTECTED]> wrote: > snip > > > What you are trying to do there won't work and even if it did it would > > > be a bad idea. It appears as if you are trying to modularize your > > > code. There are better ways of doing it than that. If you describe > > > what effect you are trying to achieve, we may be able to point you in > > > the right direction. > > > > > > What I see is that , reference is received in next program but I am > > not able to access the hash after dereferencing it. I was passing the > > hash reference. > snip > > No, what you are seeing is the result of turning a reference into a > string. You cannot turn a string back into a reference, even in the > same Perl program. Please describe what you want to do (not how you > want to do it) and someone on this list will be able to point you in > the right direction. The proper answer is to probably turn that > second script into a module that the first script can use. > -- Thanks and Regards, Praveen Kumar Mall Jr. SQA Engineer Pune Mo. No. 09850982204