#!/usr/bin/perl
use warnings;
use strict;
package Foo;
sub foo {
print "1\n";
}
package Bar;
sub new {
bless {}, shift;
}
sub steal {
my ($self, $call) = @_;
my @segments = split('::', $call);
my $method = pop @segments;
my $class = join '::', @segments;
no strict 'refs';
*{__PACKAGE__ . "::$method"} = *{$class . "::$method"};
$self;
}
package main;
Bar->new->steal("Foo::foo")->foo;
Or with Sub::Install (Score:1)
I think you may do the same with Sub::Install (by rjbs). The code would be something like:
Note: Your code currently does not handle packages like 'Data::Foo::Glub' as the source.
Re: (Score:1)
Smooth! Thanks for the tip.
Note: Your code currently does not handle packages like 'Data::Foo::Glub' as the source.
Good call. I've fixed it now so that it does.
Nothing special (Score:1)
That’s just how exporting works in Perl.
You’re better off doing it like so, btw:
That affects only the subroutine slot of the destination glob, and avoids nuking it in entirety.
You’re doing a lot of pointless busywork, though:
Re: (Score:1)
Cool. All part of the learning process.
You’re better off doing it like so, btw... That affects only the subroutine slot of the destination glob, and avoids nuking it in entirety.
Yeah. I realised that a little later after some more reading on globs.
You’re doing a lot of pointless busywork, though:
I call it a first pass! The split/pop/join would most likely have become a regex the next time I looked at it. I see that it also obviates the need
Re: (Score:1)
Oh yeah. That’s a specific instance of a broader idiom that comes in many forms. An instance you have probably encountered elsewhere is
chomp( my @line = <$fh> );. It looks somewhat different, but it operates on the same principle: using parens to increase the precedence of an assignment so it happens before another part of the expression, which then operates on the lvalue of the assignment.Use it sparingly, though, as overuse of this idiom can easily degrade readability.
that's not theft (Score:2)
Re: (Score:1)
Re: (Score:1)
The theiving magpie (Score:1)