CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Win32 Programming Help:
open files???

 



perlkid
stranger

Sep 18, 2000, 1:31 AM

Post #1 of 10 (6916 views)
open files??? Can't Post

 
Hey Dws and Brian,

My Boss figured it all out, he got perl running on his system. Smile

Turns out I had everything a-ok except I was missing DCOM for Windows 98. Doh!

But now that we have perl on win 98, We are having trouble getting our programs to run because of the open function.

I'm using the regular open function and it won't read the file. This is what doesn't work,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(people, "<users");
flock people, LOCK_SH;
@all=<people>;
flock people, LOCK_UN;
close(people);
</pre><HR></BLOCKQUOTE>

Now that I think about it I don't think th < is on the open function in te script. Would that matter on Win os?

Or is there a completly different way all together.

One more thing,

when I use LWP::Simple;

I get compiling errors. It says it cannot find LWP::Simple I know it on there too.

Is there something different about win 32 here also?

Thanks A lot,

perlkid


Kanji
User

Sep 18, 2000, 3:35 AM

Post #2 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

kid, you should know better by now: what must you always, always, always do with an open()?

As for LWP::Simple, how did you install it? If not by ppm, you may have problems. It's helpful if you can give the exact error message, along with a copy of your @INC path, and the path where you have LWP::Simple installed.


dws
Deleted

Sep 18, 2000, 9:35 AM

Post #3 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

perlkid -- flock() doesn't work on windows 98. One of several reasons why '98 isn't a robust hosting platform.

Dunno about why the Perl can't seem to find LWP::Simple. Try

perl -V

from a command shell. The (capital) V option will dump more info than (lowercase) v, including the paths Perl will search for modules.



[This message has been edited by dws (edited 09-18-2000).]


perlkid
stranger

Sep 18, 2000, 2:51 PM

Post #4 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

 
Thanks Guys,

But Kanji,

I got confused, I was trying to use open()

But guess what it was after all, since \ means to escape, and the path in windows uses \ instead of /, It kinda doesn't mean much to open(data, "C:\bla\bla\bla.txt");

I know I didn't put that in my prev example and I'm soo sure you knew that already.

So what I did was this, open(data, "C:\\bla\\bla\\bla.txt");

And win98 stinks for not suporting flock.

Thanks a lot guys,

Thanks,

perlkid (Better Known As ConfusedKid)


dws
Deleted

Sep 18, 2000, 3:16 PM

Post #5 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

Perl automagically translates forward slashes in paths to backslashes. There's no need to use backslashes.


Kanji
User

Sep 18, 2000, 5:03 PM

Post #6 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

It doesn't matter whether or not you fully qualified the path to the file you were opening: you should still be checking open()'s return code if only to stop yourself from executing code unnecessarily (ie, whats the point of trying to flock or read a non-existant file handle?).

In this case, if would have discounted open() as the culprit because it wasn't die()ing (or however else you wanted to handle it) at that point.

Also, about flock(), there's no need to unflock(): perl will handle that for you when you close() the file so that you risk less of a chance of mucking things up. ;)

You may also want to investigate using LOCK_NB to stop flock() from waiting indefinitely.


perlkid
stranger

Sep 18, 2000, 8:39 PM

Post #7 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

 
Thanks Kanji,

Yea, About that LOCK_NB, how do I use it. In this book "Perl Core Language Little Black Book" it mentions it and says

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>LOCK_NB(=1)--Use with LOCK_SH or LOCK_EX for nonblocking access (that is, flock returns at once, before verifying the lock is active).<HR></BLOCKQUOTE>

But that's all it says, where and how do I use it anyway. But You said to <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>investigate using LOCK_NB<HR></BLOCKQUOTE> So never mind I sould go find out myself. Thanks for that tip, I'm going to do that.

The only reason I was worried about flocks is I read in this book that on unix flock is only advisory, but on other os's like NT it's mandatory.

And thanks for the tip also dws, But I found that code to work and then I just kept using it since I stuggled with it for about 2 hours.

Thanks Again Dws And Kanji

perlkid


perlkid
stranger

Sep 18, 2000, 8:53 PM

Post #8 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

 Kanji,

I'm not on NT or anything, but I though win 98 would have been the same until dws told me that win98 doesn't support flock.

Thanks for the tip about LOCK_UN, that's what I meant I was going to do.

But I don't even think I'm using flock right, I read that flock handle, LOCK_SH; needs module for it to work, they say to use Fcntl;

That sound right to you?

So would I have to do this,

use Fcntl;
if ( open file, $data){
flock file, LOCK_SH;
@all=<file>;
close(file);
}else
{
print "Something you did has caused a fatal error and you should never come here again, $!";
}

Does that look right to you.

And is it more efficent than what I usually do,

open(file, "<$data");
@all=<file>;
close(file);

Is it any slower?

Thanks,

perlkid

[This message has been edited by perlkid (edited 09-18-2000).]


perlkid
stranger

Sep 19, 2000, 12:37 AM

Post #9 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

 
OK, I did some looking around and I found that this code answered all of my flock questions.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use Fcntl;
sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile: $!";
flock(FH, 2) or die "can't flock numfile: $!";
$num = <FH> &#0124; &#0124; 0;
seek(FH, 0, 0) or die "can't rewind numfile: $!";
truncate(FH, 0) or die "can't truncate numfile: $!";
(print FH $num+1, "\n") or die "can't write numfile: $!";
# DO NOT UNLOCK THIS UNTIL YOU CLOSE
close FH or die "can't close numfile: $!";
</pre><HR></BLOCKQUOTE>

Thanks Guys

Perlkid


dws
Deleted

Sep 19, 2000, 12:19 PM

Post #10 of 10 (6916 views)
Re: open files??? [In reply to] Can't Post

Small nit: seek(FH,0,0) is redundant, since you're not opening the file in append mode.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives