Re: Can I pass a reference between two scripts?
by mr.nick (Chaplain) on Jun 28, 2001 at 01:43 UTC
|
No, you can't. But you can do something just as good, use Storable; more specifically, freeze & thaw. Hm. Except that freeze doesn't create an ASCII only representation of the reference. But you could add URI::Escape to make it clean:
use strict;
use URI::Escape;
use Storable qw(freeze);
my $hashref={ Foo=>1, Bar =>'string' };
my $frozen=uri_escape(freeze($hashref));
my $output=`otherscript.pl $frozen`;
print $output;
and in your otherscript.pl, do something like:
use strict;
use URI::Escape;
use Storable qw(thaw);
use Data::Dumper;
my $hashref=thaw(uri_unescape(shift @ARGV));
print Dumper $hashref;
Update: Oops, you said "array reference", not "hash reference". Sorry; but the idea should be the same. But a warning, this method is limited entirely by the size of the reference that you are passing. If it's too big, the command-line version of it will probably exceed your OS's limit.
In fact, this method probably isn't a good idea in any circumstances :)
mr.nick ...
| [reply] [d/l] [select] |
Re: Can I pass a reference between two scripts?
by bikeNomad (Priest) on Jun 28, 2001 at 02:22 UTC
|
Well, you can pass the array contents: use Data::Dumper;
$Data::Dumper::Indent = 0;
$Data::Dumper::Useqq = 1;
my @arr = [ 'abc', 2, 'def', [ 5 , 6 ], 4 ];
my $cmd = "echo '" . Dumper(\@arr) . "'";
print qx{$cmd};
| [reply] [d/l] |
Re: Can I pass a reference between two scripts?
by sierrathedog04 (Hermit) on Jun 28, 2001 at 02:13 UTC
|
My brother tells me that even in C (which has pointer arithmetic) the value of a pointer in one process is meaningless to any other process. In other words, consider a C pointer that at a particular moment has a value of 0x12A99B and which points to a char having the value 'Y'.
At that exact moment, setting some other C pointer in some other process to that exact value of 0x12A99B will not result in a value which when dereferenced is 'Y'.
Perl is written in C and in this case would share C's limitations. The addresses in memory are not meaningful when sent from one process to another concurrent one. So the short answer is no, you cannot.
Sure, serializing your array is the right answer for how to do it, but if you want to be off-the-wall then convert your array to XML, write it to a file and then read it into your other process. Who needs serialized arrays anyway? All data should always be transferred as XML. That way you can practice the skills you will need for SOAP, the Dublin Core and other XML implementations. | [reply] |
|
|
That limitation has nothing to do with C. The limitation
has all to do with the process model the operating system
uses. Suppose it would be possible to pass a reference
(or a pointer) to a child process. There are at least two
problems with that:
- What do you think is going to happen if the parent process dies?
- Pointers (and hence, references), are nothing more than
integers - virtual addresses to some place in memory.
If one could pass a pointer or a reference, it would
mean one process can muck around in the data space of
another process. Regardless of whether that pointer was
passed. Just think of those implications. No security!
Script kiddie heaven.
-- Abigail
| [reply] |
|
|
| [reply] |
|
|
Re: Can I pass a reference between two scripts?
by runrig (Abbot) on Jun 28, 2001 at 02:25 UTC
|
You might try writing the data to a file using Storable, and passing the name of the file to the other script. Or trying IPC::Shareable, but that may or may not be be more trouble than its worth. | [reply] |
The entire concept is a little off...
by dragonchild (Archbishop) on Jun 28, 2001 at 17:09 UTC
|
The entire concept, while an interesting subject, sounds a little off. My first response would be "Why?!"
It sounds like you have a set of behaviors ABC in some script ABC.pl and you want to incorporate those behaviors into script XYZ. This sounds like the perfect use for modules. Instead of creating a whole script, create a module that has a function in it. That function will have all the behaviors ABC within it, and be able to take an array reference, or whatever else you might want to pass it. And, you can extend that module, get funky with OO, or anything else you might want to do. You get encapsulation and better error-handling and perfect Julienne fries, every time. | [reply] |
| A reply falls below the community's threshold of quality. You may see it by logging in. |