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

Hi,

I am using strawberry perl 5 version 32 in my Windows 10 Pro. in my perlscript i called "Win32::FileOp" for calling "BrowseForFolder", when i click the button for browsing folder, script will close immediately. is there any way to fix this issue.

Regards,

srikrishnan

Replies are listed 'Best First'.
Re: win32-FileOp module for strawberry
by marto (Cardinal) on Nov 04, 2022 at 12:55 UTC

      Hi Marto

      Thanks for your response. Below i have pasted my sample code.

      use Tk; use Win32::FileOp; my $mw = MainWindow -> new; my $frmWidth = 452; my $frmHeigt = 50; my $screenypos = $mw->screenheight(); my $screenxpos = $mw->screenwidth(); $screenxpos = $screenxpos - $frmWidth; + + $screenxpos = $screenxpos/2; $screenypos = $screenypos - $frmHeigt; $screenypos = $screenypos/2; $mw->geometry("${frmWidth}x$frmHeigt+$screenxpos+$screenypos"); $mw->resizable('no', 'no'); $mw->title ("Test"); my $mainFrame = $mw -> Frame -> pack (-side => 'top', -padx => '8', -p +ady => '10'); $BrowseFrame = $mainFrame -> Frame() -> pack (-side => 'top', -pady => + '5'); $BrowseFrame -> Button (-text => 'Select folder', -command => \&gotoFi +le, -background => '#ccccff', -activebackground => '#6666ff') -> pack + (-side => 'left', -padx => '5'); $BrowseFrame -> Entry (-textvariable => \$filepath, -width => '84', -b +ackground => '#ccccff') -> pack (-side => 'left', -padx => '2'); $mw->update; MainLoop; sub gotoFile { $filepath = BrowseForFolder ("Choose Directory", CSIDL_DRIVES, BIF +_RETURNONLYFSDIRS); $mw->Unbusy; return 'Cancel' unless($filepath); return; }

      Thanks

      Srikrishnan

        Why wouldn't you use Tk::chooseDirectory if it's Tk application anyway? Seems to work as advertised.

        I don't have that Tk module installed on my system, but as guess without testing, I would change:
        $filepath = BrowseForFolder ("Choose Directory", CSIDL_DRIVES, BIF_RET +URNONLYFSDIRS); to $filepath = $mw->BrowseForFolder ("Choose Directory", CSIDL_DRIVES, BI +F_RETURNONLYFSDIRS);
Re: win32-FileOp module for strawberry
by kcott (Archbishop) on Nov 05, 2022 at 02:36 UTC

    G'day srikrishnan,

    Win32::FileOp was last updated about a decade ago. I suspect it is abandonware.

    See "Bug #120791 for Win32-FileOp: Crash in BrowseForFolder()". This was raised six years ago; its Status is still "new".

    Here's the guts of what I think you want:

    #!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(); $mw->Button( -text => 'Select directory', -command => sub { my $dir = $mw->chooseDirectory(); # Do something with $dir # You probably want more than this demo test: if (defined $dir) { warn "Directory selected: '$dir'\n"; } else { warn "No directory selected.\n"; } }, )->pack(); MainLoop;

    I strongly advise that you

    • always use the strict and warnings pragmata; and,
    • use lexical, instead of package, variables.

    — Ken

      Hi kcott

      Thanks for your response and alternate suggestion

      in my original script i have used strict and warnings. I used to do that always.

      Regards,

      srikrishnan

        "Thanks for your response and alternate suggestion"

        You're welcome.

        "in my original script i have used strict and warnings. I used to do that always."

        If you do that always, that's great; however, if you don't show it in your code, we don't know.

        An SSCCE is supposed to be code that we can run and reproduces your problem. Had you included those pragmata, your SSCCE would have output something along the lines of:

        Global symbol "$BrowseFrame" requires explicit package name ... Global symbol "$filepath" requires explicit package name ... Execution ... aborted due to compilation errors.

        So, without them, your SSCCE is not representative of your real code; and with them, your SSCCE cannot be run.

        Also, for future reference, the first "S" in SSCCE stands for "Short". Please exclude code that is not relevant to the problem. In this case, all of the cosmetic parts (colours, padding, and so on) could have been omitted; this allows you, and us, to focus on the parts that matter.

        And just so you know, I'm not trying to beat you over the head with a rule book. Helping us to help you will generally result in faster and better responses.

        — Ken