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: Intermediate: Re: [demon01] send to a db and then to a script: Edit Log



7stud
Enthusiast

Mar 25, 2010, 4:22 AM


Views: 3944
Re: [demon01] send to a db and then to a script

cgi script:


Code
#!/usr/bin/env perl 
use strict;
use warnings;
use 5.010;

use CGI ;

use CGI::Carp qw{fatalsToBrowser warningsToBrowser};
#now *fatal* errors will automatically appear in the browser,
#which is useful for debugging

use DBI;


my $query = new CGI; #for retrieving form data
my $college = $query->param("college");

my $data_source = 'DBI:mysql:test';
my $user = 'root';
my $pass = '';

#dbh stands for 'db handle'
my $dbh = DBI->connect(
$data_source,
$user,
$pass,
{RaiseError => 1, AutoCommit => 0}, #1=>true, 0=>false
);

#sth stands for 'statement handle'
my $sth = $dbh->prepare(
"INSERT INTO college_counts(college, count) VALUES (?,?)"
);

$sth->execute($college, 1);
$dbh->commit(); #Because AutoCommit is false above

$dbh->disconnect();


#Open a pipe to another script. Anything you
#write to the filehandle will go to the other script's STDIN.

open my $PIPE_WRITER, '| ../helper_programs/myprog.pl'
or die "Couldn't open pipe to myprog.pl: $!";

say $PIPE_WRITER $college;
close $PIPE_WRITER;


my $query = new CGI; #for output back to browser

print $query->header;
warningsToBrowser(1);
#Inserts warnings as html comments in the html source.
#Can only be called after printing a header.

print
$query->start_html,
$query->div("Your college choice, $college, was entered into our db."),
$query->div("$college was also sent to another script"),
$query->end_html
;


Another script:


Code
#!/usr/bin/env perl 

use strict;
use warnings;
use 5.010;

#On my server, the relative path from cgi-bin to here is:
#../helper_programs/myprog.pl
#The path will most likely be different on your server.


#Open a file and write the received data to the file to prove
#that this program received the data:

open my $OUTFILE, '>', '../writables/data1.txt'
or die "Couldn't open file data1.txt: $!";

while (<STDIN>) { #this program expects line oriented input, i.e. lines that
#end with "\n". When the other program closes the pipe
#this while() loop gets an eof signal

print $OUTFILE $_;

#do whatever needs to be done with the data
}

close $OUTFILE;


Make sure both scripts have read+execute permissions, and proper shebang lines at the top.


(This post was edited by 7stud on Mar 25, 2010, 5:07 AM)


Edit Log:
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:22 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:24 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:29 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:29 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:32 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:33 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:35 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:36 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:52 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:53 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:55 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:57 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:57 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 4:59 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 5:01 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 5:03 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 5:06 AM
Post edited by 7stud (Enthusiast) on Mar 25, 2010, 5:07 AM


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

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