| Newsgroups |
perl.cvs.perlfaq |
| Message-ID |
<[email protected]> |
cvsuser 02/09/04 15:33:45
Modified: . perlfaq7.pod
Log:
* How can I access a dynamic variable while a similarly named lexical is in scope?
Revision Changes Path
1.10 +24 -14 perlfaq/perlfaq7.pod
Index: perlfaq7.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq7.pod,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -w -r1.9 -r1.10
--- perlfaq7.pod 21 Jun 2002 04:32:00 -0000 1.9
+++ perlfaq7.pod 4 Sep 2002 22:33:45 -0000 1.10
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq7 - General Perl Language Issues ($Revision: 1.9 $, $Date: 2002/06/21 04:32:00 $)
+perlfaq7 - General Perl Language Issues ($Revision: 1.10 $, $Date: 2002/09/04 22:33:45 $)
=head1 DESCRIPTION
@@ -480,23 +480,33 @@
=head2 How can I access a dynamic variable while a similarly named lexical is in scope?
-You can do this via symbolic references, provided you haven't set
-C<use strict "refs">. So instead of $var, use C<${'var'}>.
+If you know your package, you can just mention it explicitly, as in
+$Some_Pack::var. Note that the notation $::var is B<not> the dynamic $var
+in the current package, but rather the one in the "main" package, as
+though you had written $main::var.
+ use vars '$var';
local $var = "global";
my $var = "lexical";
print "lexical is $var\n";
+ print "global is $main::var\n";
- no strict 'refs';
- print "global is ${'var'}\n";
+Alternatively you can use the compiler directive our() to bring a
+dynamic variable into the current lexical scope.
-If you know your package, you can just mention it explicitly, as in
-$Some_Pack::var. Note that the notation $::var is I<not> the dynamic
-$var in the current package, but rather the one in the C<main>
-package, as though you had written $main::var. Specifying the package
-directly makes you hard-code its name, but it executes faster and
-avoids running afoul of C<use strict "refs">.
+ require 5.006; # our() did not exist before 5.6
+ use vars '$var';
+
+ local $var = "global";
+ my $var = "lexical";
+
+ print "lexical is $var\n";
+
+ {
+ our $var;
+ print "global is $var\n";
+ }
=head2 What's the difference between deep and shallow binding?