2

I have arraytest.pm with below

package arraytest;

our @myarray = {"1", "2", "3", "4"};

and i'm trying to get the values from a different perl script

use arraytest;

foreach (@arraytest::myarray) {
  print "$arraytest::myarray\n";
}

I get nothing printed and no errors as well. Pleas let me know if referencing the array is correct?

Thanks in advance

1
  • use warnings; to get more errors to be printed Commented Jul 29, 2013 at 2:55

3 Answers 3

5

Yer array syntax is wrong.

Perl does not have array literals. It has

  • Lists: 1, 2, 3, 4
  • Array references literals: [1, 2, 3, 4]
  • Hash reference literals: {1 => 2, 3 => 4}.

Hashes and arrays can be initialized by assigning lists.

Instead, you intitialized your array to contain one hash reference as first element. This is equivalent to

my %hash = (1 => 2, 3 => 4);
our @myarray = (\%hash);

What you meant to do was

our @myarray = (1, 2, 3, 4);  # parens needed because of precedence

or

our @myarray = 1 .. 4;

Oh, and please do not use lowercase names for your packages. These are reserved for pragmatic modules that affect compilation, like strict or feature.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion on naming the packages and braces. I got it working now.
3

Always use

use strict;
use warnings;

The problem is that you are trying to write content of $arraytest::myarray variable which doesn't exist at all. Try this:

foreach my $val (@arraytest::myarray) {
  print "$val\n";
}

and also your array doesn't contain what you expect. You have hash ref in first cell of array. You should use:

our @myarray = ("1", "2", "3", "4");

3 Comments

Thanks for your response. I tried this, but I get a hash output instead of the items in the array HASH(0x4f8360)
@user2628187 That's because you are initializing your array to conatain a hash reference (see my answer for details). You get such output when you print a hashref. The last snippet in Hynek -Pichi- Vychodil's answer shows you how to correctly initialize your array (use parens, not braces).
Sorry It was my mistake .. was using {"1", "2", "3", "4"}; instead if ("1", "2", "3", "4");... It worked thanks a lot
0

You trying achieve something like:

use 5.012;
use warnings;

package MyArr;
our @arr = (1..10);

package main;
for (@MyArr::arr) {
    say "$_";
}

#or like above with a helper variable
for my $val (@MyArr::arr) {
    say "$val";
}

Ps: the above is usually not a good practice. Use objects instead.

Edit - @dave's comment

use 5.012;
use warnings;

package MyArr {
    our @arr = (1..10);
}

package main {
    for (@MyArr::arr) {
        say "$_";
    }

    for my $val (@MyArr::arr) {
        say "$val";
    }
}

3 Comments

Thanks for your input and info on object.
Because our has a lexical effect, you don't need to use the fully qualified name (@MyArr::arr) in either of your for loops. Switching from package MyArr to package main does not create a new lexical scope. To force the need for the fully qualified name (which is what you're trying to demonstrate here) you need to put a naked block around the two lines of the MyArr package.
@DaveCross of course, youre right. But, the OP asking for different perl script. Sure, i can write the two file example, one for package MyArr and second file for main and require the MyArr from the main - but this was not the main point of the question... (imho)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.