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

Hi all,

I'm a bit of a perl newbie so sorry if this is obvious but I'm just missing it. I found this .pm to open folders but don't know how to call it from my pl script.

# DirSelect: A Tk directory selection widget. # # This widget allows navigating MS Windows local and mapped network # drives and directories and selecting a directory. # # Perl/Tk includes several widgets to perform the function of selectin +g # a file or directory. However, widgets originally written for Unix # systems (such as FileSelect) don't allow browsing local and mapped # drives under Windows, and widgets adapted for Win32 systems that do # allow this (such as GetOpenFile) don't allow the user to select a # directory instead of a file. # # On non-MS systems, this is simply a dialog box with a Dirtree in it. + # # Usage: my $dir = $mainwindow->DirSelect->Show; # # Email comments, questions or bug reports to Kristi Thompson, # kristi@kristi.com package DirSelect; use vars qw($VERSION); $VERSION = '1.0'; @EXPORT_OK = qw(glob_to_re); use strict; use English; require Tk::Derived; use vars qw(@EXPORT_OK); use base qw(Tk::Toplevel); use Tk::widgets qw(Frame Button Radiobutton Label DirTree); use Cwd; Construct Tk::Widget 'DirSelect'; sub Populate { my($cw, $args) = @ARG; $cw->SUPER::Populate($args); my $top = $cw->Frame( -relief => 'groove', -bd => 2, )->pack( -fill => 'x', -padx => 2, -pady => 3, ); my $mid = $cw->Frame->pack( -fill => 'both', -expand => 1, ); my $bottom = $cw->Frame->pack( -fill => 'x', -ipady => 6, ); $bottom->Button( -width => 7, -text => 'OK', -command => sub {$cw->{dir} = $mid->packSlaves- >selectionGet( +)}, <br> )->pack( -side => 'left', -expand => 1, ); $bottom->Button ( -width => 7, -text => 'Cancel', -command => sub {$cw->{dir} = undef}, )->pack( -side => 'left', -expand => 1, ); if ($OSNAME !~ /mswin/i) { $top->packForget; _dirtree($mid, '/'); } else { require Win32API::File; my @drives = Win32API::File::getLogicalDrives(); my $startdrive = _drive(cwd); my $selcolor = $top->cget(-background); foreach my $d (@drives) { my $drive = _drive($d); my $b = $top->Radiobutton( -selectcolor => $selcolor, -indicatoron => 0, -text => $drive, -width => 3, -command => [\&_browse, $mid, $d], -value => $d, )->pack( -side => 'left', -padx => 4, -pady => 6, ); $b->invoke if $startdrive eq $drive; } } } sub Show { my($cw, $grab) = @ARG; my $old_focus = $cw->focusSave; my $old_grab = $cw->grabSave; $cw->Popup(); Tk::catch { if (defined($grab) and length($grab) and $grab =~ /global/i) +{ <br> $cw->grabGlobal; } else { $cw->grab; } }; $cw->focus; $cw->_wait; &$old_focus; &$old_grab; return($cw->{dir}); } sub _dirtree { my($f, $d) = @ARG; $f->Scrolled('DirTree', -scrollbars => 'osoe', -directory => $d, -bg => 'white', )->pack( -fill => 'both', -expand => 1, -pady => 4, ); } sub _drivelabel { my($f, $msg) = @ARG; $f->Label( -text => " $msg", -relief => 'sunken', -bd => 1, -anchor => 'w', )->pack( -padx => 2, -fill => 'x', -ipady => 2, ); } sub _drive { shift =~ /^(.*:)/; return($1); } sub _browse { my($f, $d) = @ARG; foreach ($f->packSlaves) {$_->packForget;} my $drive = _drive($d); if (chdir($d)) { my $volumelabel; Win32API::File::GetVolumeInformation($d, $volumelabel,[],[],[ +],[],[],[]); <br> _drivelabel($f, "$volumelabel ($drive) " . { 0 => 'Unknown', 1 => 'No root drive', 2 => 'Removable disk drive', 3 => 'Fixed disk drive', 4 => 'Network drive', 5 => 'CD-Rom drive', 6 => 'RAM Disk', }->{Win32API::File::GetDriveType($d)} ); _dirtree($f, $d); } else { _drivelabel($f, "$drive is not available."); } } sub _wait { my($cw) = @ARG; $cw->waitVariable(\$cw->{dir}); $cw->grabRelease; $cw->withdraw; $cw->Callback(-command => $cw->{dir}); } 1;
I tried my $dir = $mainwindow->DirSelect->Show; with no luck.

Thanks in advance for the help.

-Mark

update (broquaint): added <readmore> tag and fixed formatting

Replies are listed 'Best First'.
Re: how do I call this .pm from my script
by bobn (Chaplain) on Aug 23, 2003 at 23:32 UTC

    First, it make no sense to post the entire contents of a module that you didn't write. We can see DIrSelect.pm any time we want - posting it here just uses up space.

    Instead, you should post the code you wrote which is not working - but not the whoel program, just enough to demonstrate the issue. Post it between <code> </code> tags - otherwise it gets mangled.

    To use this module after installation, you would do:

    # DELETED. This module may be unueable in it's current form

    be sure to see http://perlmonks.com/index.pl?node=Tutorials#usingmods

    Second Update: I suggest you look for a different module, one with doc. This module may be useable, but I can't figure out how.

    For the reord, here is ALL the comments and doc for this module:

    # DirSelect: A Tk directory selection widget. # # This widget allows navigating MS Windows local and mapped network # drives and directories and selecting a directory. # # # On non-MS systems, this is simply a dialog box with a Dirtree in it. # # supercedes all versions of Win32Dirselect # Usage: my $dir = $mainwindow->DirSelect->Show; # Options: -dir=>"directory", -w=>"width" # # Email comments, questions or bug reports to Kristi Thompson, kristi@ +indexing.ca
    There are no other comments or pod ANYWHERE in the module or included in the CPAN distribution, so far as I can tell.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      bobn - I can't find DirSelect.pm on CPAN - so where could we find it? And I tried the code above, although I have no clue whatsoever about Perl/Tk, and came to the conclusion that there are probably even some issues in the module itself.
      The call to Win32API::File::GetVolumeInformation should probably be changed from
      Win32API::File::GetVolumeInformation($d,$volumelabel,,,,,,);
      to something like
      my $dummy; Win32API::File::GetVolumeInformation($d, $volumelabel, [], [], $dummy, + [], $dummy, $dummy);
      I still get an error about -directory => $d in sub _dirtree, though.
      Maybe I am completely wrong, but that is what I thought of it after reading the docs and playing a bit with it.
      Cheers, CombatSquirrel.

      Update: Found it at Tk::DirSelect; the first one of the above issues is not present at the module's installation on my system (ver 1.03).

        It's actually Tk::DirSelect and CPAN.pm found and installed it with no problems.

        I agree that there probablyly issues in the module - the example says use -w for width, but the Populate sub in the module refers to $args->{width}, I think. I deleted this thing from machine, (and I almost NEVER delete anything), so I no longer have it for reference and don't plan on using it for anything.

        POD is easy enough to use. So is #. If the author of the module couldn't use either of those, it's very disappointing.

        --Bob Niederman, http://bob-n.com

        All code given here is UNTESTED unless otherwise stated.