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: Beginner:
can somebody help with this small scripts please

 



gerble1000
User

May 21, 2012, 11:47 AM

Post #1 of 13 (1988 views)
can somebody help with this small scripts please Can't Post

i have a script here wich needs more code inside.
i need it to check if inuse.tmp has the same 8 digit code inside after 30 seconds.


Code
while (1) { 
last if not -e "inuse.tmp";
}



something along these lines


Code
open(FILE,'inuse.tmp'); 
my @lines = <FILE>;
close(FILE);
my $code = $lines[0];
my $count = 0;


while (1) {
$count++
last if not -e "inuse.tmp";

if ("inuse.tmp" == $code) & ($count == "30")
{
unlink "inuse.tmp";
}

}


but still need the "last -e" to still be working during the counting
could you please help correct my code thankyou


rovf
Veteran

May 21, 2012, 11:55 PM

Post #2 of 13 (1972 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post


Quote
8 digit code


I don't understand the question. What is a "8 digit code of a file"? (or of a filename?)


gerble1000
User

May 22, 2012, 4:16 AM

Post #3 of 13 (1963 views)
Re: [rovf] can somebody help with this small scripts please [In reply to] Can't Post

yes inside the inuse.tmp is an 8 digit code that changes all the time. i.e 15368799


rovf
Veteran

May 22, 2012, 6:49 AM

Post #4 of 13 (1959 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post

So your program would go like this:

- extract the code from the file

- wait half a minutes

- extract the new code from the file

- see whether they are different

Correct?


gerble1000
User

May 22, 2012, 12:50 PM

Post #5 of 13 (1949 views)
Re: [rovf] can somebody help with this small scripts please [In reply to] Can't Post

yes but whilst still checking to see if the file exists and if not "last" break out


rovf
Veteran

May 23, 2012, 12:02 AM

Post #6 of 13 (1930 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post


Quote
whilst still checking to see if the file exists


You are doing this implicitly, when fetching the code number from the file. I would create a sub which can be called as

fetch_code_number()

which returns the code number if the file exists, returns the empty string if the file exists but does not contain a valid code number, and returns undef if the file does not exist.

It's up to you what you do if the file exists but can't be opened for reading. One solution would be to also return undef if this case, but of course more clever alternatives are possible.



Quote
if not "last" break out


What does this mean? Sorry, I don't understand the (English) meaning of the sentence.


gerble1000
User

May 23, 2012, 10:04 AM

Post #7 of 13 (1922 views)
Re: [rovf] can somebody help with this small scripts please [In reply to] Can't Post

ok here is what i have wich doesnt seem to work


Code
open(FILE,'inuse.tmp'); 
my @lines = <FILE>;
close(FILE);
my $inusecheck = $lines[0];

my $count2 = 0;

BEFORERANDCHECK:





while (1) {

last if not -e "inuse.tmp";
if ($count2 eq "20")
{
open(FILE,'inuse.tmp');
my @lines = <FILE>;
close(FILE);
my $inusecheck2 = $lines[0];
$inusecheck2 =~ s/\s+$//;

if ($inusecheck == $inusecheck2)
{
unlink("inuse.tmp");
goto BEFORERANDCHECK;
}
}


$count2++
}



gerble1000
User

May 23, 2012, 10:27 AM

Post #8 of 13 (1920 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post

i fixed it with this


Code
open(FILE,'inuse.tmp'); 
my @lines = <FILE>;
close(FILE);
my $inusecheck = $lines[0];

my $countb = 0;

BEFORERANDCHECK:





while (1) {

last if not -e "inuse.tmp";
if ($countb eq "20")
{
open(FILE,'inuse.tmp');
my @lines = <FILE>;
close(FILE);
my $inusecheck2 = $lines[0];
$inusecheck2 =~ s/\s+$//;

if ($inusecheck == $inusecheck2)
{
unlink("inuse.tmp");
goto BEFORERANDCHECK;
}
}

sleep 1;
$countb++
}



i added sleep 1; to the script.
i thought while (1) was loop for 1 second at a time


rovf
Veteran

May 24, 2012, 2:31 AM

Post #9 of 13 (1866 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post

There are a few more things I would change in your code:

(1) Why do you read in the whole file, if you need only the first line?

(2) You repeat the code for reading the first line twice - why don't you make a sub?

(3) Are you aware that 99 out of 100 project managers will burn you on the stake, if you use a 'goto'?

(4) strict and warnings are missing


FishMonger
Veteran / Moderator

May 24, 2012, 6:37 AM

Post #10 of 13 (1861 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post

A few other problems/mistakes to note.

1) Using a bareword for the filehandle instead of a lexical var

2) Not using the 3 arg form of open

3) No error handling on the open call. You should ALWAYS check the return code of an open call!

4) Needlessly using a counter var

5) Using a string comparison when checking the int value of the counter

Your actual code probably needs to do more than what you've posted, but here's a rewrite of that code.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $file = 'inuse.tmp';
open my $fh, '<', $file or die "failed to open '$file' <$!>";
chomp(my $code1 = <$fh>);

my $code2 = 0;

while ($code1 != $code2) {

sleep 20;
seek $fh, 0,0;

chomp($code2 = <$fh>);

}
close $fh;
unlink $file;



FishMonger
Veteran / Moderator

May 24, 2012, 6:42 AM

Post #11 of 13 (1859 views)
Re: [FishMonger] can somebody help with this small scripts please [In reply to] Can't Post

After thinking about it, you may need to close/reopen the file in the loop rather than using seek.


gerble1000
User

May 24, 2012, 9:19 AM

Post #12 of 13 (1855 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post

what the issue is,
when i have say "5" people using the script at the same time, i want the script to make a random number and store it in a file "inuse.tmp"
then wait 1 second and check if the inuse.tmp has your random code in. if so continue if not go back to the begining.
whilst another is using the script there script will check for a inuse.tmp if there is one it will wait untill it has been removed. but if it hasnt been removed in say "20 seconds" then remove it.
and make my own attempt of creating an inuse.tmp with my own random number... i am basicaly making a session per user by puting down an inuse sign as to stop others conflicting with the rest of the script :)

also fishmonger your script looks great just curious about the sleep 20 does this mean it will wait 20 seconds to see if the inuse.tmp has been removed as that will be to long. it needs to check for free session at least once a second


FishMonger
Veteran / Moderator

May 24, 2012, 10:34 AM

Post #13 of 13 (1853 views)
Re: [gerble1000] can somebody help with this small scripts please [In reply to] Can't Post

You should rethink your strategy. That is pretty odd way to approach your goal.

Yes, my code checks every 20 seconds, which is exactly what your code did.

 
 


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

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