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:
Create and amend tab delimited .txt from Perl

 



Alpha1980
Novice

Feb 19, 2007, 2:48 PM

Post #1 of 9 (1674 views)
Create and amend tab delimited .txt from Perl Can't Post

If I have a web form that submits the following variables to Perl CGI:


Code
$title 
$surname
$firstname


I want to:

1.) Create a .txt file with the name $surname*datestamp*.txt
2.) Append that .txt file with tab delimited data as follows:


Code
Title Surname First Name 
$title $surname $firstname


3.) Send it to an email address

Is this a lot to ask?

Thanks in advance!


KevinR
Veteran


Feb 19, 2007, 3:14 PM

Post #2 of 9 (1673 views)
Re: [Alpha1980] Create and amend tab delimited .txt from Perl [In reply to] Can't Post

This is not a script writing service, it's a help forum. What have you tried?
-------------------------------------------------


Alpha1980
Novice

Feb 19, 2007, 3:49 PM

Post #3 of 9 (1672 views)
Re: [KevinR] Create and amend tab delimited .txt from Perl [In reply to] Can't Post

Sorry Kevin.

I haven't got as far as the timestamp or email yet.

Code
#!/usr/bin/perl 

use CGI;

$query = new CGI;

@names = $query->param;

$title = $query->param(title);
$surname = $query->param(surname);
$firstname = $query->param(firstname);

open(TEXT,+<$surname.txt.);
printf TEXT "Title"\t"Surname"\t"First Name"\n;
printf TEXT ($title)\t($surname)\t($firstname)\n;
close (TEXT);



PGScooter
stranger

Feb 19, 2007, 4:34 PM

Post #4 of 9 (1668 views)
Re: [Alpha1980] Create and amend tab delimited .txt from Perl [In reply to] Can't Post

Hi Alpha 1980,

first of all, I don't know why you do that to append. Perhaps that works; I'm not sure. I would do it like this: open(INFILE,">>whatever.txt") or die "can't open the txt file";

as for the timestamp, look into (look into meaning look in a book or do a google search) the localtime function.

finally, for the email, I would suggest looking into "sendmail". I'm not sure, however, how you can send files through an email; I've never tried it.

Also I'm curious, what does this part of your code do?

Code
@names = $query->param;


Try to write as much code as you can for those things (I'm guessing you should spend at least 3 hours reading and experimenting with code), post what you can do, and I'll see if I can help you some more after that.
The more you teach me, the more I learn. The more I learn, the more I teach.


KevinR
Veteran


Feb 19, 2007, 5:21 PM

Post #5 of 9 (1666 views)
Re: [Alpha1980] Create and amend tab delimited .txt from Perl [In reply to] Can't Post


Code
#!/usr/bin/perl  
use strict;
use warnings;
use CGI;

my $query = new CGI;

my $title = $query->param('title');
my $surname = $query->param('surname');
my $firstname = $query->param('firstname');

open(TEXT,">>$surname".time. '.txt') or die "$!";
print TEXT "Title\tSurname\tFirst Name\n";
print TEXT "($title)\t($surname)\t($firstname)\n";
close (TEXT);

send to email stuff here


if you get stuck on the emailing part post more questions.
-------------------------------------------------


Alpha1980
Novice

Feb 24, 2007, 6:12 PM

Post #6 of 9 (1627 views)
Re: [KevinR] Create and amend tab delimited .txt from Perl [In reply to] Can't Post

Thanks Scooter and Kevin. I'd never get anywhere without you guys.

Here is where I am at:


Code
#!/usr/bin/perl 

print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

use strict;
use warnings;
use CGI;

my $query = new CGI;

my $title = $query->param('title');
my $surname = $query->param('surname');
my $firstname = $query->param('firstname');

open(TEXT,">>$surname".time. '.txt') or die "$!";
print TEXT "Title\tSurname\tFirst Name\n";
print TEXT "$title\t$surname\t$firstname\n";
close (TEXT);

use MIME::Lite;

# set up email
$to = "standard\@example.co.uk";
$from = "admin\@example.co.uk";
$subject = "Email Sent via Perl";
$message = "This email was sent using Perl.";
$file = "$surname".time. '.txt'";

# send email
email($to, $from, $subject, $message, $file);

# email function
sub email
{
# get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;

# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message
);

# add the attachment
$msg->attach(
Type => "text/plain",
Path => $file,
Filename => $file,
Disposition => "attachment"
);

# send the email
MIME::Lite->send('smtp', 'mail.example.co.uk', Timeout => 60);
$msg->send();
}

