Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

-e fails when file is proven to exist

by zemplen (Novice)
on Mar 08, 2012 at 20:10 UTC ( #958529=perlquestion: print w/ replies, xml ) Need Help??
zemplen has asked for the wisdom of the Perl Monks concerning the following question:

my $newDir = "\"${menu}$CGIFORM::group\\\""; print $newDir,"\n"; print "Doesn't exist ",(! -d {newDir}),"\n"; print " Does exist ",( -d {newDir}),"\n"; exit;
Produces:
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PhoneTools\" Doesn't exist 1 Does exist
However:
$ls -al "C:\ProgramData\Microsoft\Windows\StartMenu\Programs\PhoneTool +s\" total 36 drwxr-xr-x 1 Joe None 0 Mar 8 12:28 . drwxr-xr-x 1 Joe None 0 Mar 8 12:27 .. -rwxr-xr-x 1 Joe None 850 Mar 8 12:28 Ac32.exe.lnk $dir "C:\ProgramData\Microsoft\Windows\StartMenu\Programs\PhoneTools\" Volume in drive C is OS Volume Serial Number is FA69-4ADC Directory of C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Pho +neTools 03/08/2012 12:28 PM <DIR> . 03/08/2012 12:28 PM <DIR> .. 03/08/2012 12:28 PM 850 Ac32.exe.lnk 1 File(s) 850 bytes 2 Dir(s) 652,128,382,976 bytes free

Update: It was the extra quote at each end of the string that was the problem. I accidentally deleted the $ when I prettied up the code for posting. I just couldn't see my mistake.

Comment on -e fails when file is proven to exist
Select or Download Code
Re: -e fails when file is proven to exist
by chromatic (Archbishop) on Mar 08, 2012 at 20:20 UTC

    You have the name of a directory in the scalar $newDir, and then you write -d {newDir}. What do you expect {newDir} to do? What if you write -d $newDir instead?


    Improve your skills with Modern Perl: the free book.

Re: -e fails when file is proven to exist
by JavaFan (Canon) on Mar 08, 2012 at 20:33 UTC
    You have quotes in your directory name. On top of that, you are using {newDir} instead of $newDir.
Re: -e fails when file is proven to exist
by Eliya (Vicar) on Mar 08, 2012 at 20:33 UTC

    Also (in addition to what chromatic said), I don't think you want the double quotes to be part of $newDir.  Perl's built-in -d isn't parsed/executed via some shell, and thus doesn't need the quotes to keep both space-separated parts of the directory name together. As you have it, the quotes are considered part of the name itself.

Re: -e fails when file is proven to exist
by pemungkah (Priest) on Mar 09, 2012 at 02:32 UTC
    For future people coming to this node (like me, who missed it when you first posted), this now makes no sense because the broken example code is now completely missing. It's better practice to use the strikethru markup or to add an edit that says something like "changed the third line to $foo = $bar. I accidentally deleted the second $ when prepping the code to post" - but leave the initial content alone, or fix it and stick in a comment noting this line was wrong.
      PerlMonks is all about obfuscation, right? :-) I'm fairly new to the Monastery and to Perl, but I'd like to second pemungkah's request.. If everyone consistently wrote perfect code, there would be little reason for this community to exist. Leave your "mistakes" visible, so when I make them I don't feel so lonely.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://958529]
Approved by Eliya
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-08-29 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls