On Thursday 13 April 2006 09:19, [EMAIL PROTECTED] wrote:
> #!/usr/local/bin/perl
>
> write_function1("first");
> print auto_function(), "\n";
> # This will print:
> # auto_function first
>
> write_function1("second");
> # How would I undefine the autoloaded version of auto_function?
> print auto_function(), "\n";
> # This will print:
> # auto_function first
> # But I would like it to print
> # auto_function second
>
>
>
>
> sub AUTOLOAD {
>     my $attr = $AUTOLOAD;
>     $attr =~ s/.*:://;
>     if ( -e $attr ) {
>             require $attr;

What you are trying to do here will not work, have a look at perldoc -q 
require.

>     } else {
>         die "oops";
>     }
>     warn $@ if $@;
>     $attr->("z") if -e $attr;
> }
>
> sub write_function1 {
>     my $thing = shift;
>     unlink "auto_function";
>     open "FILE", ">", "auto_function";
>     print FILE <<END;
> sub auto_function{
>     " auto_function $thing";
> }
> 1;
> END
>     close FILE;
> }

How about trying this instead

#!/usr/bin/perl

use warnings;
use strict;

*auto_function = write_function1('first');
print auto_function(), "\n";
# This will print:
# auto_function first

{
        #PERL complains when you redefine subroutines.
        #Since you know what your doing disable it for now
        no warnings 'redefine';
        *auto_function = write_function1('second');
}

print auto_function(), "\n";
# This will print:
# auto_function second

sub write_function1
{
        my $thing = shift;
        return sub {"auto_function $thing";};
}


Hope this helps

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to