rgcosma has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks,

I'm trying to understand how to make a change in any of the steps through such an installer, e.g.:

use Tk; use Tk::Wizard; my $wiz = new Tk::Wizard (-title => "Kickstart Wizard"); my $oschoice='test'; $wiz->addSingleChoicePage( -text => 'Choose OS', -variable => \$oschoice, -choices => [ { -title => 'Linux', -value => 'Lin' }, { -title => 'Windows', -value => 'Win', -selected => 1, }, ], -preNextButtonAction => sub { warn $oschoice; }, ); $wiz->addTextFramePage( -title => 'Partitioning', -boxedtext => "/mirror/inifiles/partition.$oschoice" ); $wiz->Show; MainLoop;

Problem is, variable $oschoice is changed only in the context of addSingleChoicePage, I can't see any way to communicate to addTextFramePage its new value.

The module has some properties like  $wiz->{_pages} or $wiz->{_pages_e} but nothing like $wiz->{pages}->{0}->{boxedtext}

Replies are listed 'Best First'.
Re: Confused by variable scope in Tk::Wizard
by Anonymous Monk on Apr 18, 2011 at 13:05 UTC
    See sub PM899927
    #!/usr/bin/perl -- use strict; use warnings; use Tk; use Tk::Wizard; Main(@ARGV); exit(0); sub Main { @_ ? T160() : PM899927(); } #~ http://perlmonks.org/?node_id=899927 #~ Confused by variable scope in Tk::Wizard sub PM899927 { my $wiz = new Tk::Wizard( -title => "Kickstart Wizard" ); my $oschoice = 'test'; $wiz->addSingleChoicePage( -text => 'Choose OS', -variable => \$oschoice, -choices => [ { -title => 'Linux', -value => 'Lin' }, { -title => 'Windows', -value => 'Win', -selected => 1, }, ], -preNextButtonAction => sub { warn "\$oschoice $oschoice "; } +, ); $wiz->addPage( sub { #~ shift->_text_frame( $wiz->_text_frame( { -title => 'Partitioning', -boxedtext => \"/mirror/inifiles/partition.$oschoi +ce", } ); }, ); $wiz->Show; $wiz->focus; MainLoop; } ## end sub PM899927
      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", } ); } );
        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 :)