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

I have the following bit of code

open TEST,$path."\\".$item && do{-d TEST ? fillQueryBox($path."\\".$item,$qBox) : $cBox->insert('end +',$item); close TEST; last CLICK_OPTION;};
This code is for handling double clicks on a Tk-Listbox. Which should attempt to open a file/dir, and then when it is open check to see if it is a directory. If the selected listbox item is a directory it should call fillQueryBox(), if its not it should simply insert the item selected into another Tk-Listbox

the error I am getting is -d on closed filehandle TEST at C:/Perl/lib/list_maker.pl line l47 It was my understanding that a file open returned true on success... so it would pass the && and fall into the do{} block, implying the file or directory was open for me to call a -d <HANDLE> on.

WHAT AM I MISSING?...pardon the frustration.


Grygonos

Replies are listed 'Best First'.
Re: File closing on its own?
by Abigail-II (Bishop) on Nov 24, 2003 at 22:21 UTC
    && has a higher priority than a comma in list context. You need to put parens around the arguments of open, or replace && with and.

    Abigail

Re: File closing on its own?
by idsfa (Vicar) on Nov 24, 2003 at 22:24 UTC
    perl -e 'open TEST,"."; do {( -d TEST )?print "1$/" : print "0$/";}' 1
    perl -e 'open TEST,"." && do {( -d TEST )?print "1$/":print "0$/";}' 0

    It's an operator precedence issue. Try:

    perl -e 'open TEST,"." and do {( -d TEST )?print "1$/": print "0$/";}' 1

    My parents just came back from a planet where the dominant life form had no
    bilateral symmetry, and all I got was this stupid F-Shirt.
      Thanks you all. That took care of it. I never would have came up w/ that on my own. Haven't ever ran into precedence issues in Perl... which probably means I just haven't done enough perl or haven't tried to code enough good Perl :) Thanks again!

      Grygonos