
The standard methods in Perl/Tk for displaying file dialogs are getOpenFile (for opening existing files) and getSaveFile (for selecting a name for a non-existing file). These methods will use - if possible - a native interface. That is, on Windows the standard file dialog and on Macintosh (if there will be ever a port of Perl/Tk for the Mac) too. In Unix/X11, there is no standard file dialog (note that Tk is built on top of Xlib, so KDE/Qt, CDE/Motif and GNOME/Gtk widgets are not available). In this case, the dialog is a mega widget written in Perl (which, as shown below, can also be used for other platforms).
getOpenFile/getSaveFile is easy to use: just say
$top->getOpenFile;or
$top->getSaveFile;The execution of the program will stop at this point and a file dialog will be displayed. After selecting Open/Save or Cancel, the method call will return the selected file name or an empty string.
There are a couple of extra options which are described in the Tk::getOpenFile manual page. An example script using more options can be found here. The -filetypes option can be used to specify a number of filters (e.g. show only perl files with the file extension .pl). These filters are displayed in an option menu widget. It is good practice to always include an entry "All files", which matches every file in the current directory. The first entry in the -filetypes argument is the default entry. If -filetypes is omitted, the file dialog will show every file.
The Windows user is usually familiar with the use of the standard file dialog. Using the X11 file dialog is quite similar. There is an additional feature which is especially useful in mouse-less usage. By switching the focus to the icon list (this is done by hitting Shift-Tab once), it is possible to type the wanted file name. While typing, the selection moves to match an appropriate file name. Hitting return will accept the active selection as the return value of the dialog.

Back in the old Tk 400.xxx days, there was only the Tk::FileSelect widget available. It's still in the newer Perl/Tk distributions, and it has some limitations, but also its advantages.
First, here is an example for the usage of the FileSelect dialog. This example shows one of the advantages of this widget: you can change the names of the labels, which make it more suitable for internationalisation (in the example the labels were changed to carry german names). The set of label options is not complete (yet), but will hopefully in the future.
You can also specify a -verify option to check if the selected file meets your specifications. The argument is a list of the standard perl file tests (like ['-r', '-T'], to restrict to readable text files). This option can also be used to restrict FileSelect to "DirSelect", by changing the verify option to ['-d'] (here is an example). I will discuss later another way for selecting directories.
The options of Tk::FileSelect are poorly documented, so here is a short overview of possible options:
Note that Tk::FileSelect is partially broken in the newer Tk's (that is the beta ones: from 800.016 to 800.018), but this will change in the next releases.
If you want to use Tk::FileSelect as the default file dialog, then you can say
use Tk::FileSelect 'as_default';This will cause to change the getOpenFile/getSaveFile methods to use the FileSelect widget from now on.

A lesser known widget in the Perl/Tk distribution is Tk::Dirlist. It's a widget displaying a tree of directories and files, beginning from the root to a specified directory (or the current directory). This widget is not a dialog, so you have to do the packing (or gridding) yourself. It also has no bindings to user interaction, but since the widget is derived from Tk::HList, the -command and -browsecmd options can be used. Here is a short but complete example for the use of Tk::Dirlist.

One way for selecting directories was already discussed (the -d verify option of Tk::FileSelect). In the Perl/Tk distribution, there is another widget specifically dealing with directories: Tk::DirTree. This widget is not a dialog, so you have to create a toplevel window and embed this widget in it, and also use waitVariable to block execution until the user made a choice (Note: waitVariable waits until the referenced variable changes its value. While waiting, the event mechanism of Tk is not blocked, so you can click on other buttons etc.). In the example script this usage is demonstrated. The widget is configured to change the directory with a single click and to select a directory with a double click (or by clicking the Ok button which will use the last selected directory). If you don't like this behaviour, you can easily change the code so that double-clicks open the selected directory, and the Ok button is the only way to confirm a selection. This is also described in the sample script.