#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Array of Hashes between perl scripts via commandline argument


    Hello @,
    I am trying to pass "Array of Hash" as command line argument from one perl file to another perl file using reference. But the address becomes invalid once it reached another file . Is it because of different process address space between the perl files ? How to pass parameters like array, hashes and array of hashes from one perl file to another perl file using system("perl abc.pl <Array of Hashes>) command.
    Code which call another perl command is:
    #!/usr/bin/perl
    use strict;
    my (@AoH, $myRef, $CMD, $NewProcess );
    @AoH = (
    {
    Instance => "orcl",
    Metric => "6",
    Filter => "Tablespace_name NOT LIKE 'TEMP'",
    },
    {
    Instance => "orcl",
    Metric => "16",
    Filter => "Segment_name NOT LIKE 'TEMP'",
    },

    {
    Instance => "orcl",
    Metric => "10",
    Filter => "Any filter statement",
    },
    {
    Instance => "orcl",
    Metric => "100",
    Filter => "Any filter statement 1",
    },
    );
    $NewProcess = "TCAOH.pl";
    $myRef = \@AoH;
    print "Callee Address $myRef \n";
    $CMD = "perl $NewProcess -f $myRef" ;
    print "To execute : $CMD";
    system("$CMD > Mytestoutput.txt");


    My Code which will be called is :
    #!/usr/bin/perl
    use strict;
    use Getopt::Long;
    my (@AoH, $href, $role, $myRef, $input,$ret) ;
    print "Inside TCAOH.pl file \n";
    $ret = GetOptions ( "f" => \$input);
    print "Ret value : $ret \n";
    print "Address : $input \n";
    for (my $i=0; $i < @$input; $i++) {
    print $input->[$i]{Instance}."\n";
    print $input->[$i]{Metric}."\n";
    print $input->[$i]{Filter}."\n";
    }
    1;

    Output Error is:
    Callee Address ARRAY(0x32e6fc)
    To execute : perl TCAOH.pl -f ARRAY(0x32e6fc)Can't use string ("1") as an ARRAY
    ref while "strict refs" in use at TCAOH.pl line 38.

    Here line 38 is:
    for (my $i=0; $i < @$input; $i++) {

    Please suggest.
    End of the day, I want to store set of data into memory and pass to different perl scripts as and when required to avoid I/O activities by each set of perl scripts.
    What would be the best way to achieve this objective ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    Use the Storable module to exchange the data structure between the scripts.

    http://search.cpan.org/~ams/Storable-2.30/Storable.pm
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0
    Thanks a lot for your reply.
    Yes, it worked. But again its writing into a flat file and reading from the flat file which means I/O activity.
    Is there any way, we can avoid such I/O overheads ?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    How big of a data structure are you taking about? The example you've given would only take a few milliseconds of I/O overhead.

    You haven't provided enough info on what the real goal is, but my first suggestion would be to redesign/refactor your scripts and turn one of then into a module.

    Another option would be to use a database instead of a flat file.

    Comments on this post

    • OmegaZero agrees : Measure before you worry is always good advice.

IMN logo majestic logo threadwatch logo seochat tools logo