2

I would like to do:

$array_ref = [ 1, 2, 3];
alias @array @$array_ref;
$array[0] = 4;
# $array_ref->[0] should now be 4

In other words: I have a reference to an array, but I would like to address it as if it was a normal array.

Background

This is a for a potential extension of GNU Parallel. In GNU Parallel the user can submit a perl expression to be evaled on the internal data structures. Accessing the array of the args is cumbersome to say the least, so I would like to make it possible simply to use @arg instead.

2
  • 4
    "Accessing the array of the args is cumbersome to say the least" I don't see what's cumbersome about $array_ref->[0], @$array_ref, and @{$array_ref}[0,2,4]` Commented Aug 7, 2015 at 6:38
  • stackoverflow.com/a/12959090/4248931 Commented Aug 7, 2015 at 8:29

1 Answer 1

2

Data::Alias does what you're looking for.

use Data::Alias;
my $array_ref = [1, 2, 3];
my @array;
alias @array = @$array_ref;
$array[0] = 4;
print $array_ref->[0];

Output: 4

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

2 Comments

Is there a module that does not require a C-compiler (i.e. no .xs)?
I don't know. Check out serenesat's comment on your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.