Click to See Complete Forum and Search --> : Use/Require to save file space


Ultimater
08-07-2005, 07:50 PM
The code beneath works fine but takes up a massive amount of room and blinds you from the birds-eye view of the code. Is there a way that I can move the red-colored text into a PM or PL file then use use or require to call the code to save room? I tried both approaches but I get an error when I run the code using an include or use statement when trying to put the red-colored code into a seperate file:

Global symbol "$upload1_name" requires explicit package name at /cgi-bin/ultimater/business_submit.pl line 42.
Global symbol "$upload2_name" requires explicit package name at /cgi-bin/ultimater/business_submit.pl line 42.
Execution of /cgi-bin/ultimater/business_submit.pl aborted due to compilation errors.



#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use File::Basename;
use HTML::Template;
use DBI;
use uploadStuff;
print header;

our $userid = &generateUniqueUserId;

our $base_href = "../../Business/images/";
our $database_dir = "../../Business/images/";
our $bus_csv = "../../Business/Business.csv";




our $megaByte=1073741824;
our $upload1_name="";
our $upload2_name="";
our $upload1 = param "field_15";
our $upload2 = param "field_16";
our $suffix1=getImageExtension($upload1);
our $suffix2=getImageExtension($upload2);
if($suffix1){$upload1_name="p".$userid."n1.".$suffix1;}
if($suffix2){$upload2_name="p".$userid."n2.".$suffix2;}
if($suffix1 ne ""){
if(-s $upload1 > $megaByte){
$upload1="";$upload1_name="";$suffix1="";
}
else{
if (-e ($database_dir.$upload1_name)){unlink($database_dir.$upload1_name);}
open(IMAGEFILE, "> ".$database_dir.$upload1_name) or die "error: opening \$upload1_name!";
print IMAGEFILE <$upload1>;
close(IMAGEFILE);
}
}

if($suffix2 ne ""){
if(-s $upload2 > $megaByte){
$upload2="";$upload2_name="";$suffix2="";
}
else{
if (-e ($database_dir.$upload2_name)){unlink($database_dir.$upload2_name);}
open(IMAGEFILE, "> ".$database_dir.$upload2_name) or die "error: opening \$upload2_name!";
print IMAGEFILE <$upload2>;
close(IMAGEFILE);
}
}






my $busname=unilinize(max80(param "field_12"));
my $businfo=max10000(param "field_9");
my $website=unilinize(max80(param "field_4"));
my $street=unilinize(max80(param "field_23"));
my $city=unilinize(max80(param "field_25"));
my $zipcode=unilinize(max80(param "field_26"));
my $state=unilinize(max80(param "field_7"));
my $phonenumber=unilinize(max80(param "field_5"));
my $faxnumber=unilinize(max80(param "field_22"));
my $e_mail=unilinize(max80(param "field_10"));
my $link1=unilinize(max80(param "field_8"));
my $link2=unilinize(max80(param "field_13"));
my $password=unilinize(max80(param "field_21"));
&databaseWrite($bus_csv,$userid,$busname,$businfo,$website,$street,$city,$zipcode,$state,$phonenumber ,
$faxnumber,$e_mail,$link1,$link2,$upload1_name,$upload2_name,$password);


#############################################################################
# get data
#############################################################################

unless(-e $bus_csv){die "fatal error opening file: Business.csv";}
open(CSVFILE, $bus_csv) or die "fatal error opening file: Business.csv";
my @incoming=<CSVFILE>;
close(CSVFILE);
grep(chomp $_, @incoming);




my $submitLink = "../../cgi-bin/ultimater/testing123.pl";
my $backLink = "javascript:history.go(-1)";

my $pic1="";
my $pic2="";
my $busName="";
my $busInfo="";
my $website="";
my $street="";
my $city="";
my $zipcode="";
my $state="";
my $phoneNumber="";
my $faxNumber="";
my $email="";
my $link1="";
my $link2="";
my $getRecord = $userid;
foreach my $Line (@incoming)
{
my ($userid1,$busname1,$businfo1,$website1,$street1,$city1,$zipcode1,$state1,$phonenumber1,$faxnumber1, $e_mail1,$link11,$link21,$upload1_name1,$upload2_name1,$password1)= split(/\,/, $Line);
if ($userid1 eq $getRecord) {

if(length($upload1_name1) > 1){
$pic1="<img src=\"".$base_href.$upload1_name1."\">";
}
if(length($upload2_name1) > 1){
$pic2="<img src=\"".$base_href.$upload2_name1."\">";
}
$busName=unescape($busname1);
$busInfo=unescape($businfo1);
$website=unescape($website1);
$street=unescape($street1);
$city=unescape($city1);
$zipcode=unescape($zipcode1);
$state=unescape($state1);
$phoneNumber=unescape($phonenumber1);
$faxNumber=unescape($faxnumber1);
$email=unescape($e_mail1);
$link1=unescape($link11);
$link2=unescape($link21);

}
}

$busInfo=~ s/\r\n/<br>/g;
$busInfo=~ s/\n/<br>/g;
$busInfo=~ s/\r/<br>/g;


my $template = HTML::Template->new(filename => '../../Business/template_business_newentry.htm');

$template->param(submitLink=> $submitLink );
$template->param(backLink => $backLink);

$template->param(pic1 => $pic1);
$template->param(pic2 => $pic2);
$template->param(busName => $busName);
$template->param(busInfo => $busInfo);
$template->param(website => $website);
$template->param(street => $street);
$template->param(city => $city);
$template->param(zipcode => $zipcode);
$template->param(state => $state);
$template->param(phoneNumber => $phoneNumber);
$template->param(faxNumber => $faxNumber);
$template->param(email => $email);
$template->param(link1 => $link1);
$template->param(link2 => $link2);


print $template->output;






__END__


Again, the above code, as is, works fine. But how would I go about putting the red-colored text into a seperate file to save me room in this file?

If it helps, here's my module uploadStuff that I wrote:

my @ext = qw(gif jpeg jpg bmp png);
foreach (@ext) { s/\./\\./; }

sub generateUniqueUserId{

unless(-e "newuser.counter"){
open(COUNTERFILE, "> newuser.counter") or die "fatal error creating file: newuser.counter";
print COUNTERFILE "1";
close(COUNTERFILE);
}

open(COUNTERFILE, "newuser.counter") or die "fatal error openning file: newuser.counter";
local ($counterId) = <COUNTERFILE>;
close(COUNTERFILE);

open(COUNTERFILE, "> newuser.counter") or die "fatal error writting to file: newuser.counter";
print COUNTERFILE $counterId+1;
close(COUNTERFILE);

return $counterId;
}


sub getImageExtension{
local ($tmpFilePath)=@_;
local ($name1,$path1,$suffix1) = fileparse(lc $tmpFilePath,@ext);
return $suffix1;
}
sub max80{
local ($string)=@_;
return substr($string,0,80);
}
sub max10000{
local ($string)=@_;
return substr($string,0,10000);
}
sub escape {
my($str) = splice(@_);
$str =~ s/(\W)/sprintf('%%%02X', ord($1))/eg;
return $str;
}

sub unescape {
my($str) = splice(@_);
$str =~ s/%(..)/chr(hex($1))/eg;
return $str;
}

sub databaseWrite{
local ($bus_csv,$userid,$busname,$businfo,$website,$street,$city,$zipcode,$state,$phonenumber,$faxnumber,$ e_mail,$link1,$link2,$upload1_name,$upload2_name,$password)=@_;
unless(-e $bus_csv){open(CSVFILE, "> ".$bus_csv) or die "fatal error creating file: Business.csv";print CSVFILE "MEM ID,BUS NAME,BUS INFO,WEBSITE,STREET,CITY,ZIP,STATE,PHONE,FAX,EMAIL,LINK1,LINK2,UPLOAD1,UPLOAD2,PASS";close(CSVFILE);chmod 0700, $bus_csv;}
open(CSVFILE, ">> ".$bus_csv) or die "fatal error appendin to file: Business.csv";
print CSVFILE "\r\n".$userid.",".escape($busname).",".escape($businfo).",".escape($website).",".escape($street).",".escape($city).",".escape($zipcode).",".escape($state).",".escape($phonenumber).",".escape($faxnumber).",".escape($e_mail).",".escape($link1).",".escape($link2).",".escape($upload1_name).",".escape($upload2_name).",".escape($password);
close(CSVFILE);
}

sub unilinize{
local ($text)=@_;
local @lines = split("\n",$text);
local $line = $lines[0];
$line =~ s/(\r|\n)//g;
return $line;
}

1;#return true

Jeff Mott
08-07-2005, 10:23 PM
Here's a quick example.#MyMod.pm

package MyMod;

our $var1 = 1;
our $var2 = 2;
our $var3 = 3;

1;use MyMod;

print $MyMod::var1;

- or -

#MyMod.pm

package MyMod;

use base Exporter;
our @EXPORT = qw[$var1 $var2 $var3];

our $var1 = 1;
our $var2 = 2;
our $var3 = 3;

1;use MyMod;

print $var2;

Ultimater
08-07-2005, 10:52 PM
A little problem occurs for the module to grab the params...

syntax error at uploadedImages.pm line 9, near "param "field_15""
syntax error at uploadedImages.pm line 10, near "param "field_16""
Compilation failed in require at cgi-bin/ultimater/business_submit.pl line 21.
BEGIN failed--compilation aborted at /cgi-bin/ultimater/business_submit.pl line 21.

uploadedImages.pm

package uploadedImages;

use base Exporter;
our @EXPORT = qw[$megaByte $upload1_name $upload2_name $upload1 $upload2 $suffix1 $suffix2];

our $megaByte=1073741824;
our $upload1_name="";
our $upload2_name="";
our $upload1 = param "field_15";
our $upload2 = param "field_16";
our $suffix1=getImageExtension($upload1);
our $suffix2=getImageExtension($upload2);
if($suffix1){$upload1_name="p".$userid."n1.".$suffix1;}
if($suffix2){$upload2_name="p".$userid."n2.".$suffix2;}
if($suffix1 ne ""){
if(-s $upload1 > $megaByte){
$upload1="";$upload1_name="";$suffix1="";
}
else{
if (-e ($database_dir.$upload1_name)){unlink($database_dir.$upload1_name);}
open(IMAGEFILE, "> ".$database_dir.$upload1_name) or die "error: opening \$upload1_name!";
print IMAGEFILE <$upload1>;
close(IMAGEFILE);
}
}

if($suffix2 ne ""){
if(-s $upload2 > $megaByte){
$upload2="";$upload2_name="";$suffix2="";
}
else{
if (-e ($database_dir.$upload2_name)){unlink($database_dir.$upload2_name);}
open(IMAGEFILE, "> ".$database_dir.$upload2_name) or die "error: opening \$upload2_name!";
print IMAGEFILE <$upload2>;
close(IMAGEFILE);
}
}


1;

Is there any way that the module can read the params by itself w/o having to declare ours in the main PERL file?

Jeff Mott
08-07-2005, 11:29 PM
A little problem occurs for the module to grab the params...You will have to use the CGI module within your uploadedImages module. At the moment there is no definition for param within its scope.Is there any way that the module can read the params by itself w/o having to declare ours in the main PERL file?Technically yes, but setting a variable for every parameter is dangerous because the user can give your script whatever parameters they want, and your script would thus set any variable the user wants.

Ultimater
08-07-2005, 11:54 PM
FINALLY got it to work!

package uploadedImages;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use File::Basename;
use base Exporter;
our @EXPORT = qw[$megaByte $upload1_name $upload2_name $upload1 $upload2 $suffix1 $suffix2];

my @ext = qw(gif jpeg jpg bmp png);
foreach (@ext) { s/\./\\./; }
sub getImageExtension{
local ($tmpFilePath)=@_;
local ($name1,$path1,$suffix1) = fileparse(lc $tmpFilePath,@ext);
return $suffix1;
}

our $megaByte=1073741824;
our $upload1_name="";
our $upload2_name="";
our $upload1 = param "field_15";
our $upload2 = param "field_16";
our $suffix1=getImageExtension($upload1);
our $suffix2=getImageExtension($upload2);
if($suffix1){$upload1_name="p".$userid."n1.".$suffix1;}
if($suffix2){$upload2_name="p".$userid."n2.".$suffix2;}
if($suffix1 ne ""){
if(-s $upload1 > $megaByte){
$upload1="";$upload1_name="";$suffix1="";
}
else{
if (-e ($database_dir.$upload1_name)){unlink($database_dir.$upload1_name);}
open(IMAGEFILE, "> ".$database_dir.$upload1_name) or die "error: opening \$upload1_name!";
print IMAGEFILE <$upload1>;
close(IMAGEFILE);
}
}

if($suffix2 ne ""){
if(-s $upload2 > $megaByte){
$upload2="";$upload2_name="";$suffix2="";
}
else{
if (-e ($database_dir.$upload2_name)){unlink($database_dir.$upload2_name);}
open(IMAGEFILE, "> ".$database_dir.$upload2_name) or die "error: opening \$upload2_name!";
print IMAGEFILE <$upload2>;
close(IMAGEFILE);
}
}


1;


I even even had to redeclare:

my @ext = qw(gif jpeg jpg bmp png);
foreach (@ext) { s/\./\\./; }
sub getImageExtension{
local ($tmpFilePath)=@_;
local ($name1,$path1,$suffix1) = fileparse(lc $tmpFilePath,@ext);
return $suffix1;
}


I Couldn't just simply use the uploadStuff module again. But it worked and that was what I was aiming at!

Jeff Mott! Thank You for the help! You were of excellent assistance, keep up the good work!

Nedals
08-08-2005, 02:34 AM
Something to think about. :)
Gets rid of a ton of variables.

my $template = HTML::Template->new(filename => '../../Business/template_business_newentry.htm');
$template->param(
submitLink => "../../cgi-bin/ultimater/testing123.pl",
backLink => "javascript:history.go(-1)",
pic1 => $pic1,
pic2 => $pic2,
busName => unescape($data[1]),
busInfo => unescape($data[2]),
website => unescape($data[3]),
street => unescape($data[4]),
city => unescape($data[5]),
zipcode => unescape($data[6]),
state => unescape($data[7]),
phoneNumber => unescape($data[8]),
faxNumber => unescape($data[9]),
email => unescape($data[10]),
link1 => unescape($data[11]),
link2 => unescape($data[12]),
);

Ultimater
09-20-2005, 11:24 AM
edit: wrong thread.