You cannot use a lexically scoped array as a parameter, because lexical variables don't have typeglobs associated with them.
Lexical variables won't be in symbol tables. | [reply] [Watch: Dir/Any] |
Was this meant as an example to illustrate questions about how to work with globs or are you simply trying to pass a value into a function doublevalue(...)? The sample you have posted looks like a very old Perl coding style - what version of Perl are you working with?
If the point was to pass values, the current way to pass values is to use variables and references, like this:
use strict;
use warnings;
use Data::Dumper;
{
my @arra = (10,20);
#no need for & and you can just pass a reference
#&doublevalue(*arra);
doublevalue(\@arra);
}
sub doublevalue {
#my is preferred to local
#local *a = shift ;
my $a = shift;
print Dumper @$a;
}
If you want to learn more about how type globs work, see perlref.
Best, beth | [reply] [Watch: Dir/Any] [d/l] [select] |
&doublevalue(\@arra);
This seems to work just fine for me, unless I'm missing something. | [reply] [Watch: Dir/Any] [d/l] |
you need to use references, see print shift; for clues | [reply] [Watch: Dir/Any] [d/l] |
You can pass in an ordinary reference to a subroutine expecting a type glob, and it will work fine.
| [reply] [Watch: Dir/Any] |
nagalenoj gave you the correct info; I'm just stating it another way. Your my @arra and your *arra are different and unrelated variables. The *arra is a package variable *main::arra which can, as a typeglob, refer to the variable @main::arra but that has nothing to do with a lexical, aka my, variable named @arra.
You are passing an empty typeglob to doublevalue.
This demonstrates that you are passing nothing, loosely speaking, to your function:
my @arra = (10,20);
print ">@main::arra<$/";
print ">>@arra<<$/";
Update: With your code, use warnings; would have flagged that the name main::arra was used only once, hinting at the problem. With use diagnostics; the message is more confusing as if it were written before lexical variables were added to the language.
Be well,
rir
| [reply] [Watch: Dir/Any] [d/l] [select] |