Richard Trahan wrote:
> Consider the following code:
>
> package mypackage;
> $x = 7;
> otherpackage::mysub($x);
> ...
> package otherpackage;
> sub mysub
> {
> # retrieve variable name here
> }
>
> In mysub, I would like to retrieve the name mypackage:
, as well as
> the variable type (SCALAR). How do I do that? I know how to use
> caller() to retrieve the caller's package, and I know how to iterate
> through the caller's symbol table to retrieve symbolic names
> and existence of types, but I don't know how to associate one
> of them with the alias to $x in mysub, i.e., $_[0].
if ( \${"${caller}::$name"} == \$_[0] ) {
print "First argument to mysub is \$$name in package $caller\n";
}
> Any help, please.
More helpfull would be the advice "Don't do this". Package variables
are very rare in modern well written Perl code, most variables are
lexicially scoped so won't have a name in the symbol table. Some, of
course, could have more than one.
What are you trying to achive? This smells distictly X-Y.
If your subroutine really must take an argument that is a symbol table
entry (which I think is unlikely) then it is better to make it
explicitly do so. Actually I hate psudo-GLOBs so I'd pass a GLOBref.
$x = 7;
otherpackage::mysub(\*x);