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

Hi Monks

I have created a package to export an array. I use the array in another package in a hash reference. In a test script, I use the Data::Dumper module, but the exported array doesn't want to print. How do I go about this?

Here is the code in the package:

package My_Stuff; use strict; use Data::Dumper; require Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw (@ary); my @ary = ( rec_length => 'A4', fname => 'A20', lname => 'A20', ); 1;

Here is the second package

package Record_Stuff; use strict; use Data::Dumper; use My_Stuff; my $rec_struct = [ @ary, title => 'A4', initials => 'A3', street => 'A40', ]; sub get_record_structure { return $rec_struct; } 1;

Here is the script to test it

use strict; use Data::Dumper; use Record_Stuff; print Dumper Record_Stuff::get_record_structure();

This is what I get printed out

$VAR1 = [ title, 'A4', initials, 'A3', street, 'A40', ];

Replies are listed 'Best First'.
Re: Using exported symbols
by broquaint (Abbot) on Jan 19, 2004 at 17:12 UTC
    The exported symbols need to live in the current package e.g
    use vars '@ary'; @ary = ( rec_length => 'A4', fname => 'A20', lname => 'A20', );
    This is because lexical variables don't live in symbol tables (they live in a private 'lexical pad'), which is what Exporter works with.
    HTH

    _________
    broquaint

Re: Using exported symbols
by Roy Johnson (Monsignor) on Jan 19, 2004 at 17:11 UTC
    I don't think you can export a my variable. Try our @ary.

    The PerlMonk tr/// Advocate