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.

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Also check out my sisters artwork and my weekly webcomics

In reply to Re: set proto string by cavac
in thread set proto string by vincentaxhe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.