Path: utzoo!attcan!uunet!etnibsd!vsh From: vsh@etnibsd.UUCP (Steve Harris) Newsgroups: comp.lang.perl Subject: (repost, and possible fix): is ( -e _ ) always true?? Message-ID: <1180@etnibsd.UUCP> Date: 26 Oct 90 20:08:56 GMT Organization: Eaton Corp, Semiconductor Equipment Div., Beverly, MA Lines: 107 Nobody answered my first query, so I "fixed" it myself. (I'm still running patchlevel 18, if this has been fixed in a more recent release, say so and I'll shut up! But, looking through the patches, I don't see anything that looks like this problem has been addressed.) Bug: the -e operator (test for file existence) always returns 1 (true) when applied to the magic argument _ For example: =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- print "-e _ returns ", -e _ ? "true" : "false", "\n"; # returns true (error) $f = "."; # file does exist print "-e $f returns ", -e $f ? "true" : "false", "\n"; # returns true print "-e _ returns ", -e _ ? "true" : "false", "\n"; # returns true $f = "...."; # file doesn't exist print "-e $f returns ", -e $f ? "true" : "false", "\n"; # returns false print "-e _ returns ", -e _ ? "true" : "false", "\n"; # returns true (error) =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Here's what **SEEMS** to be going on: The return value of the -e operator is computed from the return value of the stat system call. This value is passed back by the mystat() function. However, when mystat() is called for the magic argument _, it simply returns 0. The fix is to have it return the previous return value. Here are some diffs that work on my system, but there's an awful lot I don't know about that's going on here (in particular, I don't understand why ( -e _ ) returns true BEFORE any other filetest/stat operations have been performed). Also, remember that these diffs are made against patchlevel 18 files. Anyway, I'd wait till Larry has given his blessing before installing them. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *** doio.c.BAK Fri Oct 26 14:13:44 1990 --- doio.c Fri Oct 26 15:02:26 1990 *************** *** 752,757 **** --- 752,758 ---- STR *str; { STIO *stio; + static int retcd = -1; if (arg[1].arg_type & A_DONT) { stio = stab_io(arg[1].arg_ptr.arg_stab); *************** *** 758,780 **** if (stio && stio->ifp) { statstab = arg[1].arg_ptr.arg_stab; str_set(statname,""); ! return fstat(fileno(stio->ifp), &statcache); } else { if (arg[1].arg_ptr.arg_stab == defstab) ! return 0; if (dowarn) warn("Stat on unopened file <%s>", stab_name(arg[1].arg_ptr.arg_stab)); statstab = Nullstab; str_set(statname,""); ! return -1; } } else { statstab = Nullstab; str_sset(statname,str); ! return stat(str_get(str),&statcache); } } --- 759,781 ---- if (stio && stio->ifp) { statstab = arg[1].arg_ptr.arg_stab; str_set(statname,""); ! return (retcd = fstat(fileno(stio->ifp), &statcache)); } else { if (arg[1].arg_ptr.arg_stab == defstab) ! return retcd; if (dowarn) warn("Stat on unopened file <%s>", stab_name(arg[1].arg_ptr.arg_stab)); statstab = Nullstab; str_set(statname,""); ! return (retcd = -1); } } else { statstab = Nullstab; str_sset(statname,str); ! return (retcd = stat(str_get(str),&statcache)); } } -- Steve Harris - Eaton Corp. - Beverly, MA - uunet!etnibsd!vsh