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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

*** Perl Syntax Problem *** HELP!

Status
Not open for further replies.

jonnywah

Programmer
Feb 7, 2004
80
0
0
US
I have been stuck 2 days on this code. I am using Perl to create directories with HTML files. The directories are being created but the HTML files are not. Can anyone see what is the problem with my code here? Any suggestions or information would be appreciated. Thank you in advance.


#!/C/Perl/bin/perl

use MSSQL::DBlib;
use MSSQL::DBlib::Const::General;
$dbh = MSSQL::DBlib->dblogin('123', 'password123', 'COMP_NAME', 'DB_NAME');
print "1\n";
$query = "SELECT a.businessTypeId as MYBTID, a.*, businessIndustryDescription, businessTypeDescription FROM db1.dbo.Business a, db1.dbo.BusinessIndustry b, db1.dbo.BusinessType c WHERE a.BusinessIndustryId = b.BusinessIndustryId AND a.BusinessTypeId = c.BusinessTypeId AND a.BusinessStatusId = 1 ORDER BY MYBTID";
$dbh->dbcmd($query);
$dbh->dbsqlexec;
$dbh->dbresults;

$curr = 0;
while($dbh->dbnextrow2($dataref, 1) != NO_MORE_ROWS) {

### $businessIndustryId = $$dataref{"businessIndustryId"};

$businessTypeId = $$dataref{"businessTypeId"};
######$businessTypeId = $$dataref{"MYBTID"};

# new category??
### if ($curr != $businessIndustryId) {

if ($curr != $businessTypeId) {
# close category file
if ($curr != 0) {
open (FOOTERTEMPLATE, "litefooter.html");
@footer = <FOOTERTEMPLATE>;
close (FOOTERTEMPLATE);
foreach $line (@footer) {
print CATFILE $line;
}
close (CATFILE);
}
# create new directory
### $curr = $businessIndustryId;
### $dir = $$dataref{"businessIndustryDescription"};
### $dir =~ s/ /_/g;
### mkdir ($dir);
$curr = $businessTypeId;
###### can't comment out dir or directories don't get created, why?
$dir = $$dataref{"businessIndustryDescription"};
$dir2 = $$dataref{"businessTypeDescription"};
$dir2 =~ s/ /_/g;
mkdir ($dir2);

# create header of category file
### open (CATFILE, "> $dir/litecatlistings.html");
open (CATFILE, "> $dir2/litecatlistings.html");


$bizti = "TEST";
$metabizti = "TEST";

open (HEADERTEMPLATE, "litecatheader.html");
@header = <HEADERTEMPLATE>;
close (HEADERTEMPLATE);
foreach $line (@header) {

###### $line =~ s/--TITLE--/$$dataref{"businessTypeDescription"}/;
$line =~ s/--TITLE--/$$dataref{"businessIndustryDescription"}/;
$line =~ s/--BIZTYPEINFO--/$bizti/;
$line =~ s/--METABIZTYPEINFO--/$metabizti/;
print CATFILE $line;

}
}

# append body part category file
open (BODYTEMPLATE, "litecatbody.html");
@body = <BODYTEMPLATE>;
close (BODYTEMPLATE);
foreach $line (@body) {

$line =~ s/--BUSINESSTITLE--/$$dataref{"businessTitle"}/;
$state = $$dataref{"businessState"};
$country = $$dataref{"businessCountry"};
if ($state !~ m/^\s*$/ && $country !~ m/^\s*$/) {
$location = $state.",".$country;
}
elsif ($state !~ m/^\s*$/) {
$location = $state;
}
elsif ($country !~ m/^\s*$/) {
$location = $country;
}
else {
$location = "N/A";
}
$line =~ s/--LOCATION--/$location/;
$description = $$dataref{"businessDescription"};
if (length ($description) > 100) {
$description = substr ($description, 0, 97)."...";
}
$line =~ s/--DESCRIPTION--/$description/;
$url = "listing".($$dataref{"businessId"}*9).".html";

##$mydir = $$dataref{"businessIndustryDescription"};
$myurl = "
####$line =~ s/--URL--/$url/;
$line =~ s/--URL--/$myurl/;


print CATFILE $line;

}

# create listing file
### open (LISTINGFILE, "> $dir/$url");
open (LISTINGFILE, "> $dir2/$url");
open (HEADERTEMPLATE, "litecatheaderForListing.html");
@header = <HEADERTEMPLATE>;
close (HEADERTEMPLATE);
foreach $line (@header) {
### $line =~ s/--TITLE--/$$dataref{"businessIndustryDescription"}/;
$line =~ s/--TITLE--/$$dataref{"businessTypeDescription"}/;
#for metatags
$line =~ s/--BUSINESSTITLE--/$$dataref{"businessTitle"}/;
$line =~ s/--LOCATION--/$location/;
$line =~ s/--BIZCATSEOINFO--/$bizti/;
print LISTINGFILE $line;
}

open (BODYTEMPLATE, "litelistingbody.html");
@body = <BODYTEMPLATE>;
close (BODYTEMPLATE);
foreach $line (@body) {
$line =~ s/--BUSINESSTITLE--/$$dataref{"businessTitle"}/;


$state = $$dataref{"businessState"};
$country = $$dataref{"businessCountry"};
if ($state !~ m/^\s*$/ && $country !~ m/^\s*$/) {
$location = $state.",".$country;
}
elsif ($state !~ m/^\s*$/) {
$location = $state;
}
elsif ($country !~ m/^\s*$/) {
$location = $country;
}
else {
$location = "N/A";
}
$line =~ s/--LOCATION--/$location/;
####$line =~ s/--LOCATION--/$location." ".$bizti/;
$line =~ s/--DESCRIPTION--/$$dataref{"businessDescription"}/;
$line =~ s/--USERID--/$$dataref{"businessId"}/;

if ($line =~ m/--ASKINGPRICE--/) {
if ($$dataref{"businessAskingPrice"} !~ m/^\s*$/) {
$line =~ s/--ASKINGPRICE--/$$dataref{"businessAskingPrice"}/;
}
else { $line = ""; }
}

if ($line =~ m/--REVENUE--/) {
if ($$dataref{"businessSalesRevenue"} !~ m/^\s*$/) {
$line =~ s/--REVENUE--/$$dataref{"businessSalesRevenue"}/;
}
else { $line = ""; }
}

if ($line =~ m/--INVENTORY--/) {
if ($$dataref{"businessInventory"} !~ m/^\s*$/) {
$line =~ s/--INVENTORY--/$$dataref{"businessInventory"}/;
}
else { $line = ""; }
}


print LISTINGFILE $line;
}


open (FOOTERTEMPLATE, "litefooter.html");
@footer = <FOOTERTEMPLATE>;
close (FOOTERTEMPLATE);
foreach $line (@footer) {
print LISTINGFILE $line;
}
close (LISTINGFILE);
}
open (FOOTERTEMPLATE, "litefooter.html");
@footer = <FOOTERTEMPLATE>;
close (FOOTERTEMPLATE);
foreach $line (@footer) {
print CATFILE $line;
}
close (CATFILE);

