reload sub but call an old one from the new

  • Thread starter Peter Vereshagin
  • Start date
P

Peter Vereshagin

I have a sub in a package and I need to tweak its environment before it to be called.
I supposed I could do it klike this:
*old_sub = *some_sub;
sub some_sub{
$ENV{ FOO } = 'BAR';
&old_sub( @_ );
}

But it doesn't work.
Looking at Sub::Override I can say it is possible but the real thing spoils from me on it source code. I cannot use Sub::Override for this either because I need to use the replaced sub inside the overriding one.
The whole thing is on the sources below:
=== Test07.pm ===
package Test07;

use strict;
use warnings;

use Test09;

sub testing07{
print $Test09::test09."07\n";
}

1;
=== Test08.pm ===
package Test08;

use strict;
use warnings;

1;

package Test07;

use strict;
use warnings;

sub testing07{
print "EFGH"."07\n";
}

1;

package Test11;

use strict;
use warnings;

use base qw/Test07/;

*mod_test07 = *Test07::testing07;

1;
=== Test09.pm ===
package Test09;

use strict;
use warnings;

our $test09 = "ABCD";

1;
=== test10.pl ===
#!usr/bin/perl

use strict;
use warnings;

use lib qw/./;

use Test08;

Test11::mod_test07;
=== output of test10.pl ===
Subroutine testing07 redefined at Test07.pm line 8.
ABCD07
===

The what I need here for Test07 package is:
===
sub testing07{
$Test09::test09 = "EFGH";
some_old_testing07;
}
===
to have "EFGH07" as the output. Is it any way possible? I can achieve this by something like:
===
sub testing07{
$Test09::test09 = "EFGH";
Test11::mod_test07; # the old testing07() is saved here
}
===

but this is a pretty bad style, isn't it?

Thank you.


73! Peter pgp: A0E26627 (4A42 6841 2871 5EA7 52AB 12F8 0CE1 4AAC A0E2 6627)
 
T

Ted Zlatanov

PV> I have a sub in a package and I need to tweak its environment before it to be called.
PV> I supposed I could do it klike this:
PV> *old_sub = *some_sub;
PV> sub some_sub{
PV> $ENV{ FOO } = 'BAR';
PV> &old_sub( @_ );
PV> }

PV> But it doesn't work.
PV> Looking at Sub::Override I can say it is possible but the real thing
PV> spoils from me on it source code. I cannot use Sub::Override for
PV> this either because I need to use the replaced sub inside the
PV> overriding one.

Have you tried Aspect from CPAN? Its "advice" facility is, I think,
what you're looking for.

Ted
 
P

Peter Vereshagin

God love is hard to find. You got lucky Ted!
2010/06/01 13:27:19 -0500 Ted Zlatanov <[email protected]> => comp.lang.perl.misc:

PV>> *old_sub = *some_sub;
PV>> sub some_sub{
PV>> $ENV{ FOO } = 'BAR';
PV>> &old_sub( @_ );
PV>> }


TZ> Have you tried Aspect from CPAN? Its "advice" facility is, I think,
TZ> what you're looking for.

Very likely, thanks a lot!

73! Peter pgp: A0E26627 (4A42 6841 2871 5EA7 52AB 12F8 0CE1 4AAC A0E2 6627)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top