Newbie with simple File handling problem

J

JasonGilbertUK

Hi All,

I'm working through "Learn Perl in a weekend" and have hit issues in
the file handling section. The code below is taken from the book and is
a simple demo of opening / reading / closing a file.

Everytime I run it, it fails at line 2 when attempting to lock the
file. I have no other apps open that are accessing the file new.txt.
Can anyone tell me what the problem is.

For reference, I'm running under Windows XP Professional, with Admin
privilages and am using ActivePerl 5.8.8

open (sHANDLE, "<c:\\perl\\eg\\weekend\\new.txt") || die ("Cannot open
new.txt");
flock (sHANDLE, LOCK_EX) || die("Cannot lock new.txt");
$onecharacter = getc (sHANDLE);
print $onecharacter
flock (sHANDLE, LOCK_UN) || die("Cannot unlock new.txt");
close (sHANDLE) || die ("Cannot close new.txt");
print "Press <ENTER> to continue...";
<>

Output from the program below, together with directory listing of the
file I'm trying to open.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

c:\perl\eg\weekend>files.pl
Cannot lock new.txt at c:\perl\eg\weekend\files.pl line 5.

c:\perl\eg\weekend>dir n*.txt
Volume in drive C has no label.
Volume Serial Number is BCB0-E820

Directory of c:\perl\eg\weekend

14/11/2006 16:41 11 namesfile.txt
14/11/2006 17:05 40 new.txt
2 File(s) 51 bytes
0 Dir(s) 23,670,530,048 bytes free

c:\perl\eg\weekend>attrib new.txt
C:\perl\eg\weekend\new.txt

c:\perl\eg\weekend>
 
J

JasonGilbertUK

Spent another 45 mins on it and cracked it.
LOCK_EX and LOCK_UN are both coming out as 0 for some reason. Replacing
these with 2 and 8 respectively and fixing the print statement (which
was missing a ;) and it's all working now.
 
J

J. Gleixner

JasonGilbertUK said:
Spent another 45 mins on it and cracked it.
LOCK_EX and LOCK_UN are both coming out as 0 for some reason.

For some reason? The reason is that they're not defined.
Replacing
these with 2 and 8 respectively and fixing the print statement (which
was missing a ;) and it's all working now.

perldoc perlopentut

perldoc Fcntl



use Fcntl qw:)DEFAULT :flock);

Include the reason why, in the die:

my $file = 'c:/perl/eg/weekend/new.txt';
open( my $fh, '<', $file ) or die "Can't open $file: $!";
 
U

Uri Guttman

J> Hi All,
J> I'm working through "Learn Perl in a weekend" and have hit issues in
J> the file handling section. The code below is taken from the book and is
J> a simple demo of opening / reading / closing a file.

the weekend is over and you don't know perl yet? get a better and longer
reading book!

J> open (sHANDLE, "<c:\\perl\\eg\\weekend\\new.txt") || die ("Cannot open
J> new.txt");
J> flock (sHANDLE, LOCK_EX) || die("Cannot lock new.txt");

where is LOCK_EX defined? since i bet you didn't use strict that will be
the string 'LOCK_EX' which will have a value of 0. your other post
figured out the 0 part but did the wrong thing by hardwiring constants.

so see if that book tells you how to get those constants defined. hint:
you have to load a module which defines them.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top