vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
Sorry if it's a stupid question, but I wonder if it's possible to pass array pointers in input stream and if it makes sense at all, like
........ @arr = ....... `perl myscript.pl \@arr`; ............
I assume that in contrast to C where we need to describe "pointer to what" in Perl pointers already have some info and inside myscript.pl I do
my $arr_ptr = $ARGV[0];
and then I work with @$arr_ptr ..... or this is nonsense ...

Replies are listed 'Best First'.
Re: Passing array pointer through script input
by GrandFather (Saint) on Aug 20, 2008 at 22:25 UTC

    It's nonsense. ;)

    If you try `perl myscript.pl \@arr`; the \ quotes the @ and you end up with '@arr'. You could:

    my $ref = \@arr; `perl myscript.pl $ref`;

    but Perl would stringify $ref to something of the form: ARRAY(0x182df38).

    However, none of this is solving your real problem. But then, you haven't actually told us what your real problem is. Take a couple of steps back in your thinking and tell us what you are trying to solve with this technique.


    Perl reduces RSI - it saves typing
      OK, the whole story.
      I am passing file paths in the loop from the main script through several levels of script calls. In the bottom I read files and process.
      Sinse main script is also looped, I want to do something like to store files in array of arrays once and read from these arrays instead of reading from files which should be much faster.

        Sounds like premature optimization. If the work you are doing per file is significant then the minor overhead of reading a list from a small file is trivial.

        It may be worth using something like DBM::Deep to store the list of files to be processed however to make management of the list easier and perhaps faster.


        Perl reduces RSI - it saves typing
Re: Passing array pointer through script input
by moritz (Cardinal) on Aug 20, 2008 at 22:18 UTC
    You can't, not like this. Perl opens a shell (or a pipe) to execute the back ticks, so it's only text that goes "over the wire".

    `perl myscript.pl \@arr` would actually pass a literal @arr to myscript.pl.

    To pass any data structure except strings you first have to convert them to string, by serializing them.

      There are three places along the path where stringification is forced, but none are related to pipes.

      1. `...` is a form of string literal. A string is created from its contents. `...` is the same thing as readpipe("...").
      2. If the shell is required, then the "..." in "`...`" is a shell command, which can only be a string.
      3. Arguments are passed to the child via a variant of the exec system call, which only accepts strings.
        Arguments are passed to the child via a variant of the exec system call, which only accepts strings.

        That's where I thought the pipes where involved - to read the output of the exec'ed command, perl has to open a pipe to it, which I thought would replace the exec call altogether. But my C fu is rusty to inexistent...

Re: Passing array pointer through script input
by tilly (Archbishop) on Aug 20, 2008 at 22:31 UTC
    You can't do this in the way you want. Odds are that you either want to fork or else use a module like Storable to stringify your data structure, write it to a file, then read that at the other end.

    Another option worth considering is that you could write the data structure to an external file for persistent storage. If you're interested in that, you may find DBD::Deep useful.