Link to home
Start Free TrialLog in
Avatar of siceman105
siceman105

asked on

Easy modification to script....I HOPE!

Hi all,
I have the following script (included at the botton of this message)

What it basically does is when its called it looks for a variable called "id" and opens an associated text file. For example page.cgi?id=hello
would open hello.txt
It prints this out between a header ($header) and footer ($footer)
What I would like to do is is modify the script to accept the details of which header and footer to display.
For example, if the script was called as page.cgi?id=hello&template=red

Then the header red1.txt and the footer red2.txt would be opened.

Can someone please help me modify the script, and maybe tidy it up a little.

Thanks

Siceman

---------
#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------

open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


 ## Print the footer and exit
 open (FOOTER, "$footer");
 while (<FOOTER>) { print; }
 close(FOOTER);
 exit;
}

open (file, "$name.txt");
 while (<file>) { print; }

open (FOOTER, "$footer");
     while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $FORM{$name} = $value;
   }
}


sub cgi_header {
    print "Content-type: text/html\n";
    print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
    if ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
    }
    else {
        $incoming = $ENV{'QUERY_STRING'};
    }
    @pairs = split(/&/, $incoming);

    foreach (@pairs) {
        ($name, $value) = split(/=/, $_);

        $name  =~ tr/+/ /;
        $value =~ tr/+/ /;
        $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
        $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

        #### Strip out semicolons unless for special character
        $value =~ s/;/$$/g;
        $value =~ s/&(\S{1,6})$$/&\1;/g;
        $value =~ s/$$/ /g;

        $value =~ s/\|/ /g;
        $value =~ s/^!/ /g; ## Allow exclamation points in sentences

        #### Skip blank text entry fields
        next if ($value eq "");

        #### Check for "assign-dynamic" field names
        #### Mainly for on-the-fly input names, especially checkboxes
        if ($name =~ /^assign-dynamic/) {
            $name = $value;
            $value = "on";
        }

     #### Allow for multiple values of a single name
        $FORM{$name} .= ", " if ($FORM{$name});

        $FORM{$name} .= $value;
    }
}
Avatar of bebonham
bebonham

why are you in the cgi area and not using cgi.pm :)

really, in all seriousness...it is a standard part of perl, and I would be glad to decomplicate your script by using cgi.pm if that is okay, and I can also add that dynamic headers...

since you already are using lwp, why not use cgi?

Bob
Avatar of siceman105

ASKER

Im not sure how to use cgi.pm

But Im open to it if youd show me what to do etc!

Cheers

Siceman
how can this work ??!?!

$name = $FORM{id}; ??

doesn't it have to be $FORM{'id'} or $FORM{$id} !?!?

I would like an explanation on that one...

anyways, try these changes.

it should work...I can't belive that id thing works...

just make sure you make a field in your html form called template!!





#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2";
$header=$header + "1";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
    while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

  # Get the input
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

  # Split the name-value pairs
  @pairs = split(/&/, $buffer);

  foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);

     $value =~ tr/+/ /;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

     $FORM{$name} = $value;
  }
}


sub cgi_header {
   print "Content-type: text/html\n";
   print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
   if ($ENV{'REQUEST_METHOD'} eq "POST") {
       read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
   }
   else {
       $incoming = $ENV{'QUERY_STRING'};
   }
   @pairs = split(/&/, $incoming);

   foreach (@pairs) {
       ($name, $value) = split(/=/, $_);

       $name  =~ tr/+/ /;
       $value =~ tr/+/ /;
       $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
       $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

       #### Strip out semicolons unless for special character
       $value =~ s/;/$$/g;
       $value =~ s/&(\S{1,6})$$/&\1;/g;
       $value =~ s/$$/ /g;

       $value =~ s/\|/ /g;
       $value =~ s/^!/ /g; ## Allow exclamation points in sentences

       #### Skip blank text entry fields
       next if ($value eq "");

       #### Check for "assign-dynamic" field names
       #### Mainly for on-the-fly input names, especially checkboxes
       if ($name =~ /^assign-dynamic/) {
           $name = $value;
           $value = "on";
       }

    #### Allow for multiple values of a single name
       $FORM{$name} .= ", " if ($FORM{$name});

       $FORM{$name} .= $value;
   }
}



good luck,

Bob
cool...this is in cgi...

because it looks simpler :)


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;
use CGI ':standard';

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2";
$header=$header + "1";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
    while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {
&cgi_receive;
}



sub cgi_header {
 print header unless ($FORM{'next-url'});
}        

sub cgi_receive {

foreach(param())
{
$FORM{$_}=param($_);
}


otherwise, I pretty much like the script...

I like how you have an error routine that include the printing of the header...

I would have done it like this
..this isn't real code...this is just the logic I would have used...

if(error)
{
print (errmsg);
}

if(!error)
{
print (correcttextFile);
}

print footer;
exit;

but you did...

if(error)
{
print (errmsg);
print header;
exit;
}

print (correcttextFile);
print footer;
exit;


I think I like your logic better :)


Bob
hold on here...I will repost...

forgot to add the .txt to the new files...


first, the CGI version:


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;
use CGI ':standard';

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
   while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {
&cgi_receive;
}



sub cgi_header {
print header unless ($FORM{'next-url'});
}        

sub cgi_receive {

foreach(param())
{
$FORM{$_}=param($_);
}





#######################
okay, now for the non-cgi version:







#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
   while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

 # Get the input
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

 # Split the name-value pairs
 @pairs = split(/&/, $buffer);

 foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $FORM{$name} = $value;
 }
}


