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?
LVL 1
hank1Asked:
Who is Participating?
 
ozoCommented:
It seems to work with my if you also use the array in the sub not in an eval
it seems to be more a peculiarity of using eval.
0
 
Adam314Commented:
what error do you get?
0
 
hank1Author Commented:
I didn't get anything!  I also put a print inbetween the 2 foreach structs -
just to  insure it was motoring.  Was.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
hank1Author Commented:
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.


0
 
hank1Author Commented:
or is that fine fine.  TOO much coffee.
0
 
ozoCommented:
where did you set $eth[0]?
0
 
hank1Author Commented:
Was set within B.pm by a call from the main script.  Package names differ.
0
 
ozoCommented:
Could you show a complete example of code that fails?
0
 
hank1Author Commented:
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;
0
 
ozoCommented:
 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";

  }
0
 
ozoCommented:
or
 our @note;
0
 
hank1Author Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.