[Israel.pm] perl/tk warnings

Offer Kaye oferk at oren.co.il
Tue Apr 20 02:36:22 PDT 2004


> sub add(){
>     my $file = shift;
>     unless (Exists($files_nb)){
>     $files_w  = $mw->Toplevel();
>       $files_w->geometry("150x150+40+60");
>     $files_nb = $files_w->Scrolled('NoteBook')
>     ->pack(
>     -fill=>'both',
>     -expand=>'1');
>
>     $files_nb->add($file,-label=>$file)
>     ->pack();
>     return;
>     }
> }
>

Note sure, but perhaps the problem is near the end of the add sub - try
moving the closing curly brace of the "unless" before the last $files_nb
part, e.g.:

     $files_nb->add($file,-label=>$file)
     ->pack();
     return;
     }

Should become:

     }
     $files_nb->add($file,-label=>$file)
     ->pack();
     return;

Now for some general comments:
1. By your own words, you are an inexperienced Perl programmer. I *highly*
recommened using the "-w" switch and "use strict;" pragma. Your code will be
a lot cleaner thanks to them and you'll catch many errors.
2. "local $mw = MainWindow->new;" -- you probably want to use "my" here, not
"local". See "perldoc -f local" and "perldoc -f my".
3. You use a lot of empty return statements: "return;". You do realize that
returns an empty list/undef value, depending on context? If you're not
planning to EVER use the return value, just drop the statement- the last
expression evaluated will be returned instead. I doubt you actually expect
an undef as a return value...
4. Don't use "unless". Period. Okay, other people may disagree, it's a
matter of taste, and maybe with the newer usage form it is a little better,
for example:
	print "okay!\n" unless not_okay();
But to me it has always seemed add complication where non is needed. In your
case, a simple "if (!Exists($files_nb))" would serve much better. At least
it is clearer to me... :-)
5. Use indentation. In the add sub, everything inside the "unless" curlies
should be indented.

Hope this helps :-)
--
Offer Kaye



More information about the Perl mailing list