For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Backup of a subroutine









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Backup of a subroutine
Marcos Rebelo

2005-08-03, 10:00 pm

I need to redefine localy one subroutine and have it correctlly after.
Who do I do it?

sub myPrint () {
print "Teste 1\n";
}

sub test {
no strict "refs";
no warnings;
=20
my $str =3D "::myPrint";

# my $backup =3D $main::{myPrint};
my $backup =3D *$str;

*$str =3D sub() {
print "Teste 2\n"; =20
};
=20
myPrint;
=20
*$str =3D $backup;
}

myPrint;
test;
myPrint;
exit;

Note: This is not beautyfull but I'm doing some test-cases and I don't
want to change the code to be tested.
Marcos Rebelo

2005-08-03, 10:00 pm

sub myPrint () {
print "Teste 1\n";
}

sub test() {
no strict "refs";
no warnings;

my $subName =3D "::myPrint";

my $backup =3D \&myPrint;

*$subName =3D sub() {
print "Teste 2\n"; =20
};
=20
myPrint;
=20
*$subName =3D $backup;
}

myPrint;
test;

myPrint;

On 8/3/05, marcos rebelo <oleber@gmail.com> wrote:
> I need to redefine localy one subroutine and have it correctlly after.
> Who do I do it?
>=20
> sub myPrint () {
> print "Teste 1\n";
> }
>=20
> sub test {
> no strict "refs";
> no warnings;
>=20
> my $str =3D "::myPrint";
>=20
> # my $backup =3D $main::{myPrint};
> my $backup =3D *$str;
>=20
> *$str =3D sub() {
> print "Teste 2\n";
> };
>=20
> myPrint;
>=20
> *$str =3D $backup;
> }
>=20
> myPrint;
> test;
> myPrint;
> exit;
>=20
> Note: This is not beautyfull but I'm doing some test-cases and I don't
> want to change the code to be tested.
>

John W. Krahn

2005-08-03, 10:00 pm

marcos rebelo wrote:
> I need to redefine localy one subroutine and have it correctlly after.
> Who do I do it?
>
> sub myPrint () {
> print "Teste 1\n";
> }
>
> sub test {
> no strict "refs";
> no warnings;
>
> my $str = "::myPrint";
>
> # my $backup = $main::{myPrint};
> my $backup = *$str;
>
> *$str = sub() {
> print "Teste 2\n";
> };
>
> myPrint;
>
> *$str = $backup;
> }
>
> myPrint;
> test;
> myPrint;
> exit;
>
> Note: This is not beautyfull but I'm doing some test-cases and I don't
> want to change the code to be tested.


$ perl -e'
my $myPrint = sub { print "Teste 1\n" };

sub test { $myPrint = sub { print "Teste 2\n" } }

$myPrint->();
test;
$myPrint->();
exit;
'
Teste 1
Teste 2



John
--
use Perl;
program
fulfillment
Dave Gray

2005-08-03, 10:00 pm

On 8/3/05, John W. Krahn <krahnj@telus.net> wrote:
> marcos rebelo wrote:
>=20
> $ perl -e'
> my $myPrint =3D sub { print "Teste 1\n" };
>=20
> sub test { $myPrint =3D sub { print "Teste 2\n" } }
>=20
> $myPrint->();
> test;
> $myPrint->();
> exit;
> '
> Teste 1
> Teste 2


If you combine this solution with local variables, you can do exactly
what you need:

#!/usr/bin/perl
use strict;
use warnings;
our $print =3D sub { print "test 1\n" };
sub test { $print =3D sub { print "test 2\n" } }
$print->();
{
local $print =3D $print;
$print->();
test();
$print->();
}
$print->();
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com