• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

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?
0
hank1
Asked:
hank1
  • 6
  • 5
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now