File: FindVariable.pm

package info (click to toggle)
libdevel-repl-perl 1.003026-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 464 kB
  • ctags: 106
  • sloc: perl: 1,939; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,305 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use strict;
use warnings;
package Devel::REPL::Plugin::FindVariable;
BEGIN {
  $Devel::REPL::Plugin::FindVariable::AUTHORITY = 'cpan:PHAYLON';
}
$Devel::REPL::Plugin::FindVariable::VERSION = '1.003026';
use Devel::REPL::Plugin;
use namespace::autoclean;

sub find_variable {
    my ($self, $name) = @_;

    return \$self if $name eq '$_REPL';

    # XXX: this code needs to live in LexEnv
    if ($self->can('lexical_environment')) {
        return \( $self->lexical_environment->get_context('_')->{$name} )
            if exists $self->lexical_environment->get_context('_')->{$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);

    # Class::MOP::Package::has_package_symbol method *requires* a sigil
    return unless length($sigil) and $meta->has_package_symbol("$sigil$name");
    $meta->get_package_symbol("$sigil$name");
}

1;

__END__

=head1 NAME

Devel::REPL::Plugin::FindVariable - Finds variables by name

=head1 VERSION

version 1.003026

=head1 AUTHOR

Shawn M Moore, C<< <sartak at gmail dot com> >>

=cut