user961627
user961627

Reputation: 12747

Absolute path in perl's copy command gone wrong?

This is my very simple code, which isn't working, for some reason I can't figure out.

#!/usr/bin/perl
use File::Copy;

$old = "car_lexusisf_gray_30inclination_000azimuth.png";
$new = "C:\Users\Lenovo\Documents\mycomp\simulation\cars\zzzorganizedbyviews\00inclination_000azimuth\lexuscopy.png";

copy ($old, $new)  or die "File cannot be copied.";

I get the error that the file can't be copied.

I know there's nothing wrong with the copy command because if I set the value of $new to something simple without a path, it works. But what is wrong in the representation of the path as I've written it above? If I copy and past it into the address bar of windows explorer, it reaches that folder fine.

Upvotes: 0

Views: 157

Answers (2)

choroba
choroba

Reputation: 241748

If no interpolation is intended, use single quotes instead of double quotes.

Upvotes: 3

amon
amon

Reputation: 57590

Tip: print out the paths before you perform the copy. You'll see this:

C:SERSenovodocumentsmycompsimulationrszzzorganizedbyviewsinclination_000azimuthexuscopy.png

Not what we wanted. The backslash is an escape character in Perl, which needs to be escaped itself. If the backslash sequence does not form a valid escape, then it's silently ignored. With escaped backslashes, your string would look like:

"C:\\Users\\Lenovo\\Documents\\mycomp\\simulation\\cars\\zzzorganizedbyviews\\00inclination_000azimuth\\lexuscopy.png";

or just use forward slashes instead – in most cases, Unix-style paths work fine on Windows too.

Here is a list of escapes you accidentally used:

  • \U uppercases the rest
  • \L lowercases the rest
  • \ca is a control character (ASCII 1, the start of heading)
  • \00 is an octal character, here the NUL byte
  • \l lowercases the next character.

Upvotes: 7

Related Questions