Skip to content

Commit

Permalink
0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed May 18, 2011
1 parent 0ce0dc6 commit 10fd38e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Changes
@@ -1,4 +1,10 @@
---
version: 0.13
date: Thu May 19 00:01:18 EST 2011
changes:
- Support hashes, arrays, and nested refs properly
- Thanks to rafl++ and rjbs++ for pointing out the bugs
---
version: 0.12
date: Wed May 18 15:18:27 EST 2011
changes:
Expand Down
29 changes: 29 additions & 0 deletions README
Expand Up @@ -33,6 +33,35 @@ DESCRIPTION
function returns the lexicals as a hash reference (in scalar or list
context).

ARRAYS AND HASHES
The above examples deal with lexical scalars. You can also get back
lexical arrays and hashes. Note: since there is no sigil to tell scalars
from arrays from hashes, you can't get back a scalar and an array or
hash of the same name. In this case, SCALAR beats HASH beats ARRAY. Why?
Because I said so! (Actually I just used the sort order of the sigils).

sub foo {
my %h = ( O => 'HAI' );
my @a = [ qw( foo bar baz ) ];
my $s = 42;
my %x = ( O => 'HAI' );
my @x = [ qw( foo bar baz ) ];
my $x = 42;
print Dump lexicals;
}

would yield:

---
a:
- foo
- bar
- baz
h:
O: HAI
s: 42
x: 42

NOTE
The "lexicals" function only reports the lexical variables variables
that were defined before where it gets called.
Expand Down
46 changes: 39 additions & 7 deletions lib/lexicals.pm
Expand Up @@ -12,7 +12,7 @@ package lexicals;
use 5.005008;
use strict;

our $VERSION = '0.12';
our $VERSION = '0.13';

use PadWalker;

Expand All @@ -21,12 +21,14 @@ our @EXPORT = qw(lexicals);

sub lexicals {
my $hash = PadWalker::peek_my(1);
my $lex = {};
while ( my ($k, $v) = each %$hash ) {
$k =~ s/^\$//;
$lex->{$k} = $$v;
}
return $lex;
return +{
map {
my $v = $hash->{$_};
$v = $$v if ref($v) =~ m'^(SCALAR|REF)$';
s/^[\$\@\%\*]//;
($_, $v);
} reverse sort keys %$hash
};
}

1;
Expand Down Expand Up @@ -64,6 +66,36 @@ Assuming you have a $foo and $bar defined, you get the same thing.
The `lexicals` module exports a function called `lexicals`. This function
returns the lexicals as a hash reference (in scalar or list context).
=head1 ARRAYS AND HASHES
The above examples deal with lexical scalars. You can also get back lexical
arrays and hashes. Note: since there is no sigil to tell scalars from arrays
from hashes, you can't get back a scalar and an array or hash of the same
name. In this case, SCALAR beats HASH beats ARRAY. Why? Because I said so!
(Actually I just used the sort order of the sigils).
sub foo {
my %h = ( O => 'HAI' );
my @a = [ qw( foo bar baz ) ];
my $s = 42;
my %x = ( O => 'HAI' );
my @x = [ qw( foo bar baz ) ];
my $x = 42;
print Dump lexicals;
}
would yield:
---
a:
- foo
- bar
- baz
h:
O: HAI
s: 42
x: 42
=head1 NOTE
The C<lexicals> function only reports the lexical variables variables that
Expand Down
25 changes: 25 additions & 0 deletions t/type.t
@@ -0,0 +1,25 @@
use Test::More tests => 7;
use lexicals;

sub foo {
my %h = ( a => 1, b => 2);
my @a = ( 'x', 'y', 3);
my $s = 42;
my $r = \$s;
my $r2 = \\$s;
my $l = lexicals;
my $x = 7;
my @x = (3, 4);
my %x = (1, 3, 2, 1);

my $l = lexicals;
is $l->{x}, 7, 'Scalar wins on match';
is ref($l->{h}), 'HASH', 'lexical hashes are ok';
is ref($l->{a}), 'ARRAY', 'lexical arrays are ok';
is ref($l->{r}), 'SCALAR', 'lexical refs are ok';
is ref($l->{r2}), 'REF', 'lexical refs are ok';
is ref($l->{s}), '', 'lexical scalars are ok';
is ${$l->{r2}}, $l->{r}, 'references are correct';
}

foo();

0 comments on commit 10fd38e

Please sign in to comment.