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

Hello, I am trying to pass the parameters a user enters in the text to a subroutine. The values in my print message are correct, but the program does not work like it should. Here is my code:
# Create the GUI # Create the Main Window and set the size to 500 x 200 my $mw = MainWindow->new; $mw->title("Cheat Checker"); my $sizeOfWindow = $mw->Canvas(-width => 500, -height => 80)->pack; # Create the Menus $mw->configure(-menu => my $menubar = $mw->Menu); my $fileMenu = $menubar->cascade(-label => '~File'); my $helpMenu = $menubar->cascade(-label => '~Help'); my $exitMenuOption = $fileMenu->command(-label => 'Exit', -underline => 0, -command => \&exit,); my $aboutMenuOption = $helpMenu->command(-label => 'About', -underline => 0, -command => sub {my $d1 = $mw +->Dialog(-text => "Cheat Checker\n" . + "Version: 0.0.0.1\n" . + "Andy Daykin\n" . + "2-11-08",); $d1->Show;}); # Options for program settings my $labelDirPath = $mw->Label(-text => "Directory of files to grade")- +>pack(); my $textDirPath = $mw->Text(-width => 25, -height => 1)->pack; + my $labelFileExt = $mw->Label(-text => "File Extension to pick up")->p +ack; my $textFileExt = $mw->Text(-width => 25, -height => 1)->pack; my $sumbitOptions = $mw->Button(-text => 'Submit', -command => sub{&runLoop($textDirPath- +>Contents, $textFileExt->Contents)})->pack; # Program output window my $output = $mw->Text(qw/-width 60 -height 10/)->pack; tie *STDOUT, ref $output, $output; # Initialize the GUI MainLoop; sub runLoop { print "Starting $FindBin::Script ".localtime()."...\n"; # Set parameters passed in to dirPath and fileExt my $dirPath = $_[0]; my $fileExt = $_[1]; print "dirPath: ", $dirPath, "\n"; print "fileExt: ", $fileExt, "\n"; # Set defaults if needed: # This is where it dies and spits out the error +message unless(-d $dirPath){ die "The directory path '$dirPath' is not a v +alid directory, $!"; } unless($fileExt){ die "You need to supply a file extension!"; } blah blah blah I do some stuff :-) }
If I enter in C:\Test in the field $textDirPath, I will get a message saying that C:\Test is not a valid directory, even though it is. I had the program working from the command prompt, but when I tried to convert it to a GUI program I started getting errors

Replies are listed 'Best First'.
Re: Attempting to pass parameters to a sub via TK Text input
by chromatic (Archbishop) on Feb 18, 2008 at 07:31 UTC

    What is the exact output from your program? What if you provide a different directory name? What if you try C:/Test?

Re: Attempting to pass parameters to a sub via TK Text input
by thundergnat (Deacon) on Feb 18, 2008 at 16:58 UTC

    I'm going to assume that you didn't have warnings enabled since if you did, you might have noticed the "Unsuccessful stat on filename containing newline" warning.

    Tk Text widget contents always contains at least one newline, even if you didn't explicitly add one. It's one of the little bizarrnesses(word?) you almost never need to deal with except when you do.

    You could easily chomp() the contents to get rid of the newline assuming you haven't changed $/, but you would likely be better off using Entry widgets instead. They are specifically designed to be used for single line inputs.

    You also need to escape the $ in your print statement, but that is minor.

    Here is your script, modified to use Entrys (and perltidyed.)

    use warnings; use strict; use Tk; # Create the GUI # Create the Main Window and set the size to 500 x 200 my $mw = MainWindow->new; $mw->title("Cheat Checker"); my $sizeOfWindow = $mw->Canvas( -width => 500, -height => 80 )->pack; # Create the Menus $mw->configure( -menu => my $menubar = $mw->Menu ); my $fileMenu = $menubar->cascade( -label => '~File' ); my $helpMenu = $menubar->cascade( -label => '~Help' ); my $exitMenuOption = $fileMenu->command( -label => 'Exit', -underline => 0, -command => \&exit, ); my $aboutMenuOption = $helpMenu->command( -label => 'About', -underline => 0, -command => sub { my $d1 = $mw->Dialog( -text => "Cheat Checker\n" . "Version: 0.0.0.1\n" . "Andy Daykin\n" . "2-11-08", ); $d1->Show; } ); # Options for program settings my $labelDirPath = $mw->Label( -text => "Directory of files to grade" +)->pack(); my $textDirPath = $mw->Entry( -width => 25, )->pack; my $labelFileExt = $mw->Label( -text => "File Extension to pick up" ) +->pack; my $textFileExt = $mw->Entry( -width => 25 )->pack; my $sumbitOptions = $mw->Button( -text => 'Submit', -command => sub { &runLoop( $textDirPath->get, $textFileExt->get ) + } )->pack; # Program output window my $output = $mw->Text( qw/-width 60 -height 10/ )->pack; tie *STDOUT, ref $output, $output; # Initialize the GUI MainLoop; sub runLoop { print "Starting \$FindBin::Script " . localtime() . "...\n"; # Set parameters passed in to dirPath and fileExt my $dirPath = $_[0]; my $fileExt = $_[1]; print "dirPath: ", $dirPath, "\n"; print "fileExt: ", $fileExt, "\n"; # Set defaults if needed: # This is where it dies and spits out the error mess +age unless ( -d $dirPath ) { die "The directory path '$dirPath' is not a valid directory, $ +!"; } unless ($fileExt) { die "You need to supply a file extension!"; } #blah blah blah I do some stuff :-) }

      I did have strict and warnings on, but I didn't really think much of the error message you mentioned since this is my first Perl Tk program. Thanks a bunch for your help, my program is working fine now!

      In case people were curious this program checks for students that have similar papers, I grade for a class at the university I go to. Needless to say there has been a big copying problem in the class. It uses the Knuth Morris Pratt algorithm to determine just how similar they are.