Skip to content

Instantly share code, notes, and snippets.

@jberger
Created December 31, 2011 07:19
Show Gist options
  • Save jberger/1543221 to your computer and use it in GitHub Desktop.
Save jberger/1543221 to your computer and use it in GitHub Desktop.
A basic use_from implementation
package UseX::Declare;
use strict;
use warnings;
use Carp;
use Devel::Declare ();
my $verbose = 0;
sub import {
my $class = shift;
my $opts = shift;
$verbose = $opts->{verbose} || 0;
my $caller = caller;
Devel::Declare->setup_for(
$caller,
{ 'use_from' => { const => \&rewrite_use_from } }
);
no strict 'refs';
*{$caller.'::use_from'} = sub {};
}
sub rewrite_use_from {
no strict 'refs';
my $linestr = Devel::Declare::get_linestr;
print STDERR "Got: $linestr" if $verbose;
my $caller = Devel::Declare::get_curstash_name;
$linestr =~ s/use_from\s+\$(\w+)/
my $varname = $caller . '::' . $1;
my $module =
(defined ${$varname})
? ${$varname}
: croak "Cannot access variable \$$varname";
"use_from; use $module"
/e;
print STDERR "Rewritten: $linestr" if $verbose;
Devel::Declare::set_linestr($linestr);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment