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

Hi, I have a perl program , lets call it A.perl. In the perl , i call a sub routine(present in C.perl , C is available to A using "require") which sets up an array on its first call . Subsequent call to the sub routine , checks the array status and decides wht to do but it does not set the array again. This is working fine as long as the sub routine call is with in the perl A.perl. No when i call the perl B from A , and use the same sub routine whihc A used , I lose the array value set by A. The array is defines globally in C.perl. Every time i call be to B.perl the array is getting set again . I want to avoid this. What I feel is every time i call B from A , it creates a new unix shell where all the variable are re-set. How can i use the value set by A in other perls called by A. I tired execution B.perl using the following 3 ways .None worked out :( (1) using perl SYSTEM function (2) Using perl EXEC function (3) Using unix backticks. Please tell me how to solve this. Thank you, Deepu

Replies are listed 'Best First'.
Re: Perl - unix process
by GrandFather (Saint) on Apr 01, 2009 at 03:28 UTC

    If you are using require, then really you are treating C.perl as a module. It's more conventional to use a .pm extension and use use. But in either case what you are doing is somewhat like a #include in C - in a sense you are including the (compiled) source of C.perl in the execution space of A.perl. If you run B.perl as a separate process the B.perl process does not have access to the execution context of A.perl and thus does not have access to the array tucked away in C.perl.

    You need to tell us something of the bigger picture and what you are trying to achieve at a higher level. You should also put together a little sample code to demonstrate what you are trying to do. Acme::ESP is currently a little flaky so it doesn't always help with this sort of question (although no-one is reporting bugs - we just expect the module's developers to know).


    True laziness is hard work
Re: Perl - unix process
by lakshmananindia (Chaplain) on Apr 01, 2009 at 03:35 UTC

    It will be nice if you post the code that you have done . Moreover read the
    Writeup formatting tips before posting

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


Re: Perl - unix process
by cdarke (Prior) on Apr 01, 2009 at 08:38 UTC
    You do not seem to appreciate how processes work - if I am wrong them please forgive me, like other monks I am having difficulty in understanding the issue.

    When you use system or `backticks` you will create a new process. That process effectively has a "firewall" around it preventing other processes from accessing anything inside. So if you set an array in one process it cannot normally be seen by another. To get around that there are features called IPC - InterProcess Communication. The simplest IPC mechanism is to use a pipe, which you are probably familiar with in ls|more. More complex mechanisms, like message queues and shared memory require synchronisation and locking, and you probably don't want to go there.

    What I can't figure out is why you are running separate processes at all, in fact the "code" you supplied (which is basically a bunch of comments) does not create a process. As others have said, placing other subroutines in modules, then loading these modules using use or require is the normal way of doing things.

    So, the question is, are these different programs which have to run, or just subroutines that can run in the same process?

    Update: I missed the call to system when I looked at your "code" - hardly surprising the way you formatted it. Basically you cannot pass a Perl array back and forth between processes in a simple manner: data is not shared automagically, you have to use an IPC mechanism, a database, or a file. Better yet - don't use separate programs.
Re: Perl - unix process
by ig (Vicar) on Apr 01, 2009 at 04:09 UTC

    The easiest way to make data generated by A.perl available to a separate process running B.perl may be to have A.perl write the data to external storage that B.perl can read. This would commonly be a database or file.

    To learn how to use a database you might read some of the material in Database Programming.

    To use a file, you have many options for formatting your data. You might search CPAN for serialize. In particular, you might be interested in YAML. There are also many modules that would help you if you wanted to save your data in the form of an XML file: you can search cpan for xml for options.

    Another set of modules that you might find helpful are the Tie modules. Tie::Concurrent comes to mind as it appears you want concurrent access to the data. If you are certain that concurrency is not an issue then Tie::Simple or Tie::Persistent may be adequate.

    Otherwise, you might consider the options presented in perlipc.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl - unix process
by roubi (Hermit) on Apr 01, 2009 at 02:13 UTC
    I think it would help if you could include the relevant pieces of code from A, B and C so we can make some sense of your post. After reading it a dozen times I am still not sure what you are trying to do and/or how to help you.
    A reply falls below the community's threshold of quality. You may see it by logging in.