develooper Front page | perl.perl6.language | Postings from May 2005

RE: Declaration of my() variables using symbolic referentiation

Thread Previous
From:
Joe Gottman
Date:
May 21, 2005 04:46
Subject:
RE: Declaration of my() variables using symbolic referentiation
Message ID:
200505211145.j4LBjn0V025506@ms-smtp-02-eri0.southeast.rr.com


> -----Original Message-----
> From: news [mailto:news@sea.gmane.org] On Behalf Of Ingo Blechschmidt
> Sent: Saturday, May 21, 2005 7:22 AM
> To: perl6-language@perl.org
> Subject: Declaration of my() variables using symbolic referentiation
> 
> Hi,
> 
> am I correct in the assumption that the following is an error?
>   # Not in a BEGIN block
>   my $::(calc_varname()) = 42;
> 
> I think so, as my() is a compile-time operation, but in this
> example, the variable name is not known until runtime, so I
> think this should be forbidden.  Correct?
> 
> But:
>   BEGIN {
>     my $::(calc_varname()) = 42;
>   }
> I think this one is ok, as the compiler can invoke
> &calc_varname at compile-time, and therefore know the variable
> name at compile-time.  Correct?
> 
> FWIW, I wouldn't mind BEGIN { my $::(...) } being disallowed, too
> (consistency).
> 

   Even if it's legal it's fairly useless because $::(calc_varname()) goes
out of scope at the end of the BEGIN block.  You could probably write a
macro to accomplish what you want:

   macro declare_var(String $varName) {"\$$varName"}

Then you wouldn't even need the BEGIN block.
my declare_var(calc_varname()) = 42;

The compiler wouldn't know the variable name until runtime, but I think this
just means that this just transforms compile-time errors to runtime errors.

Joe Gottman


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About