CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
parseinput()

 



tucats
Deleted

Jan 30, 2000, 6:04 PM

Post #1 of 5 (741 views)
parseinput() Can't Post

I need to know how to pass variables to this script...

the script:


#!/usr/local/bin/perl



$data_dir = "/home/kidz4day/kidz4dayz-www/cgi-bin/counter/";

$show_date = "1";

$show_link = "0";

$auto_create = "1";

$lock_sec = "3";

$pad_size = "1";


print "Content-type: text/html\n\n";

$count_page = "$ENV{'DOCUMENT_URI'}";


if ($count_page =~ /\/$/) {
chop($count_page);
}

$count_page =~ s/[^\w]/_/g;
$lock_file = "$count_page\.lock";

&check_lock($lock_sec);

if (-e "$data_dir$count_page") {
open(COUNT,"$data_dir$count_page");
$line = <COUNT>;
chop($line) if $line =~ /\n$/;
close(COUNT);
($date,$count) = split(/\|\|/,$line);
}
elsif ($auto_create == 1) {
&create;
}
else {
&error('page_not_found');
}

$count++;
$print_count = $count;

$count_length = length($count);

for ($i = $pad_size;$i > $count_length;$i--) {
$print_count = "0$print_count";
}


if ($show_date == 1) {
if ($show_link =~ /http:\/\//) {
print "<a href=\"$show_link\">$print_count</a> visitors since $date";
}
else {
print "$print_count visitors since $date";
}
}
else {
if ($show_link =~ /http:\/\//) {
print "<a href=\"$show_link\">$print_count</a>";
}
else {
print "$print_count";
}
}

open(COUNT,">$data_dir$count_page") &#0124; &#0124; &error('could_not_increment');
print COUNT "$date\|\|$count";
close(COUNT);

&clean_up;


sub create {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
@months = ("January","February","March","April","May","June","July",
"August","September","October","November","December");
$year += 1900;
$date = "$months[$mon] $mday, $year";
$count = "0";
open(COUNT,">$data_dir$count_page") &#0124; &#0124; &error('count_not_created');
print COUNT "$date\|\|$count";
close(COUNT);
}

sub error {
$error = shift(@_);

if ($error eq 'page_not_found') {
print "[TextCounter Fatal Error: This Page Not Found\; Auto-Create Option Disabled]";
}
elsif ($error eq 'bad_uri') {
print "[TextCounter Fatal Error: This Page Not In Valid URI]";
}
elsif ($error eq 'count_not_created') {
print "[TextCounter Fatal Error: Could Not Write to File $datadir$count_page]";
}
elsif ($error eq 'could_not_increment') {
print "[TextCounter Fatal Error: Could Not Increment Counter]";
}
exit;
}

sub check_lock {
$time = $_[0];

for ($i = 1;$i <= $time; $i++) {
if (-e "$data_dir$lock_file") {
sleep 1;
}
else {
open(LOCK,">$data_dir$lock_file");
print LOCK "0";
close(LOCK);
last;
}
}
}

sub clean_up {
unlink("$data_dir$lock_file");
}

##### ok, now i need to be able to send values for the variables...:

$show_date = "1";

$show_link = "0";

$auto_create = "1";

$lock_sec = "3";

$pad_size = "1";

from a query... i was shown this subrouting...:

sub parseInput()
{

if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
}

elsif ($ENV{'REQUEST_METHOD'} eq "GET" )
{
$temp = $ENV{'QUERY_STRING'};
}

@pairs=split(/&/,$temp);

foreach $item(@pairs)
{
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}

}


and calling the script by:

&parseInput;

he said to put this before i tried to get at any of the variables...

I have tried all kinds of things... can you show me how to pass variable values through a query...

example...:

<!--#exec cgi="/cgi-bin/counter.cgi?show_date=1&pad_size=5"-->

thank you so much for your time...


brian.hayes
User

Jan 31, 2000, 4:29 AM

Post #2 of 5 (741 views)
Re: parseinput() [In reply to] Can't Post

Your last line looks wrong:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$fields{$key}=$content;
</pre><HR></BLOCKQUOTE>
Shoud be:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%fields{$key}=$content;
</pre><HR></BLOCKQUOTE>
Then refrence it:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


&parseinput();

if ($fields{'show_date'} = 1){

do something....

}

</pre><HR></BLOCKQUOTE>

Try this. You also may want to try CGI.pm

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>



use CGI;
my $q = new CGI;

my $show_date = $q->param('show_date');

if ($show_date == 1) {

do something....
}

</pre><HR></BLOCKQUOTE>


Let us know ..

Brian Hayes.


ljfaraci
Deleted

Jan 31, 2000, 8:18 AM

Post #3 of 5 (741 views)
Re: parseinput() [In reply to] Can't Post

If I understand your question,
you want to pass arguments to your script
from the unix command line.

ie: unix> perl my_program.pl arg0 arg1 arg2 ... argn

where arg0 arg1 arg2 ... argn are the data items you want to
pass in.

If so, you will need to set up the script
somewhat like this:


#!/usr/local/bin/perl

my @arglist = @_; # this puts the input arguments in a list
# then you can process the list
# in the body of the script

$data_dir = "/home/kidz4day/kidz4dayz-www/cgi-bin/counter/";

# the rest of the script...

Hope this helps... LJF...


tucats
Deleted

Jan 31, 2000, 3:27 PM

Post #4 of 5 (741 views)
Re: parseinput() [In reply to] Can't Post

i did figure it out... once i parse the values... (inport them from my SSI query) i name the variables:

#!/usr/local/bin/perl

sub parseInput()
{
my( $temp, $item );
read(STDIN, $temp, $ENV{'CONTENT_LENGTH'})

if( $ENV{'REQUEST_METHOD'} eq "POST" );
$temp = $ENV{'QUERY_STRING'}

if( $ENV{'REQUEST_METHOD'} eq "GET" );
@pairs=split(/&/,$temp);
for $item(@pairs){
my($key,$content) = split( /=/, $item, 2 );
$content =~ tr/+/ /;
$content =~ s/%(..)/pack("c",hex($1))/ge;

$fields{$key}=$content;
}
}

&parseInput;


$data_dir = "/home/kidz4day/kidz4dayz-www/cgi-bin/counter/";

$show_date = $fields{ 'show_date' } or $show_date = 0;
$show_link = $fields{ 'show_link' } or $show_link = 0;
$pad_size = $fields{ 'pad_size' } or $pad_size = 1;
$auto_create = 1;
$lock_sec = 3;


print "Content-type: text/html\n\n";

##### blah blah blah... the rest of the script...

thanks for your time guys...


japhy
Enthusiast

Jan 31, 2000, 4:43 PM

Post #5 of 5 (741 views)
Re: parseinput() [In reply to] Can't Post

Uh, just need to clarify a few points.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$hash{$key} = $value; # correct
%hash{$key} = $value; # incorrect

@command_line_args = @ARGV; # correct
@command_line_args = @_; # incorrect
</pre><HR></BLOCKQUOTE>

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives