in reply to Re: Confused by variable scope in Tk::Wizard
in thread Confused by variable scope in Tk::Wizard

Thank you very much, the example does work. I do have to admit though that I don't understand why.. the syntax around this segment gives me a headache:
$wiz->addPage ( sub { $wiz->_text_frame( { -title => 'Partitioning', -boxedtext => "/mirror/inifiles/partition.$oschoice", } ); } );

Replies are listed 'Best First'.
Re^3: Confused by variable scope in Tk::Wizard
by Anonymous Monk on Apr 19, 2011 at 01:52 UTC
    Here is the exact same thing only written more verbosely
    { my $Aspirin = sub { return $wiz->_text_frame( { -title => 'Partitioning', -boxedtext => "/mirror/inifiles/partition.$oschoice", } ); }; $wiz->addPage($Aspirin); }
    I suspect you're having trouble with closures

    In my original program, this unnamed subroutine, this anonymous subroutine, whose reference is now stored in $Aspirin, refers to two variables outside its scope. Namely $oschoice and $wiz, which are not declared (my) within $Aspirin. This makes $Aspirin a closure.

    Each time you call this subroutine, $Aspirin->(); it calls _text_frame with the current value of $oschoice

    So if $oschoice="Lin" then $Aspirin->() returns a frame with /mirror/inifiles/partition.Lin

    Next you change $oschoice="Win" and then $Aspirin->() returns a frame with /mirror/inifiles/partition.Win

    The first step in the wizard edits $oschoice, then when you click next, the wizard invokes $Aspirin , which returns a new frame using the current value of $oschoice

    If you click the back button and select a different OS, and click next again, the wizard will call $Aspirin again

    More on closures, hopefully easier to understand :) oh and no wizards or witches :)