sub cgi_header {
  print "Content-type: text/html\n";
  print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
  if ($ENV{'REQUEST_METHOD'} eq "POST") {
      read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
  }
  else {
      $incoming = $ENV{'QUERY_STRING'};
  }
  @pairs = split(/&/, $incoming);

  foreach (@pairs) {
      ($name, $value) = split(/=/, $_);

      $name  =~ tr/+/ /;
      $value =~ tr/+/ /;
      $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
      $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

      #### Strip out semicolons unless for special character
      $value =~ s/;/$$/g;
      $value =~ s/&(\S{1,6})$$/&\1;/g;
      $value =~ s/$$/ /g;

      $value =~ s/\|/ /g;
      $value =~ s/^!/ /g; ## Allow exclamation points in sentences

      #### Skip blank text entry fields
      next if ($value eq "");

      #### Check for "assign-dynamic" field names
      #### Mainly for on-the-fly input names, especially checkboxes
      if ($name =~ /^assign-dynamic/) {
          $name = $value;
          $value = "on";
      }

   #### Allow for multiple values of a single name
      $FORM{$name} .= ", " if ($FORM{$name});

      $FORM{$name} .= $value;
  }
}




okay,


there it is, all fixed up.

Bob
So the first one uses CGI.pm? Does that mean it will run faster or something?

Thanks

Siceman
no it actually means it will run slower :)
really!

cgi.pm is one of the biggest (besides lwp and gd) best modules available for perl...(it also takes a few seconds...maybe milliseconds) to load

the code without the cgi.pm will run faster, and be more portable...

of course...cgi.pm is great, and it's nice to have it available to your scripts...I personally always use cgi.pm if I am going to be reading fields from an http post...

but that is a matter of preference...

totally up to you...

maneshr, #1 in both this area and the perl area, would probably suggest you use the NON cgi version.

Bob
Hi,
yeah Manshr helped me develop the first one.

However, the non cgi version you just gave me doesnt seem to work, I get an Error 500 with the following details:

Scalar found where operator expected at /home//temp.cgi line 22, at end of line
     (Missing semicolon on previous line?)
syntax error at /home//temp.cgi line 22, near "$footer"
Execution of /home//temp.cgi aborted due to compilation errors.



Any ideas?

Thanks

Steve
man...
sometimes I wonder about myself :)

I can't even type 2 lines of code without a typo!

this is the problem in BOTH scripts...

$header=$FORM{'template'}


needs to have a ; after it so this is the fixed non-cgi version.



#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'};
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
  while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

   $FORM{$name} = $value;
}
}


sub cgi_header {
 print "Content-type: text/html\n";
 print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
 if ($ENV{'REQUEST_METHOD'} eq "POST") {
     read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
 }
 else {
     $incoming = $ENV{'QUERY_STRING'};
 }
 @pairs = split(/&/, $incoming);

 foreach (@pairs) {
     ($name, $value) = split(/=/, $_);

     $name  =~ tr/+/ /;
     $value =~ tr/+/ /;
     $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
     $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

     #### Strip out semicolons unless for special character
     $value =~ s/;/$$/g;
     $value =~ s/&(\S{1,6})$$/&\1;/g;
     $value =~ s/$$/ /g;

     $value =~ s/\|/ /g;
     $value =~ s/^!/ /g; ## Allow exclamation points in sentences

     #### Skip blank text entry fields
     next if ($value eq "");

     #### Check for "assign-dynamic" field names
     #### Mainly for on-the-fly input names, especially checkboxes
     if ($name =~ /^assign-dynamic/) {
         $name = $value;
         $value = "on";
     }

  #### Allow for multiple values of a single name
     $FORM{$name} .= ", " if ($FORM{$name});

     $FORM{$name} .= $value;
 }
}



okay, hope that works for you,

Bob

Hi,
thanks. Im not getting the error any more, but the header and footer arent appearing as expected. Im wondering if it has something to do with this bit of code

$header=$FORM{'template'};
$footer=$header + "2.txt";
$header=$header + "1.txt";

isnt $header being defined twice?

Thanks

Siceman
doesn't matter, I am reassigning the value...

but... in this case, the value that is being assigned is not existing.

and in this case, it is again an error of my poor debugging...


I am doing this $string + "string";

which is okay in javascript, but in perl, it doesn't work...you have to use .

so...here is the once again corrected code...sorry about the trouble :)


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'};
$footer=$header . "2.txt";
$header=$header . "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
 while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
  ($name, $value) = split(/=/, $pair);

  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

  $FORM{$name} = $value;
}
}


sub cgi_header {
print "Content-type: text/html\n";
print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
if ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
}
else {
    $incoming = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $incoming);

foreach (@pairs) {
    ($name, $value) = split(/=/, $_);

    $name  =~ tr/+/ /;
    $value =~ tr/+/ /;
    $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
    $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

    #### Strip out semicolons unless for special character
    $value =~ s/;/$$/g;
    $value =~ s/&(\S{1,6})$$/&\1;/g;
    $value =~ s/$$/ /g;

    $value =~ s/\|/ /g;
    $value =~ s/^!/ /g; ## Allow exclamation points in sentences

    #### Skip blank text entry fields
    next if ($value eq "");

    #### Check for "assign-dynamic" field names
    #### Mainly for on-the-fly input names, especially checkboxes
    if ($name =~ /^assign-dynamic/) {
        $name = $value;
        $value = "on";
    }

 #### Allow for multiple values of a single name
    $FORM{$name} .= ", " if ($FORM{$name});

    $FORM{$name} .= $value;
}
}


okay, good luck!

Bob
Cheers Bob! It works!

Just one last thing, is there any way that a small bit of code could be added setting a default for the template incase no variable or an invalid one is entered

Siceman
ASKER CERTIFIED SOLUTION
Avatar of bebonham
bebonham

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
did you have a chance to try the above modified script?
take your time, though, if you need to.