Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Variable fails where absolute path does not

by DSMcL (Initiate)
on May 14, 2009 at 05:42 UTC ( [id://764003]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a script to automatically extract archives. After extracting, I am able to get the path to the file from unrar.exe and am using the -e test to be sure the file exists on the hard disk.

The filename and path are being passed via a variable here:

unless (-e $file) { die "$file did not extract properly"; }

The following is the file and path I was using to test:

"0794515576\The Usborne Complete Book of the Human Body_0794515576.pdf"

The variable $file contains exactly what is above and the file exists but the test fails and the program dies. If I substitute the path and file name listed above in place of the variable containing the same information, the test passes and the script continues normally.

I am using the script on win32. I have tried using the full path including drive letter with no better luck. I have been unable to figure out how to get the variable to work.

Any help would be greatly appreciated!
Thank you

Replies are listed 'Best First'.
Re: Variable fails where absolute path does not
by CountZero (Bishop) on May 14, 2009 at 05:56 UTC
    Try changing the backslashes to forward slashes.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      It helped in python also!. Thanks CountZero

      Vivek
      -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Variable fails where absolute path does not
by ysth (Canon) on May 14, 2009 at 06:16 UTC
    \T in a double quoted string becomes just T (with a warning that the backslash is ignored).

    It's also possible you have a trailing newline on your filename. Try:

    use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper($file);
    to clearly see the contents of $file.
Re: Variable fails where absolute path does not
by apl (Monsignor) on May 14, 2009 at 12:19 UTC
    Replacing

            die "$file did not extract properly";

    with

            die "File |$file| did not extract properly";

    helps to immediately point out that something is funky with the pathname, when the error is displayed.

Re: Variable fails where absolute path does not
by DSMcL (Initiate) on May 14, 2009 at 06:28 UTC
    Many thanks! I do believe Data dumper will be the way to go as I did end up with the newline problem you mentioned.
      Run in debugger mode and check whether the values are substituted like what you are believing or something else.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764003]
Approved by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found