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

Hello Monks. I have this code:
package common_things; use 5.008008; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); my @EXTHDS = ( "/media/disk", "/media/disk-1", "/media/disk-2", "/media/disk-3", "/media/disk-4", "/media/disk-5" ); our %EXPORT_TAGS = ( 'all' => [ qw( @EXTHDS ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( @EXTHDS ); our $VERSION = '0.01'; 1;
and this file test.pl
#!/usr/bin/perl -w use strict; use lib('.'); use common_things ':all'; use Data::Dumper; print Dumper(@EXTHDS);
But when I run test.pl I get no output at all! What gives?

Replies are listed 'Best First'.
Re: How do I access an array in a Module?
by pjf (Curate) on Sep 13, 2008 at 08:51 UTC

    Since your variable is declared with my @EXTHDS, it's only visible until the end of the current file (or block or eval). This means that the Exporter module can't see it, which means that it can't export it.

    If you declare your array with our @EXTHDS then it becomes a package variable, which Exporter can see, and the rest of your code should work fine.

    All the best,

Re: How do I access an array in a Module?
by chromatic (Archbishop) on Sep 13, 2008 at 05:29 UTC

    You can't export lexical variables this way; you'd have to write your own custom import() method to perform typeglob assignment. Otherwise you can export package global variables.

Re: How do I access an array in a Module?
by Fletch (Bishop) on Sep 13, 2008 at 13:07 UTC

    Coping with Scoping may be of use straightening out the difference between lexical and package variables.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: How do I access an array in a Module?
by Anonymous Monk on Sep 13, 2008 at 05:25 UTC
    Oh I want this ...
    use constant EXTHDS => qw( "/media/disk" "/media/disk-1" "/media/disk-2" "/media/disk-3" "/media/disk-4" "/media/disk-5" );
    instead of declaring the array @EXTHDS. I guess.
      You almost surely do not want that. qw// splits its arguments on whitespace, so you will get an array whose 0th item is "/media/disk", not /media/disk. You can use qw// without the quotes, or drop qw// and insert commas to just construct a list directly, to get what you probably expect.

      UPDATE: On further thought, notice that constants are functions, not scalars or arrays (which is why you can't interpolate them). Thus, while you can probably export them using Exporter, it's a function (hence must be exported as a bareword, or with preceding &), not an array, that you'll be exporting. As others have mentioned, it's probably more straightforward just to make it a package variable (with our) so that you can export it directly.