http://www.perlmonks.org?node_id=906565

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

well below is my entire test code. The problem i'm encountering is that it won't return the directory name on win7 ult under activeperl. This is perl 5, version 12, subversion 3 (v5.12.3) built for MSWin32-x64-multi-thread (with 9 registered patches, see perl -V for more detail). Not sure what else I need to include to be helpful. This same code works fine on debian linux under Perl 5.10.1 with 51 patches. i'm surprised that a slightly older version of perl does it better. it could just be the differences between windows and linux
#!/usr/bin/env perl use Tk; use Env; use Tk::DirSelect; use diagnostics; #use warnings; $mw = new MainWindow; #Initialize main window my $entry_output_dir = $mw -> Entry(-textvariable=>\$last_directory); +# Directory my $label_output_dir = $mw -> Label(-text=>"Output Directory:"); my $button_choose_output_directory = $mw -> Button(-text => "Choose", +-command =>\&choose_output_directory); # open a directory picker $entry_output_dir -> grid(-row=>1,-column=>2); $label_output_dir -> grid(-row=>1,-column=>1); $button_choose_output_directory -> grid(-row=>1,-column=>3); MainLoop; sub choose_output_directory { use Data::Dumper; $new_directory = ' '; print Dumper( $last_directory, $new_directory); my $ds = $mw->DirSelect(-title => 'Select Directory', -width => 40); my $new_directory = $ds->Show($last_directory); if ($new_directory ne '') {$last_directory = $new_directory} }
This is the entire error i get
Tk::Error: Can't call method "selectionGet" on unblessed reference at +C:/Perl64/ site/lib/Tk/DirSelect.pm line 53. DirSelect::__ANON__ at C:/Perl64/site/lib/Tk/DirSelect.pm line 53 Tk callback for .dirselect.frame2.button Tk::__ANON__ at C:/Perl64/site/lib/Tk.pm line 250 Tk::Button::butUp at C:/Perl64/site/lib/Tk/Button.pm line 175 <ButtonRelease-1> (command bound to event)
  • Comment on Tk::Error: Can't call method "selectionGet" on unblessed reference at C:/Perl64/ site/lib/Tk/DirSelect.pm line 53. using TK::DirSelect
  • Select or Download Code

Replies are listed 'Best First'.
Re: Tk::Error: Can't call method "selectionGet" on unblessed reference at C:/Perl64/ site/lib/Tk/DirSelect.pm line 53. using TK::DirSelect
by syphilis (Archbishop) on May 25, 2011 at 02:37 UTC
    it could just be the differences between windows and linux

    Yes, it could - it could also be a difference between perl-5.10 and perl-5.12.
    DirSelect.pm contains some Win32-specific code, so there has at least been some attempt to deal with something that's specific to windows.
    I'd try inserting some print statements in DirSelect.pm in order to try and find out what's going on. For instance, if $w->{tree} is not a blessed reference, it would be a good idea to find out just what it is, and then trace it back to try and determine why it's not a blessed refence.

    Tk::DirSelect is a pure perl module, so I'd be hopeful that the problem could be fixed by hacking DirSelect.pm appropriately.

    Did Tk::DirSelect pass its test suite when you installed it ?

    Cheers,
    Rob
      I've opted to go for lamprecht's idea that i wish i'd realized existed. But I will continue to work on this problem to get a solution back to the monks :)
Re: Tk::Error: Can't call method "selectionGet" on unblessed reference at C:/Perl64/ site/lib/Tk/DirSelect.pm line 53. using TK::DirSelect
by lamprecht (Friar) on May 25, 2011 at 12:16 UTC

    Why don't you use chooseDirectory()?

    $path = $mw->chooseDirectory()
    Cheers, CHristoph
      ummm, I didn't know it existed? lol. i gave it a shot and it works on both linux and windows :) thanks for the idea
Re: Tk::Error: Can't call method "selectionGet" on unblessed reference at C:/Perl64/ site/lib/Tk/DirSelect.pm line 53. using TK::DirSelect
by Stoney2005 (Acolyte) on May 25, 2011 at 19:11 UTC
    chooseDirectory() works grand. except that the directory it returns has garbage characters. The solution to it is that the initial directory needs to be compliant with the OS. it worked fine on linux with the initial dir as /home/stoney but returned the garbage in windows until i used the env home and ran this $home =~ s/\\/\//g; on it. now the dir returns normal
Re: Tk::Error: Can't call method "selectionGet" on unblessed reference at C:/Perl64/ site/lib/Tk/DirSelect.pm line 53. using TK::DirSelect
by sundialsvc4 (Abbot) on May 26, 2011 at 01:50 UTC

    I don’t know if this applies to this case at all, or not, but that particular error-message just popped up for me today and I thought I’d briefly mention what it was.

    Consider:

    'use strict; use warnings; my $foo=[1,2]; foreach my $bar ($foo) { print "bar is $bar\n"}' bar is ARRAY(0x100804ed0)

    versus ...

    'use strict; use warnings; my $foo=[1,2]; foreach my $bar (@{$foo}) { print "bar is $bar\n"}' bar is 1 bar is 2

    The only difference between the two code examples is that, in the second case, @{} has been used i the foreach statement, to tell Perl that it is an array-reference.   When this is done, Perl produces the behavior you probably intended:   it returns each of the elements in the referenced array.   Whereas, in the first example, it returns one thing ... the arrayref itself; the present value of $foo.

    If (as was indeed the case in my actual code) the array in question was “a list of objects,” and you wanted to invoke a method against each of those objects, you could get this error.   An arrayref, after all, is not ... (heh) ... “any blessed thing.”