Link to home
Start Free TrialLog in
Avatar of hank1
hank1

asked on

eval array name 2, and 'my'

When I declare three global arrays in a mod B.pm thus

@eth;
@meth;
@prop;

the following works just fine.  

    foreach $n (qw(eth meth prop)) {
      foreach $t (eval "\@$n") {
        print $t;
    }}

    # just be sure
    print $eth[0];
    print $eth[1];
    print $eth[2]

If I declare the arrays with 'my'

my @eth;
my @meth;
my @prop;

The eval fails. The 'hard' coded indexed prints do not.  What's going on?
Avatar of Adam314
Adam314

what error do you get?
Avatar of hank1

ASKER

I didn't get anything!  I also put a print inbetween the 2 foreach structs -
just to  insure it was motoring.  Was.
Avatar of hank1

ASKER

Also might add that this foreach loop is within another sub in the module B.pm

For now I just took out my my's :-)  and it works just fine.


Avatar of hank1

ASKER

or is that fine fine.  TOO much coffee.
Avatar of ozo
where did you set $eth[0]?
Avatar of hank1

ASKER

Was set within B.pm by a call from the main script.  Package names differ.
Could you show a complete example of code that fails?
Avatar of hank1

ASKER

Here are two files that demonstrate the 'my'.  "x.pl"
call two subs in "HPLC.pm".  When the array @note is
'my'd, the sayNotes() eval creates no note array output.  

#--------------------------- x.pl
  use HPLC;
  HPLC::demofill();
  HPLC::sayNotes();


#--------------------------- HPLC.pm
  package HPLC;
  my $bug = 0;
  $bug = 1;

  BEGIN {
    use Exporter ();
    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
    $VERSION = 1.02;
    @ISA =         qw(Exporter);
    @EXPORT =      qw();
    %EXPORT_TAGS = ();
    @EXPORT_OK =   qw();
  }
  our @EXPORT_OK;

   @reagent;
   @created;
   @type;  
   @type2;
   my @note;

   @line_items;


  sub
  demofill {

    $note[0] = "Note one.";
    $note[1] = "Note two.";
    $note[2] = "Note three.";
    $note[3] = "Note four.";

  }

  # ------------------------------------------------------------------------
  sub
  sayNotes {
    foreach my $n (qw(reagent created type type2 note)) {
      $bug and print qq/array name: "$n"\n/;
      foreach my $val (eval "\@$n") {
        print qq/Value of notes $n is  $val\n\n/;
      }
    }

    #print $note[0] . "\n";
    #print $note[1] . "\n";
    #print $note[2] . "\n";
    #print $note[3] . "\n";

  }

  1;
 sub
  sayNotes {
    foreach my $n (qw(reagent created type type2 note)) {
      $bug and print qq/array name: "$n"\n/;
      foreach my $val (eval "\@$n") {
        print qq/Value of notes $n is  $val\n\n/;
      }
    }

    print $note[0] . "\n";
    #print $note[1] . "\n";
    #print $note[2] . "\n";
    #print $note[3] . "\n";

  }
or
 our @note;
Avatar of hank1

ASKER

Yes, does seem to work with 'our' ..  but it also works without 'my' and with
local.  I'll have to readup on 'our'.  Hope the definition of 'our' explains why
'my' doesn't work.  Thanks
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial