reload sub but call an old one from the new

Discussion in 'Perl Misc' started by Peter Vereshagin, Jun 1, 2010.

  1. 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)
    --
    http://vereshagin.org
     
    Peter Vereshagin, Jun 1, 2010
    #1
    1. Advertising

  2. Peter Vereshagin

    Ted Zlatanov Guest

    On Tue, 1 Jun 2010 11:17:35 +0000 (UTC) Peter Vereshagin <> wrote:

    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
     
    Ted Zlatanov, Jun 1, 2010
    #2
    1. Advertising

  3. God love is hard to find. You got lucky Ted!
    2010/06/01 13:27:19 -0500 Ted Zlatanov <> => 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)
    --
    http://vereshagin.org
     
    Peter Vereshagin, Jun 2, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua Beall
    Replies:
    26
    Views:
    220,634
    AndreiKaa
    Jun 29, 2007
  2. jakester
    Replies:
    3
    Views:
    1,833
    jakester
    Apr 3, 2007
  3. Ben
    Replies:
    2
    Views:
    898
  4. John Henry
    Replies:
    24
    Views:
    1,016
    alex23
    May 30, 2008
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,043
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page