in reply to Re: De-Reference an array
in thread De-Reference an array


Can you please explain in more I didnt understood what do you mean by "All you're passing are the stringified references. If you want to pass entire datastructures on the command line, use a serializer, and pass the serialized data on the command line. Deserialize in the called program."

Can you please modify the code to help me understand this.

Replies are listed 'Best First'.
Re^3: De-Reference an array
by stevieb (Canon) on Oct 09, 2009 at 14:15 UTC

    To understand what was meant by "All you're passing are the stringified references", change your code like this:

    #!/usr/bin/perl # program.pl use warnings; use strict; my @array1 =( 1..3 ); my @array2 = ( 4..6 ); my $ref_array1 = \@array1; my $ref_array2 = \@array2; system("./print.pl",$ref_array1,$ref_array2);

    ...and the print.pl:

    #!/usr/bin/perl use strict; use warnings; my ( $ref_string1 ,$ref_string2 ) = @ARGV; print "$ref_string1, $ref_string2\n";

    ...and run it.

    You can use 'Storable' to save your data serialized into a file, and then have the print program lift it back up:

    #!/usr/bin/perl use warnings; use strict; use Storable; my @array1 = ( 1..3 ); my @array2 = ( 4..6 ); my $store_file = './data.store'; # encapsulte the two arrays into a single array ref # for storage. We'll extract in print.pl store( [ \@array1, \@array2 ], $store_file ); # instead of passing the data as the parameters, pass the file name in +stead system( "./print.pl", $store_file );

    ...and the print....

    #!/usr/bin/perl use strict; use warnings; use Storable; my $store_file = $ARGV[0]; # we have to extract the two original arrays from the # single array that we encapsulated them in my $aoa = retrieve( $store_file ); my ( $aref1 ,$aref2 ) = @{ $aoa }; print "@{ $aref1 }, @{ $aref2 }\n";

    Hope this helps!

    Steve

      Thanks Steve for the help on this.
Re^3: De-Reference an array
by Utilitarian (Vicar) on Oct 09, 2009 at 14:17 UTC
    Change
    system("print.pl",$ref_array1,$ref_array2);
    to
    print "Array 1: $ref_array1\nArray 2: $ref_array2\n";
    And run, you'll see what you're passing to the second script.

    A suggested you'll have to provide the contents of the arrays rather than references to the second script.

      If I am providing contents of the array then it will not be possible to extract the info in the 2nd script.
      (@ref_arr1,@ref_arr2)=@ARGV;
      @ref_arr2 will always be empty if arrays are not passed by reference. Please correct me if I am wrong
        You are not wrong. You could provide the two arrays with a separator or as 2 quoted strings which you then split into arrays.