That's not how Perl (or many other programming languages) work. The variable is filled at the time the assignment is done.
What you probably want to achieve is that $proto2 is a reference to @a? Something like you've already done with $proto1.
But when you assign $proto2, you are generating a string (a temporary scalar, technically) that is assigned to $proto2. $proto2 has no concept that its new value is derived from @a. To make $proto2 behave more like you want it to (though not exactly), you can make it a reference to an anonymous subroutine:
#!/usr/bin/env perl
use v5.38;
use strict;
use warnings;
my @a;
my $proto2 = sub {
return "@{\@a} 3 4";
};
@a = (1, 2);
print $proto2->(), "\n"; # prints "1 2 3 4"
To make this more in line with your requirements (=evaluating the stuff on access), you can make $proto2 a tied variable. In this case, you also need to make @a accessible from other packages by declaring it "our" instead of "my". Basically, $proto2 is tied to a class (package) and runs its own logic:
#!/usr/bin/env perl
use v5.38;
use strict;
use warnings;
package vincentaxhe;
sub TIESCALAR { bless \my $self, shift }
sub STORE { ${ $_[0] } = $_[1] } # remember the postfix string
sub FETCH { "@{\@main::a} " . ${ my $self = shift } };
package main;
our @a;
tie my $proto2, 'vincentaxhe';
$proto2 = '3 4';
@a = (1, 2);
print $proto2, "\n"; # prints 1 2 3 4
@a = (22, 23, 24);
print $proto2, "\n"; # prints 22 23 24 3 4
$proto2 = 'Hello World';
print $proto2, "\n"; # prints 22 23 24 Hello World
There are probably smarter ways to bind @a to $proto2 in a tied variable than hardcoding it. But it's Saturday and i need to go shopping for some food (shops are closed tomorrow), so i got limited time to spend on this.
|