[tcltk-perl] scalar($w->getOpenFile)
Brought to you by:
hobbs
From: Gisle A. <gi...@Ac...> - 2004-04-21 13:08:33
|
Gisle Aas <gi...@Ac...> writes: > To me it wrong if: > > $tcl->Eval("set var {a b c}"); > > returns something different to perl than > > $tcl->Eval("set var [list a b c]"); > > if these two expressions have exactly the same semantics in Tcl. > > I would suggest that we always return the string in scalar context and > always list in list context. If the value is not a valid list, then > we croak in list context. If you try the following program: #!/usr/bin/perl -w use Tcl::Tk qw(:perlTk); my $mw = MainWindow->new; my $label = $mw->Label(-text => "Hello")->pack; if (my $file = $mw->getOpenFile) { $label->configure(-text => "File [$file]"); $mw->after(5000, sub { $mw->destroy }); } MainLoop; and then hit "Cancel" to the dialog that pops up, then we fill in the field with something like "File [ARRAY(0x8141ad8)]". Apparently tk_getOpenFile returns a list instead of string, but this is not documented. Since there is no semantic difference between a list and a string this does not really matter to Tcl, but for perl with the current Tcl.pm behaviour it does. I think we should make $tcl->Eval and $tcl->call behave as I describe in the quoted text above. That would fix this problem and in general make things sane. Is there a way to make an automatic test case out of this? I guess I'm asking if there is a way to make the program above hit the "Cancel" button by itself. In our private branch I have used this workaround for now: --- Tcl-Tk/lib/Tcl/Tk.pm.~1~ Wed Apr 21 05:43:50 2004 +++ Tcl-Tk/lib/Tcl/Tk.pm Wed Apr 21 05:43:50 2004 @@ -839,19 +839,25 @@ my $self = shift; my %args = @_; $args{'-parent'} = $self->path unless defined $args{'-parent'}; - $wint{$$self}->call('tk_getOpenFile', %args); + my @files = $wint{$$self}->call('tk_getOpenFile', %args); + return $files[0] unless wantarray; + return @files; } sub getSaveFile { my $self = shift; my %args = @_; $args{'-parent'} = $self->path unless defined $args{'-parent'}; - $wint{$$self}->call('tk_getSaveFile', %args); + my @files = $wint{$$self}->call('tk_getSaveFile', %args); + return $files[0] unless wantarray; + return @files; } sub chooseDirectory { my $self = shift; my %args = @_; $args{'-parent'} = $self->path unless defined $args{'-parent'}; - $wint{$$self}->call('tk_chooseDirectory', %args); + my @files = $wint{$$self}->call('tk_chooseDirectory', %args); + return $files[0] unless wantarray; + return @files; } sub messageBox { my $self = shift; End of Patch. > Alternatively we return return some object that just wraps the Tcl_Obj > and can used as both a string and array reference. This can be > combined with the previous alternative, so this only happens in scalar > context. > > Alternatively we have a separate methods that indicate if a string, > array reference or list is wanted as return value. > > $tcl->Eval_giving_string($code); > $tcl->Eval_giving_arrayref($code); > $tcl->Eval_giving_list($code); |