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

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.

Replies are listed 'Best First'.
Re^8: Closures and undefined subs
by ikegami (Patriarch) on Sep 26, 2007 at 19:17 UTC

    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.

        I was not aware that I am changing semantic of "anywhere" sub to "define before use" variable with local!

        There's nothing magical about the = in local *func = sub { ... }; or my $func = sub { ... };. It's an assignment like any other. You can't fetch the value from a variable before you assign the value to the variable. Or as the case is here, before you even create the variable.