in reply to Re: Lexical use/no Module?
in thread Lexical use/no Module?
The problem is, Hook::LexWrap isn't actually lexically scoped. As Dominus says in his proposal, Hook::LexWrap is lying (which isn't to say it's a bad module at all, it's just not *quite* lexically scoped).
For example,
Basically, theres' no good way to implement truly lexically-scoped modules right now. However:{ my $lexical = wrap 'some_sub', pre => \&wrapper; some_sub();#wrapper is called before some_sub other_sub();#other_sub's call to some_sub is *still* #wrapped with &wrapper, even though other_sub is #outside of this lexical scope! } sub other_sub { some_sub();#call made outside of lexical scope in which some_sub is +wrapped. }
Dominus is working to change this. Here's the HTMLified POD from his proposal patch for Proper Lexical Pragma Modules.:
NAME
pragma - utility functions for lexically-scoped pragmas
SYNOPSIS
package MyPragma; use pragma;sub import { my ($class, $value) = @_;# check $value here...install_pragma_value($class, $value); }1;
ABSTRACT
This should be the abstract for pragma. The abstract is used when making PPD (Perl Package Description) files. If you don't want an ABSTRACT you should also edit Makefile.PL to remove the ABSTRACT_FROM option.
DESCRIPTION
Every statement in a Perl program has a list of 'pragmas' associated with it. The pragmas are hints to modules or to the Perl interpreter that certain types of behavior should be changed. For example, a pragma might indicate to Perl that the strict 'vars' checks should be performed for that statement.
Each pragma has a name, which is a string, and perhaps some associated data. The data can be anything, but this module assumes that the data will be a regular Perl scalar (which, of course, could be a reference to a more complex data structure).
Pragmas must be set at compile time. They can be looked up at run time. See perlpragma for complete details about the internals of pragmas and the C interface to pragmas.
Pragmatic modules may inherit the import subroutine from the pragma module itself, which performs the appropriate installation. If you want to define a pragma called mypragma, create a mypragma.pm file as indicated in the SYNOPSIS section above. Then any other part of the program may use
{ ... use mypragma "Carrots"; ... }This declares that the pragma mypragma is in scope to the end of the enclosing block. The name of the pragma is 'mypragma'; the value is "Carrots". The value "Carrots" will not be used by any module other than the mypragma module.
A more typical use of the pragma value would be:
{ ... use mypragma "on"; ... use mypragma "off"; ... }Or perhaps:
{ ... use strict 'vars'; ... use strict 'refs', 'subs'; ... }
EXPORT
- install_pragma_value($name, $value)
- This function associates a pragma with rest of the block that is presently being compiled. The scope of the pragma is from the statement currently being compiled to the end of the enclosing block.
The $name may be used to retrieve the specified $value, which has no special meaning
install_pragma_value should be called at compile time, from within an import subroutine. .
- current_pragma_value($name, $value)
- Looks in the pragma list for the currently executing statement for the first pragma named $name. If it finds one, it returns the corresponding $value; otherwise, it returns undef.
- caller_pragma_value($name, $value)
- Like current_pragma_value, but for the currently executing statement in the calling function instead of the current function.
SEE ALSO
perlvar/``%^H''
AUTHOR
Mark Jason Dominus, <mjd@plover.com>
COPYRIGHT AND LICENSE
Copyright 2003 Mark Jason Dominus.
This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Lexical use/no Module?
by stvn (Monsignor) on Jan 29, 2004 at 22:20 UTC |