Accessing lexical vars in other packages

R

RA Jones

This is a question about packages and lexical variables.

I am using a very useful $SIG{__DIE__} handler module (CGI::HTMLError),
and want to extend it's functionality by adding an e-mail alert
function. Clearly I don't want to have to re-write the module to do
this, so in my apps I 'use' my extended mod Local::HTMLError, which
itself uses CGI::HTMLError as base. All works fine, except for the fact
that the CGI errors are captured within lexical variables inside a C::H
sub. I assume I cannot access these as they are not present in the
symtable (they are declared with 'my'), and indeed
$CGI::HTMLError::sub_routine::variable does not work.

Except that I read that Perl does not necessarily recycle variables if
it detects they are referenced elsewhere. So is there any way I can
achieve what I want here - to retrieve lexical variables from another
package's sub-routines?
 
A

Anno Siegel

RA Jones said:
This is a question about packages and lexical variables.

Packages have nothing to do with lexical variables.
I am using a very useful $SIG{__DIE__} handler module (CGI::HTMLError),
and want to extend it's functionality by adding an e-mail alert
function. Clearly I don't want to have to re-write the module to do
this, so in my apps I 'use' my extended mod Local::HTMLError, which
itself uses CGI::HTMLError as base. All works fine, except for the fact
that the CGI errors are captured within lexical variables inside a C::H
sub. I assume I cannot access these as they are not present in the
symtable (they are declared with 'my'), and indeed
$CGI::HTMLError::sub_routine::variable does not work.

Except that I read that Perl does not necessarily recycle variables if
it detects they are referenced elsewhere. So is there any way I can
achieve what I want here - to retrieve lexical variables from another
package's sub-routines?

Again, any talk of packages in this context is a red herring. You'd
have the same problem if the sub that accesses the lexicals were in
"your" package, never mind what that is.

You cannot access lexical variables from outside their scope, unless
code inside the scope allows it. So, unless the code in CGI::HTMLError
provides an accessor routine (in the generic sense, not necessarily a
method) to the variable, you're out of luck.

Anno
 
P

Paul Lalli

RA said:
This is a question about packages and lexical variables.

I am using a very useful $SIG{__DIE__} handler module (CGI::HTMLError),
and want to extend it's functionality by adding an e-mail alert
function. Clearly I don't want to have to re-write the module to do
this, so in my apps I 'use' my extended mod Local::HTMLError, which
itself uses CGI::HTMLError as base. All works fine, except for the fact
that the CGI errors are captured within lexical variables inside a C::H
sub. I assume I cannot access these as they are not present in the
symtable (they are declared with 'my'), and indeed
$CGI::HTMLError::sub_routine::variable does not work.

Except that I read that Perl does not necessarily recycle variables if
it detects they are referenced elsewhere. So is there any way I can
achieve what I want here - to retrieve lexical variables from another
package's sub-routines?

If you're unable/unwilling to modify the source of CGI::HTMLError, and
the error messages really only are available as lexicals in that
package, then no, you cannot have your package grab them in any way
that I know of.

The docs for CGI::HTMLError say that it is configured to allow a
previous $SIG{__DIE__} handler to be called before doing it's thing.
So it seems like your Local::HTMLError subroutine should be able to
define a handler on its own, before importing CGI::HTMLError's. Then
the error message would be avialble via @_, just as it is in
CGI::HTMLError's handler. This is all my guess based on looking at the
docs and source, however.

Reading the docs for CGI::HTMLError, it claims to be intended as a
replacement for CGI::Carp. However, it does not seem to have all of
the functionality of CGI::Carp. CGI::Carp, for example, would allow
you to add your desired email alert subroutine without needing to write
your own module. See that module's documentation for details:
http://search.cpan.org/~lds/CGI.pm-3.10/CGI/Carp.pm#Changing_the_default_message

Paul Lalli
 
R

RA Jones

You cannot access lexical variables from outside their scope, unless
code inside the scope allows it. So, unless the code in CGI::HTMLError
provides an accessor routine (in the generic sense, not necessarily a
method) to the variable, you're out of luck.

Of course, you are correct about the irrelevance of which package we are
trying to access variables from. But it seems a solution may have been
provided in the other reply.
 
R

RA Jones

If you're unable/unwilling to modify the source of CGI::HTMLError, and
the error messages really only are available as lexicals in that
package, then no, you cannot have your package grab them in any way
that I know of.
I have no real objection to re-writing it, but wanted to use the
functionality that already existed and extend it - more of an exercise
than an absolute requirement.
Reading the docs for CGI::HTMLError, it claims to be intended as a
replacement for CGI::Carp. However, it does not seem to have all of
the functionality of CGI::Carp. CGI::Carp, for example, would allow
you to add your desired email alert subroutine without needing to write
your own module. See that module's documentation for details:
http://search.cpan.org/~lds/CGI.pm-3.10/CGI/Carp.pm#Changing_the_default
_message
That does look the best way forward, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top