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

Hi, We are having a scenario where, I need to pass a class object variable as an argument to a perl script. Scenario: Inside firstscript.pl, we create a variable of a class object. Then, call secondscript.pl by passing the class object as variable. Is this possible? Request you help on this. Thanks, Vivek.
  • Comment on Pass class object as argument to perl script

Replies are listed 'Best First'.
Re: Pass class object as argument to perl script
by cdarke (Prior) on Oct 31, 2008 at 09:35 UTC
    To explain, the answers above keep the two "scripts" in the same process, and that is required in order to pass Perl objects. An object variable is only a reference with a little extra data (its class). The reference contains a memory address that only applies to the current process (assuming you are dealing with a virtual memory system like UNIX, Linux, or Windows). Passing an address as an argument would not work because that same memory address could be used for something else, or might be invalid. Might work on Windows 95, but I'm sure you are not that desperate.

    You could do it other ways if you really need two processes. The simplest would be to pass the data rather than the reference, using any number of mechanisms, including Data::Dumper. You could also use shared memory (shmem), but that has other issues concerning synchronisation. You could use fork, which creates a process (thread on Windows) as an almost identical copy of the original, right down to the memory addresses. It really depends why you want to do this.
Re: Pass class object as argument to perl script
by ccn (Vicar) on Oct 31, 2008 at 08:22 UTC

    It is possible if you call secondscript with do command.

    # firstscript.pl my $object = new Object; do 'secondscript.pl';

    # secondscript.pl print $object;

    There is no passing argument but variables declared in firstscript are visible in secondscript

      There is no passing argument but secondscript can use variables declared in firstscript

      Right, but only if the variable is declared with our, in both files, see perlfaq8:

      1) do $file is like eval `cat $file`, except the former 1.1: searches @INC and updates %INC. 1.2: bequeaths an *unrelated* lexical scope on the eval'ed code.

      my variables are file scoped and thus not visible in secondscript.pl.

        Yes, I forgot to check it. thx
Re: Pass class object as argument to perl script
by Anonymous Monk on Oct 31, 2008 at 08:51 UTC
    So you already have objects, why not have modules too? That way you don't need to call scripts at all.
    % cat secondscript.pl #!/usr/bin/perl -- use SomeOtherModuleApp; SomeOtherModuleApp->run(@ARGV);
Re: Pass class object as argument to perl script
by gnvivek (Initiate) on Nov 03, 2008 at 08:50 UTC
    Thank you very much. Both the below options are working. 1. Pass the data, rather than reference(Using Data::Dumper). 2. Call secondscript with do command, while declaring variable as 'our'. Reason for second script is, because my firstscript is growing huge. In this context, which above option is more appropriate. Thank you, Vivek.