Re: Backup of a subroutine

2005-08-03 Thread Dave Gray
On 8/3/05, John W. Krahn <[EMAIL PROTECTED]> wrote:
> marcos rebelo wrote:
> > I need to redefine localy one subroutine and have it correctlly after.
> > How do I do it?
> 
> $ 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

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

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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Backup of a subroutine

2005-08-03 Thread John W. Krahn
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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Backup of a subroutine

2005-08-03 Thread marcos rebelo
sub myPrint () {
print "Teste 1\n";
}

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

my $subName = "::myPrint";

my $backup = \&myPrint;

*$subName = sub() {
print "Teste 2\n";
};

myPrint;

*$subName = $backup;
}

myPrint;
test;

myPrint;

On 8/3/05, marcos rebelo <[EMAIL PROTECTED]> 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.
>

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Backup of a subroutine

2005-08-03 Thread marcos rebelo
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.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]