David Filmer (
[email protected]) wrote:
: The fact that my last posting seems very clear to me but not to you is
: troubling. Either I have it right but am expressing it poorly, or I
: have it wrong. I believe I'm clearly expressing my understanding of
: the subject, so I wonder if my understanding is all wrong. Please
: allow me to briefly summarize my understanding, and prehaps you will
: be kind enough to point out where I've gone astray.
: >> show some code. your words are not conveying much useful info.
: The code is at the top of the post. You didn't like it. You were
: right. I don't have any better code to post, because, as I understand
: it, what I was trying to do cannot be done (and thus cannot be coded),
: and I need to do something else.
: What I was trying to do was use a variable-as-a-variablename, and my
: original question was how I could properly scope it, because
: my $foo = 'bar';
: my @{$foo}; #ie, @bar
: doesn't work. I thought it was a syntax problem.
: What I believe you've said (and what I've concluded) is that it cannot
: be done at all (at least not without unleashing the demons of hell).
: It is flat-out impossible to scope a variable (with a 'my' statement)
: if the variable is not a hard reference. Is my understanding of this
: correct?
Yes. `my' allows the compiler, at compile time, to define a variable.
Defining a variable at compile time means various things, including doing
such things as allocating memory and assigning a name to that memory
address. When your code is running then various things have to happen,
including such things as accessing that location in memory.
If you want perl to be able to use a `my' variable then you have to give
perl all the information it needs about the variable at compile time.
That includes the name, because that is how perl knows which address in
memory holds the data that implements that variable.
If you don't want to give all the data about the variable to the compiler
at compile time (i.e. its name) then there are two things that can be
done (I doubt that either is what you need to do!)
One possibility is to use a global variable. Perl doesn't need to know
the name at compile time because it looks up the name when the program is
running. To do that, all perl has to know is where in memory the
information about names is stored, and it can calculate that at compile
time because that is simply one of the many internal data structures
created by the compiler itself.
This is very powerful, but is rarely the best way to solve a problem.
A second possibility is to generate the code when the program runs. That
way the name gets inserted into the code because the code is simply a
string, and perl doesn't compile it until you know the name. This is even
more powerful, but again, is rarely the best way to solve a problem.
E.g.
$variable_name = 'loop_data';
$my_program_segment =
" my \@$variable_name = (\"some value\");
do_something_with(\@$variable_name);
";
eval $my_program_segment;
die "$my_program_segment\n$@: $!" if $@;
I doubt that either of these are what you really need. I suspect there is
some thing about programming perl that you are misunderstanding.
Choosing which array to pass into a function should not be this difficult.
: You suggested using a hash INSTEAD of an array, correct? But I can't
: do that, because I'm trying to pass this object to a module
: (HTML::Template) which expects an array, ie:
: $template->param(THIS_LOOP => \@loop_data);
that could be written as
my $ref_to_the_correct_array =
#
# this is where I would put something to tell me which
# array I wish to use. I would show you the code but I
# don't understand what you want well enough to be able to
# do that.
#
# now use the array we just chose (sp?)
$template->param(THIS_LOOP => $ref_to_the_correct_array);