in reply to Re^5: Closures and undefined subs
in thread Closures and undefined subs

I didn't ask for the original code. Quite the opposite. I asked for the code giving the error. What you posted doesn't demonstrate the problem, so its useless. We can't debug code we don't see.

Replies are listed 'Best First'.
Re^7: Closures and undefined subs
by Anonymous Monk on Sep 26, 2007 at 17:54 UTC
    OK. Then how about this?
    package Levels::Level_IV; use strict; use warnings; use Math::RungeKutta; ... sub level_iv { my ( $args_ref, $R, $factor, $level_iii_ref ) = @_; ... for my $i ( $t..$t_final ) { ... ( $t, $dt, @y_med ) = rk4_auto( \@y_med, \&_dydt, $t, $dt, $eps + ); } ... sub _equation_air { my ( $args_ref, $level_iii_ref, $y ) = @_; #I tried these options # # my $_equation_air = sub { # my $y = shift; # local *_equation_air = sub { # my $y = shift; #for easy calculations make copies of the parameters (not alia +ses) my arguments for formula from $args_ref and $level_iii_ref #differential equation for mass my $dydt = some formula; return $dydt; }; ... #differential equation for fugacity at start and end times sub _dydt { my ( $t, @y ) = @_; my @dydt; #start differentiation at point y0 (air) $dydt[0] = _equation_air( $args_ref, $level_iii_ref, $y[0] ); #tried these #$dydt[0] = _equation_air($y[0]); #local #$dydt[0] = $_equation_air->($y[0]);#lexical ... return @dydt; } ... return some hash; }
    Failures are described in another post earlier if I try lexical or local.

      That's the original code still!! We have already told you how to fix it, and it does not give the errors you want us to look at.

      However, I think I guessed what you are doing wrong: setting local *func or my $func after the point where it's used.

      # BAD sub outer { my $var; ($t, @y) = rkXXX(\@y, $dydt, $t, $dt); my $dydt = sub { ... }; } # Good sub outer { my $var; my $dydt = sub { ... }; ($t, @y) = rkXXX(\@y, $dydt, $t, $dt); }
        I dont have all the code here but I am almost sure you are absolutely right...
        Thats why it tolds me in any case undefined or empty reference...
        I was not aware that I am changing semantic of "anywhere" sub to "define before use" variable with local!

        Will try it out but I guess you solved my problem.