We help IT Professionals succeed at work.

eval array name 2, and 'my'

hank1
hank1 asked
on
Medium Priority
163 Views
Last Modified: 2008-03-17
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?
Comment
Watch Question

Top Expert 2009

Commented:
what error do you get?

Author

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

Author

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.


Author

Commented:
or is that fine fine.  TOO much coffee.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
where did you set $eth[0]?

Author

Commented:
Was set within B.pm by a call from the main script.  Package names differ.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Could you show a complete example of code that fails?

Author

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;
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
 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";

  }
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
or
 our @note;

Author

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
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.