Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i don't know whay is not working ?

Status
Not open for further replies.

z3phir

Programmer
Dec 15, 2002
16
RO
please anyone knows what i'm doing wrong ? i'm new to perl and this is my first script. when i test it on my computer it works when i upload it to my website it dosent
P.S: scuse my bad english

on my computer i use my $filenumber ='1';
on my site my $filenumber = $ENV{'QUERY_STRING'};

to get the script use
and the data file
 
Please see the faqs page (see the tab at the top of this forum) including faq452-653 and faq452-1816. There are a few others worth reading through.

Maybe they will help you get your first one working. Feel free to come back with questions. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
nope didn't welp ! please test the script i think i don't use the $ENV{'QUERY_STRING'}; corectly.
 
sorry about that here is the source code

#!/usr/bin/perl -w

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $headfile = 'header.txt';

my $footfile = 'footer.txt';

my $twfx_file = 'tplates.dat';

my ($uselock, @headinfo, $headline, $heading, @footinfo, $footline, $footer);

my $record = '';

my $filenumber = $ENV{'QUERY_STRING'};


# Get the heading information

unless (open (DATA,"$headfile")) {die (&error);}
if ($uselock eq '1') {
flock DATA, 2;
seek DATA, 0, 0;
}
@headinfo = <DATA>;
if ($uselock eq '1') {
flock DATA, 8;
}
close (DATA);
foreach $headline (@headinfo){
$heading = $heading.$headline;
}

# Get the footer information

unless (open (DATA,&quot;$footfile&quot;)) {die (&error);}
if ($uselock eq '1') {
flock DATA, 2;
seek DATA, 0, 0;
}
@footinfo = <DATA>;
if ($uselock eq '1') {
flock DATA, 8;
}
close (DATA);
foreach $footline (@footinfo){
$footer = $footer.$footline;
}



open(TWFX_FILEHANDLE, $twfx_file) or die &quot;Cannot open totalwebfx templates file $twfx_file&quot;;

$/ = &quot;//\n&quot;;



while($record = <TWFX_FILEHANDLE>) {

my %line_types = get_line_types($record);

defined $line_types{'FN'} or next;

$line_types{'FN'} =~ /TEMPLATE/ or next;

my $fn = $line_types{'FN'};
$fn =~ s/^FN //;
$fn =~ s/; .*//;

my $pattern = $line_types{'NR'};
$pattern =~ s/NR //g;

my $fl = $line_types{'FL'};
$fl =~ s/FL //g;
$fl = twfx_2_regexp($fl);

my $fd = $line_types{'FD'};
$fd =~ s/FD //g;
$fd = twfx_2_regexp($fd);

my $regexp = twfx_2_regexp($pattern);

while ($filenumber =~ /$regexp/g) {
my $position = ((pos $filenumber) - length($&) +1);
print &quot;Content-type:text/html\n\n&quot;;
print &quot;$heading&quot;;
print &quot;$fn<br>\n&quot;;
print &quot;$fl<br>\n&quot;;
print &quot;$fd<br>\n&quot;;
print &quot;$footer&quot;;
}
}

#
# Exit the program
#
exit;



##################################################
#
# Subroutines
#
##################################################

sub twfx_2_regexp {


my($pattern) = @_;

my $regexp = $pattern;

return $regexp;
}

sub twfx_2_regexp_clever {

my($pattern) = @_;

$pattern =~ s/{/[^/g;
$pattern =~ tr/cx}()<>\-\./C.]{}^$/d;
$pattern =~ s/\[G\$\]/(G|\$)/;
return $pattern;
}


sub get_line_types {

my($record) = @_;

my %line_types_hash = ();

my @records = split(/\n/,$record);

foreach my $line (@records) {

my $line_type = substr($line,0,2);

(defined $line_types_hash{$line_type})
? ($line_types_hash{$line_type} .= $line)
: ($line_types_hash{$line_type} = $line);
}

return %line_types_hash;
};
 
Try moving the print &quot;Content-type:text/html\n\n&quot;; line to fifth line, just to see if the script dies. //Daniel
 
didn't resolve nothing. any other advice ?
 
sorry i don't have acces to the error log on the server.
 
z3phir,

When you're grabbing the file number from the query string, what are valid numbers to use? The link to your script doesn't have a query string to validate any information. I can say that the $ENV{QUERY_STRING} variable will capture everything after the ? in the URL. for instance, if I go to the page:


I'll get openSesame=alibaba as my $ENV{QUERY_STRING} value.

I'm not sure that is what is causing the error, but if I was to guess, I'd say that the error is being caused because the file doesn't have the correct permissions to run. Could you try one of the following to get more information on this?

[ol][li]Change the error pages to be more descriptive. For instance, if you have a 501 error, have the page state that it is a 501. This will help determine what type of internal server error is occurring.[/li]

[li]Check the file permissions. They need to be set to 755 so people can execute the script.[/li][/ol]

I noticed something else that struck me as odd when looking at the code. When you're opening the header and footers for reading, you're running some code if $uselock is set to the value 1, but this never gets set to 1 before opening the header and footer. Try changing the locking statements to the following.

Code:
unless (open (DATA,&quot;$headfile&quot;)) {die (&error);}
if (!$uselock) {
    flock DATA, 2;
    seek DATA, 0, 0;
    $userlock = '1';
}
@headinfo = <DATA>;
if ($uselock eq '1') {
    flock DATA, 8;
    undef $uselock;
}
close (DATA);
foreach $headline (@headinfo){
    $heading = $heading.$headline;
}

Also, you have a subroutine set to run if the program cannot open the header and footer when the subroutine does not exist in your program. This might have just been an oversight when copying the code, but just in case it's not. . .

Let me know if any of this helps!

- Rieekan
 
Nope i tried still dosen't work. For the variable please use 1. Best regards.
 
Is the CGI module installed on your server? Make a test program that simply uses CGI and CGI::Carp modules and then prints a success statement. If it is chmod'ed 775 and uploaded in ASCII format but still fails, then ask your system administrator to install the CGI module for you.

When
Code:
flock
'ing and
Code:
seek
'ing,
Code:
|| die &quot;$!&quot;;
Sometimes flock will fail because the file is in use or has the wrong permissions, or because the flock functionality does not exist on your system. Here's how I like to do my file locking...
Code:
use Fcntl qw(:flock);

sub assert_dominance 
{
	my ($handle,$type) = @_;

	# assert yourself
	unless (flock ($handle, $type)) 
	{
		# get impatient
		warn &quot;Contention: cannot get a lock yet... waiting ($!) ...&quot;; sleep 3;

		# reassert yourself or give up
		unless (flock ($handle, $type)) {die &quot;File lock error: $!&quot;;}
	}
}

sub learn_to_share 
{
	my ($handle) = @_;

	# give back to the community
	unless (flock($handle, LOCK_UN)) {die &quot;File unlock error: $!&quot;;}
}

open (FILE, &quot;>$file&quot;) || die &quot;Cannot open file $file: $!&quot;;
assert_dominance (\*FILE,LOCK_EX);
print FILE $stuff;
learn_to_share(\*FILE); # not strictly necessary since close() does it anyway
close(FILE);

BTW, you should also comment your code a lot more... at least one comment every 3-4 lines, or once per block.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top