print "script Finished!\n";

$dbh->dbcancel;
$dbh->dbclose;
 
Run the script.

Change the mode of the newly created directories to 777.

Run the script again.

If the files show up; you have a permission issue.

 
This is not a permission problem. I have created other scripts that create directories and HTML files. Any thoughts or suggestions? Please help.
 
Code:
 open (CATFILE, "> $dir2/litecatlistings.html") or die "can't create file $!";
should give you more information

HTH
--Paul

cigless ...
 
I used:

open (CATFILE, "> $dir2/litecatlistings.html") or die "can't create file $!";


No errors and still only the directories are created. I still can't create the HTML files. Please help.
 
then this means the files are being created, opened at any rate, try looking around the disk for the errant files.

it also looks like you can close the file without opening it, don't have your data (and don't want it ;-)), so can't prove it.

try a anippet of code to do wehat you're trying to do. We tend to help more when we can see the context directly ;-)

--Paul

cigless ...
 
Paul,

The problem was from the database. I removed the "/" from the "businessTypeDescription" field.

"litecatlistings.html" is created in each directory but it only prints the last record of a particular businessType.

Not sure what the bug can be ... any suggestions?

 
You're closing the file, and the reopening it to write more than once - hence the last line as the only

open FH, ">>myfile.txt" will open the file for append.

What I'd really suggest is looking at your code again and coming up with a more traditional structure. It works, don't get me wrong, but it'll be a nightmare to maintain. Just my €0.02

--Paul

cigless ...
 
I found the bug ... $url is not getting a value, so the code breaks.

See:
$url = "listing".($$dataref{"businessId"}*9).".html";



Not sure why this is happening though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top