I know that the code to create the file works and the code to send the email works but getting them to work together is a problem.

I hope it is something simple.


(This post was edited by Alpha1980 on Feb 24, 2007, 6:13 PM)


KevinR
Veteran


Feb 24, 2007, 7:40 PM

Post #7 of 9 (1622 views)
Re: [Alpha1980] Create and amend tab delimited .txt from Perl [In reply to] Can't Post

first thing to do is declare all your undeclared variables with "my", for example:


Code
my $to = "standard\@example.co.uk";

-------------------------------------------------


Alpha1980
Novice

Feb 25, 2007, 7:59 AM

Post #8 of 9 (1616 views)
Re: [KevinR] Create and amend tab delimited .txt from Perl [In reply to] Can't Post

Thanks once again Kevin. :)

I've got it working with the following code:


Code
#!/usr/bin/perl 

print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

use strict;
use warnings;
use CGI;

my $query = new CGI;

my $title = $query->param('title');
my $surname = $query->param('surname');
my $firstname = $query->param('firstname');

open(TEXT,">>$surname".time. '.txt') or die "$!";
print TEXT "Title\tSurname\tFirst Name\n";
print TEXT "$title\t$surname\t$firstname\n";
close (TEXT);

use MIME::Lite;

# set up email
my $to = "standard\@example.co.uk";
my $from = "admin\@example.co.uk";
my $subject = "$surname".time;
my $message = "This email was sent from Perl";
my $file = "$surname".time.".txt";

# send email
email($to, $from, $subject, $message, $file);

# email function
sub email
{

# create a new message
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message
);

# add the attachment
$msg->attach(
Type => "text/plain",
Path => $file,
Filename => $file,
Disposition => "attachment"
);

# send the email
MIME::Lite->send('smtp', 'mail.example.co.uk', Timeout => 60);
$msg->send();
}


I've taken out this piece of code:


Code
{  
# get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;

because it was returning the error:

Quote
Can't localize lexical variable $to at test.pl

and I wasn't sure what that mean't or what this code even does.

Anyway, there is one final thing I need to do. I have the following script which generates a .pdf containing $title, $surname and $firstname.


Code
#!/usr/bin/perl 

use CGI;

$query = new CGI;

@names = $query->param;

$title = $query->param(title);
$surname = $query->param(surname);
$firstname = $query->param(firstname);

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

print "Content-Disposition: attachment;filename=\"letter.pdf\"\n";
print "Content-type: application/pdf\n\n";

if (! open(PRINCE, "| /usr/bin/prince -"))
{
print "Content-type: text/plain\n\n";
print "Error starting Prince\n";
exit;
}
print "Content-Disposition: attachment;filename=\"letter.pdf\"\n";
print "Content-type: application/pdf\n\n";
$margin="0";


print PRINCE <<"EOF";
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
\@page {
size: letter;
margin: 10% 15%;
}
\@media screen {
body { margin: 3em }
}
div.letter {
text-align: center;
font: bold 17pt "Lega", "Georgia", serif;
margin-bottom: 3em;
}
body { font: 11pt "Gill Sans", "Chantilly", "Georgia", sans-serif }

/* IE6 and older does not support the 'white-space' property. In other
browsers, the line below can be uncommented and those pesky <br/> tags
can be removed */
/* div.letter { white-space: pre } */
</style>
</head>

<body>
<div class="letter">
$title<br/>
$surname<br/>
$firstname<br/>
</div>
</body>
</html>
EOF

close PRINCE;

I want to add three radio inputs to the webform that produces $title, $surname and $firstname.

The first radio should create the .txt file and send the email.
The second radio should create the .pdf.
And the third radio should do both.

I'd really appreciate a push in the right direction because I don't know where to start with this one.

Many Thanks :)


davorg
Thaumaturge / Moderator

Mar 1, 2007, 5:34 AM

Post #9 of 9 (1592 views)
Re: [Alpha1980] Create and amend tab delimited .txt from Perl [In reply to] Can't Post

Please add "use strict" and "use warnings" to your program. They are there to stop you shooting yourself in the foot.

Also, if you're using the CGI module, then your life will be easier if you use the "header" function to produce your CGI headers.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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