You probably want to use Tk::Wizard, see http://search.cpan.org/dist/Tk-Wizard/MANIFEST, Confused by variable scope in Tk::Wizard (hello closures) , Re: Confused by variable scope in Tk::Wizard (hello closures)

But if you want to stick with MainLoop you could write that like this (all lexically scoped and not-memory leaking )

#!/usr/bin/perl -- # Test to enter select directory with gui # # # # # perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr - +ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Encode; use Tk; use Path::Class; use Devel::CheckOS qw/ os_is /; Main( @ARGV ); exit( 0 ); sub Main { &wadebug; my $initialdir = shift || 'Z:\Projects\RunMSMS\MGF\human\ion_trap' +; my $end_file = shift || 'Z:\Projects\RunMSMS\MGF\human\ion_trap\ +test_gui_output.txt'; my $tissue = shift || "plasma"; tkgui( $initialdir, $end_file, $tissue ); } sub tkgui { &wadebug; my( $initialdir, $end_file, $tissue ) = @_; my $mw = Tk::MainWindow->new; my $tissue_but = $mw->Button( -text => 'Tissue Type', -command => [ \&select_tissue, $mw, $initialdir, \$tissue, ], ); $tissue_but->grid( -row => 3, -column => 1 ); $mw->Button( -text => 'Data Entry Complete', -command => [ \&do_end, $mw, $end_file, \$tissue, ], )->grid( -row => 10, -column => 1 ); Tk::MainLoop(); } sub select_tissue { &wadebug; my( $mw, $initialdir, $choiceref ) = @_; my $dir = $mw->chooseDirectory( -initialdir => $initialdir ); if( os_is(qw( MicrosoftWindows )) ){ $dir = encode( "windows-1252", $dir ); } if( defined $dir and length $dir ){ $dir = dir( $dir )->basename; if( length $dir ){ $$choiceref = $dir; } } return; } sub do_end { use autodie qw/ open close /; ## make open(...) or die(...) automa +tic &wadebug; my( $mw, $outfile, $tissueref ) = @_; print join " ", tissue => $$tissueref , "\n"; # open my($FOUT), '>>', $outfile; # print $FOUT "$$tissue\n"; # close $FOUT; $mw->destroy; ## exit mainloop } sub wadebug { my( $package, $filename, $line, $subroutine, $hasargs ) = caller( +1 ); for my $var ( @_ ){ if( not ref $var ){ use Data::Dump ();; print STDERR "# $line:$subroutine: @{[ Data::Dump::pp( $va +r ) ]}\n"; } elsif( ref($var) and ref($var) =~ m{ SCALAR | ARRAY | HASH } +x ) { print STDERR "# $line:$subroutine: @{[ Data::Dump::pp( $va +r ) ]}\n"; } else { print STDERR "# $line:$subroutine: $var\n"; } } print STDERR "\n"; } __END__ # 28:main::tkgui: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_trap" # 28:main::tkgui: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_trap\\test_g +ui_output.txt" # 28:main::tkgui: "plasma" # 251:main::select_tissue: Tk::MainWindow=HASH(0x9a1804) # 251:main::select_tissue: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_tra +p" # 251:main::select_tissue: \"plasma" # 251:main::do_end: Tk::MainWindow=HASH(0x9a1804) # 251:main::do_end: "Z:\\Projects\\RunMSMS\\MGF\\human\\ion_trap\\test +_gui_output.txt" # 251:main::do_end: \"x64" tissue x64

In reply to Re: Perk Tk - how to send a value from a subroutine back to the main program using Button (without MainLoop) by Anonymous Monk
in thread Perk Tk - how to send a value from a subroutine back to the main program using Button by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.