[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Perl script as webserver

Posted on 2009-04-20
8
Medium Priority
?
253 Views
Last Modified: 2012-05-06
I wrote a perl script that opens up a file (input file) reads one line , parses the values, then returns the values via an http response as xml format, example
<values>
    <name>joe</name>
    <address>3000 lane ave</address>
</values>

each line in the input file I'm reading from corresponds to one record.  I can get the script to execute via the web browser, except I need to read the next line upon subsequent calls to the perl script, for example
http://myserver/records.pl?grab_record
if that is called again i want it to read the next line (record) in the data file.  
Do I need the script to be running continously to preserve a counter variable so I know which line to read?
0
Comment
Question by:warrior32
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:oleber
ID: 24191018
The solution depends of the problem constrains.

You just have one file and multiple users reading it.
Each user reads a different file.
...

Are you using CGI? mod_perl?


please be more clear, to get a clear answer
0
 

Author Comment

by:warrior32
ID: 24191336
There is only data file.  
If client A makes a request it will get the 1st line,
if client B makes a request it should get the 2nd line,
if client A makes a request it should get the 3rd line.
if client C makes a request it should get the 4the line.

once the end of the file has been reached, it should stop serving, and write a message, until i reset it with some command.
I'm using mod_perl API.  using $r->print to print my xml
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 1800 total points
ID: 24191509
To reset it, empty the counter file.  You can do that with:
    rm /path/to/counter.txt; touch /path/to/counter.txt

use Tie::File;  #Easy access to file
use Fcntl ':flock';  #To avoid more than 1 access at a time
 
#NOTE: Changes these as necessary
my $Lines = '/path/to/file/containing/lines/to/display.txt';
my $Counter = '/path/to/file/to/store/counter.txt';
 
#Access file as an array
tie(my @array, 'Tie::File', $Lines) or die "Could not read file: $!\n";
 
#Open file for read/write access
open(my $io, "+<", $Counter) or die "Could not open/create counter: $!\n";
 
#Lock file, to prevent more than 1 access
flock($io, LOCK_EX);
 
#Read file
seek($io, 0, 0);
my $line=<$io>;
$line=-1 unless defined($line);
 
#Increment line, if not past end
$line++ if $line <= $#array;
 
#Write new value to counter file
seek($io, 0, 0);
print $io $line;
truncate($io, length($line));
 
#Unlock counter file
flock($io, LOCK_UN);
 
#Close counter file
close($io);
 
#Print line from file
if($line <= $#array) {
	print $array[$line];
}
else {
	print "Past end of file\n";
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:warrior32
ID: 24196936
Adam,

Thank you.  I'm no perl expert, but judging from the code seems like the counter file (counter.txt) is just a file with 1 integer?  
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24197894
Yes it is - that is correct.  It contains the line number that was last used.  It is updated on every request.
0
 
LVL 10

Assisted Solution

by:oleber
oleber earned 200 total points
ID: 24201877
In this case I would suggest to use a seek/tell solutions. At least you avoid to read all the file every time.

use strict;
use warnings;
use Fcntl ':flock';
 
#NOTE: Changes these as necessary
my $Lines   = '/path/to/file/containing/lines/to/display.txt';
my $Counter = '/path/to/file/to/store/counter.txt';
 
open( my $FH, "<",  $Lines )   or die "Could not open read file: $!\n";
open( my $io, "+<", $Counter ) or die "Could not open/create counter: $!\n";
 
#Lock file, to prevent more than 1 access
flock( $io, LOCK_EX );
 
#Read file position
seek( $io, 0, 0 );
my $curpos = <$io>;
$curpos = 0 if not defined($curpos);
 
seek( $FH, $curpos, 0 );
 
#Print line from file
if ( my $txt_line = <$FH> ) {
    $curpos = tell($FH);
 
    #Write new value to counter file
    seek( $io, 0, 0 );
    print $io $curpos;
    truncate( $io, length($curpos) );
 
    print $txt_line;
} else {
    warn "Past end of file\n";
}
 
flock( $io, LOCK_UN );
close($io);
close($FH);

Open in new window

0
 

Author Comment

by:warrior32
ID: 24207506
The solution appears to work.  By locking the counter file, if two requests come in at the same time, will there be two instances of the perl script being executed? Will the lock prevent contention issues?
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 1800 total points
ID: 24208055
The web server will start one instance of the script for each request - the lock does not affect this.  So, if two (or more) requests come in at the same time, there will be two (or more) instances of the script running.

The problem (without the lock) is that two instances could read the file, they would get the same number, show the same line, and update it to the same next number.  
With the lock, one script will execute the lock statement slightly earlier, and it will get the lock.  The other script will wait, on the lock statement, until the first script has finished.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question