For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > May 2004 > exporting variables from modules?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author exporting variables from modules?
Anthony

2004-05-27, 3:31 pm

Hello,

I'm reading the modules documentation here:

http://perldoc.com/perl5.8.4/pod/pe...ml#Perl-Modules

Right below the example module code there, it says this:

use Cwd; # import names from Cwd::
$here = getcwd();

However, I can't get that to work -- I can't access anything exported
from my module unless I prefix it with ModuleName::, and I'm just
copying and pasting the code from that perldoc page. Any idea what I
might be missing?

Thanks,
Anthony
http://nodivisions.com/
Gunnar Hjalmarsson

2004-05-27, 3:31 pm

Anthony wrote:
> I'm reading the modules documentation here:
>
> http://perldoc.com/perl5.8.4/pod/pe...ml#Perl-Modules
>
> Right below the example module code there, it says this:
>
> use Cwd; # import names from Cwd::
> $here = getcwd();
>
> However, I can't get that to work -- I can't access anything
> exported from my module unless I prefix it with ModuleName::, and
> I'm just copying and pasting the code from that perldoc page. Any
> idea what I might be missing?


You are probably missing that in order to be able to import names by
use'ing a module, without stating them explicitly, the module needs to
export the names by default, i.e. they need to be included in the
@EXPORT array.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jim Keenan

2004-05-27, 7:31 pm


"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:2hmqrrFeoluiU1@uni-berlin.de...
> Anthony wrote:
>
> You are probably missing that in order to be able to import names by
> use'ing a module, without stating them explicitly, the module needs to
> export the names by default, i.e. they need to be included in the
> @EXPORT array.
>

However, the documentation for Cwd.pm explicitly states that function
getcwd() is exported by default:

By default, [Cwd] exports the functions cwd(), getcwd(), fastcwd(), and
fastgetcwd() into the caller's namespace.

The code from inside Cwd (v2.04):

use base qw/ Exporter /;
our @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);

OP: Can you post more of the code with which you're having a problem?

Jim Keenan


Gunnar Hjalmarsson

2004-05-27, 7:31 pm

Jim Keenan wrote:
> Gunnar Hjalmarsson wrote:
>
> However, the documentation for Cwd.pm explicitly states that
> function getcwd() is exported by default:


Yes, but since the OP talked about "my module", I assumed that his
problem is not about using Cwd.pm...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Anthony

2004-05-28, 3:31 am

orders@nodivisions.com (Anthony) wrote in message news:<535ab17d.0405271002.28a331c1@posting.google.com>...
> I'm reading the modules documentation here:
>
> http://perldoc.com/perl5.8.4/pod/pe...ml#Perl-Modules
>
> Right below the example module code there, it says this:
>
> use Cwd; # import names from Cwd::
> $here = getcwd();
>
> However, I can't get that to work -- I can't access anything exported
> from my module unless I prefix it with ModuleName::, and I'm just
> copying and pasting the code from that perldoc page. Any idea what I
> might be missing?
>
> Thanks,
> Anthony
> http://nodivisions.com/


Thanks for the suggestions, everyone. It turns out that you need to
declare the package using its fully-qualified name in order to NOT
need the fully-qualified name on the exported variables.

For example, if you name your package Foo/Bar.pm, and put this inside
it:


---------------------------
package Bar;
....
@EXPORT qw($Var);
---------------------------


....then you can access the exported variable only as $Bar::Var. But
if you do this instead:


---------------------------
package Foo::Bar;
....
@EXPORT qw($Var);
---------------------------


....then you can access the exported variable as simply $Var.

I didn't see this mentioned in the perlmod page, but maybe I didn't
read it closely enough. But I did discover it while googling, shortly
after posting this message.

-Anthony
http://nodivisions.com/
Malcolm Dew-Jones

2004-05-28, 4:31 am

Anthony (orders@nodivisions.com) wrote:
: orders@nodivisions.com (Anthony) wrote in message news:<535ab17d.0405271002.28a331c1@posting.google.com>...
: > I'm reading the modules documentation here:
: >
: > http://perldoc.com/perl5.8.4/pod/pe...ml#Perl-Modules
: >
: > Right below the example module code there, it says this:
: >
: > use Cwd; # import names from Cwd::
: > $here = getcwd();
: >
: > However, I can't get that to work -- I can't access anything exported
: > from my module unless I prefix it with ModuleName::, and I'm just
: > copying and pasting the code from that perldoc page. Any idea what I
: > might be missing?
: >
: > Thanks,
: > Anthony
: > http://nodivisions.com/

: Thanks for the suggestions, everyone. It turns out that you need to
: declare the package using its fully-qualified name in order to NOT
: need the fully-qualified name on the exported variables.

: For example, if you name your package Foo/Bar.pm, and put this inside
: it:


: ---------------------------
: package Bar;
: ...
: @EXPORT qw($Var);
: ---------------------------


: ...then you can access the exported variable only as $Bar::Var. But
: if you do this instead:


: ---------------------------
: package Foo::Bar;
: ...
: @EXPORT qw($Var);
: ---------------------------


: ...then you can access the exported variable as simply $Var.


This is wrong.

There is no such concept as "declare the package using its fully-qualified
name". A package has a name - either you used that name, or you didn't.

In your first example you didn't use the correct package name. You could
have used "package Monkey;" if you wished, it would have had the identical
result.

The second example works because you used the correct package name.

The file name Foo/Bar.pm tells the module loader than you are going to
provide a module in the Foo::Bar name space. The export correctly
exported variables in that name space in both examples. However, in the
first example you didn't provide any variables in that name space, and so
nothing was exported.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com