Date sent:              Mon, 04 Feb 2002 11:27:38 -0800
From:                   Tarak Parekh <[EMAIL PROTECTED]>
> I was a little confused regarding scopes of packages. I did read
> portions of 
> Programming Perl but am still a little unclear.
> 
> Basically, I am trying to implement a simple form of RPC in which a
> command takes in a function name and passes it across to a daemon. The
> daemon using symbolic referencing invokes the function.
> 
> the daemon sources in 3 libraries (use A; use B; use C;)
> Let's say A contains functions - foo, bar
> and   B contains functions - foofoo, barbar
>       C contains functions - morefoo, morebar
> 
> The daemon's top few lines look like
> ----------
>    package daemon;
> 
>    use strict;
> 
>    use A;
>    use B;
>    use C;
> 
> A.pm's top few lines look like
> ----------
>    package A;
> 
>    use strict;
> 
>    use vars qw(@ISA @EXPORT);

You forgot to 
        require Exporter;

>    @ISA       = qw (Exporter);
>    @EXPORT    = qw (foo bar);
> 
>    sub foo
>    {
>        print "foo called\n";
>    }
> 
>    sub bar 
>    {
>        print "bar called\n";
>    }
> 
> The questions I had were
> 1. To invoke a function (for e.g. foo) - what do i send across from
> the client
>    command ? - daemon::foo  ? A::foo ? or just foo ? 

Either A::foo or foo.

Since the A package uses Exporter and includes the foo() in the 
@EXPORT list whenever you "use A" the foo() gets exported to 
current package ... that is an alias is made so that foo() is the 
same as A::foo().

> - I guess my
> question is
>    once you 'use' a package what happens to all the symbols from that
> package.
>    Do they become part of the importing package ?

Depends. They do not have to.

1) If the package uses Exporter and you did not specify any 
parameters to the "use A" statement all functions and variables 
mentioned in @A::EXPORT are imported.

2) If you do use a parameter like this: 
        use A qw(foo);
then Exporter looks whether A agrees with exporting foo() = looks 
whether @EXPORT_OK has an element "foo" and if it does imports 
ONLY that function. Otherwise gives you an error.

3) If neither the package or the packages specified in @ISA use 
Exporter or a similar module and doesn't contain import() function 
.... nothing gets imported at all.

4) If the package or one of those in @ISA contains an import() 
function ... then the package may export anything it likes. Read it's 
docs.

> 2. A followup question is that if B and/or C had the same name
> subroutines
>    as A.pm did then what would happen.

If you were running the script with -w parameter you'd get a warning:

        Subroutine foo redefined at ... line ....

In that case you'd most probably want to set @EXPORT_OK and 
only import the functions you want from each package.

Hope that makes sense, Jenda

=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
                                        --- me

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to