G'day virtuemart2,
Welcome to the monastery.
You've got a few issues with that. Firstly, you can test that string yourself:
$ perl -Mstrict -Mwarnings -le 'print "C:\windows\file.txt"'
Unrecognized escape \w passed through at -e line 1.
C:windows
ile.txt
Obviously, that's not good. Note that -Mwarnings (on the command line) is the same as use warnings; (in a script). When it's used, Perl tells you what's wrong; when you don't use it, you just see strange output without any explanation:
$ perl -Mstrict -le 'print "C:\windows\file.txt"'
C:windows
ile.txt
See warnings: it's your friend.
Also note -Mstrict (as I have here, on the command line) is the same as use strict; (in a script).
See strict: it, too, is your friend.
If you're stuck with a situation where you need double-quotes, you must escape (prefix with a backslash) each backslash, i.e. write "\\" wherever you had "\":
$ perl -Mstrict -Mwarnings -le 'print "C:\\windows\\file.txt"'
C:\windows\file.txt
That looks rather messy, and would be even messier with a long pathname. The best thing to avoid this, is to use single-quotes, i.e. 'C:\windows\file.txt'. I can't actually show that on the command line because I'm already using single-quotes; however, '...' is the same as q{...}, so:
$ perl -Mstrict -Mwarnings -le 'print q{C:\windows\file.txt}'
C:\windows\file.txt
And, just for completeness, "..." is the same as qq{...}:
$ perl -Mstrict -Mwarnings -le 'print qq{C:\windows\file.txt}'
Unrecognized escape \w passed through at -e line 1.
C:windows
ile.txt
Although, I've used braces ({...}) to delimit the string, you can use other characters: matchingleft-right pairs such as q[...] and q(...), and non-matchingidentical pairs such as q/.../ and q|...|.
This is explained in perlop: Quote and Quote-like Operators, with further details in perlop: Quote-Like Operators.
[Yes, those two section headings are somewhat confusing — I didn't write them. :-) ]
That's strings sorted out. The next problem is open fh $PATH.
Assuming you'd given $PATH a value, you'd get this somewhat cryptic message:
$ perl -Mstrict -Mwarnings -e 'my $PATH = q{not_a_file}; open fh $PATH
+'
Can't locate object method "fh" via package "not_a_file" (perhaps you
+forgot to load "not_a_file"?) at -e line 1.
The problem is that you've used incorrect syntax for the open function.
You could just make a minor change to fix the syntax; however, it would be much better if you wrote that like this:
open my $fh, '<', $path or die "Can't open $path: $!";
See open for a detailed explanation.
Also look at die; and you'll find $! explained in perlvar.
Putting all of that together:
# Uncomment ONLY ONE of these:
#my $path = 'C:\windows\path\to\filename';
#my $path = '/linux/path/to/filename';
# Now open your chosen file for reading:
open my $fh, '<', $path or die "Can't open $path: $!";
Finally, have a look at perlrun, so you can run those sorts of tests for yourself; and, I suspect, you'd benefit greatly from reading "perlintro -- a brief introduction and overview of Perl".
|