FindVariable plugin, which returns a reference to a global variable by name
Sartak [Mon, 26 May 2008 03:13:54 +0000 (03:13 +0000)]
git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-REPL@4408 bd8105ee-0ff8-0310-8827-fb3f25b6796d

lib/Devel/REPL/Plugin/FindVariable.pm [new file with mode: 0644]

diff --git a/lib/Devel/REPL/Plugin/FindVariable.pm b/lib/Devel/REPL/Plugin/FindVariable.pm
new file mode 100644 (file)
index 0000000..2b35755
--- /dev/null
@@ -0,0 +1,23 @@
+package Devel::REPL::Plugin::FindVariable;
+
+use Moose::Role;
+use namespace::clean -except => [ 'meta' ];
+
+sub find_variable {
+    my ($self, $name) = @_;
+
+    my $sigil = $name =~ s/^([\$\@\%\&\*])// ? $1 : '';
+
+    my $default_package = $self->can('current_package')
+                        ? $self->current_package
+                        : 'main';
+    my $package = $name =~ s/^(.*)(::|')// ? $1 : $default_package;
+
+    my $meta = Class::MOP::Class->initialize($package);
+
+    return unless $meta->has_package_symbol("$sigil$name");
+    $meta->get_package_symbol("$sigil$name");
+}
+
+1;
+