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

for example i got this two program... program one computes for the total order of orange... my $total_orange = _______ and the second program needs to get the value of the total orange for it to know how much it cost... so how will i declare it? will it be some what like Visual basic like GLOBAL? or i just call it outright like $total_orange? need some help! thanx! lelak

Replies are listed 'Best First'.
Re: variable passing....
by Fastolfe (Vicar) on Dec 11, 2000 at 07:21 UTC
    ...and this may not even be the first homework question asked by lelak.

    If you have two programs that you need to communicate between, you need to use the IPC version of open, or backticks (look for the qx// item in perlop).

    In many cases like this, where you have two scrips needing to do (in part) the same task, it's usually easier to separate out the common task into a module, and call the module from both scripts. In a pinch, use open or `backticks` and just parse the output returned by the 2nd script.

Re: variable passing....
by mirod (Canon) on Dec 11, 2000 at 13:53 UTC

    The solution you want really depends on the whole sequence:

    If your programs run in sequence, first the one that

    • If the programs are run in sequence, the one that computes the total then the one that uses it, you can write the total to a file and then use it. The alternatives for the file format would be:

      • just a number (if that's all you want to pass),
      • var=val pairs (or any other home-brewed format) if you have several values to pass,
      • use Data::Dumper for a more robust and extensible pure-Perl solution,
      • use XML::Simple for a buzzword-compliant but also extensible and cross-platform/cross-language solution.

    • if you programs are run one after the other don't forget that you can just pipe them: comp_total | use_total where the output of comp_total is the total, read from STDIN by use_total,
Re: variable passing....
by repson (Chaplain) on Dec 11, 2000 at 10:55 UTC
    Perl has no global variables. It sounds like you may want to look into one of two possibilities...
    1 - Have program two call require 'program_one.pl' at some point before it needs to use $total_orange, this technique will allow you to put $total_orange in the main package and use it as you want.
    2 (better) - Make program one a seperate package (ie. package Program_one with a function which can be called to calculate and return the desired value. Then program two needs to use Program_one; and run my $total_orange = Program_one::oranges();. This is the more desireable solution because it helps you to learn about files, packages and modules in perl.

      Stop that train! Perl has global variables. What you may be thinking is that each package global exists in its own namespace, so $foo in package Foo is a distinct entity from $foo in package Bar. But all that means is that while you're in package Bar, $foo refers to $Bar::foo. A variable declared in a package (with use vars or, in 5.6, our) is global within that package (e.g. every subroutine in that package can see and modify that variable (unless it's masked by a my declaration -- i.e. within a block (or, if used outside of a block, a file), use of $foo will refer to the variable declared with my $foo)

      You may have been thinking that under use strict 'vars', you need to declare variables with my before you can refer to them, but that's not really true (e.g. you can use vars -- for the skinny, see strict (even better the man page on your system) or tye's writeup strict.pm)

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        I'm aware that perl has locally global variables.
        However I thought the poster referred to globals in VB, which as I recall if declared in a module can be package globals in all packages simultaneously. However my recall may be incorrect since I have seen the light of the holy Perl and left icky VB behind (in other words I haven't used it for a while). Any way this would mean that one instance of the global var $foo is accessible in all packages without using a fully qualified name. This can be useful for a very common function, status variable or a useful constant. However perl does not have global globals and that is the question I was answering.