Stevie-O has asked for the wisdom of the Perl Monks concerning the following question:
The 'no warnings' and 'no strict' have lexical scope; they are in effect only until the enclosing block ends. However, when I tried this:use strict; # hacksym('name', ref) # e.g. hacksym('foo', \@bar) makes @foo an alias for @bar sub hacksym { my $name = shift; my $ref = shift || die "need a reference!"; no warnings 'uninitialized'; no strict 'refs'; *{$name} = $ref; }
package Bling; use strict qw(vars subs); use warnings; # simply put: use Bling; then, whenever your script accesses $$, # it prints 'Bling Bling!!'. my $ref; sub import { unless (defined $ref) { $ref = \${'main::$'}; undef *{'main::$'}; # tramples @$ and %$. I really don't ca +re. tie ${'main::$'}, 'Bling::Bling'; } } sub unimport { if (defined $ref) { untie ${'main::$'}; *{'main::$'} = $ref; undef $ref; } } package Bling::Bling; sub TIESCALAR { bless [], shift; } sub FETCH { local $\; print "Bling Bling!!\n"; $$ref; } 1;
The result is that no 'Bling Bling!!'s are printed, because the 'no Bling' is not lexically scoped. My question is this: Is it possible to achieve 'lexicality' of use/no in my own modules, just like strict.pm and warnings.pm do?#!/usr/bin/perl -l # blingtest.pl use Bling; $x=$$; print '$x is ', $x; { no Bling; $y=$$; print '$y is ', $y; } print '$$ is ', $$;
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Lexical use/no Module?
by Abigail-II (Bishop) on Jan 29, 2004 at 01:57 UTC | |
|
Re: Lexical use/no Module?
by stvn (Monsignor) on Jan 29, 2004 at 02:48 UTC | |
by Stevie-O (Friar) on Jan 29, 2004 at 03:52 UTC | |
by stvn (Monsignor) on Jan 29, 2004 at 04:19 UTC | |
by QwertyD (Pilgrim) on Jan 29, 2004 at 19:55 UTC | |
by stvn (Monsignor) on Jan 29, 2004 at 22:20 UTC | |
|
Re: Lexical use/no Module?
by ysth (Canon) on Jan 29, 2004 at 18:48 UTC |