One kind of nasty option is to use string eval:
$varname = "\$${_}_type";
eval "$varname = <something>";
There are technical caveats and perhaps moral issues with this approach,
but it works.
-- Jeremy
On Thu, 2008-10-16 at 08:32 -0600, Palit, Nilanjan wrote:
> How (or can) I use an interpolated scalar as an lvalue?
>
> Example: I want to assign a value to a bunch of scalar variables
> $<prefix>_type:
>
> my ($abc_type, $def_type, $xyz_type);
> ...
> ...
> foreach (qw(abc def xyz)) #List of prefixes
> {
> $varname= $_."_type"; #Actual scalar variable name is $prefix_type
> ${$varname}= <something>; # <-- Problem
> }
>
> Perl complains about the second line in the foreach loop during run time:
> Can't use string ("abc_type") as a SCALAR ref while "strict refs" in use at
> test1.pl line 19.
>
>
> I wanted to understand whether this is possible somehow, i.e., can we use a
> generated string to point to a scalar variable?
>
> (I know I can use a hash instead with the prefixes as keys, but I'm modifying
> some hand-me down code for a small change & would like to avoid invasive
> changes.)
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm