PERL create files w/ variable names

July 10, 2008 at 18:44:15
Specs: XP SP1, 2 GHz 256mb
I'm trying to write a PERL script that will create files with a name that depends on input from a user.

In other words, I'm looking for something along the lines of

open(FH, ">$variable.extension");

where the "$variable.extension" is a variable that is replaced with the user submitted file name.


See More: PERL create files w/ variable names

Report •


#1
July 11, 2008 at 07:14:37
This sounds like your homework assignment.

What have you tried?

What error message(s) are you receiving, if any?

What part are you having trouble with, getting the user input or creating the file?


Report •

#2
July 11, 2008 at 13:58:39
A homework assignment in July?...

I don't know why this is, but it seems the computing.net folks are so much more presumptive than the folks on many other boards...

No... This is not a homework assignment.

In the end, all I'm trying to do is create webpages from user input. This might sound easy, but I wrote my very first "Hello World" program in PERL barely on July 3rd, and I have extremely sparse programming experience beside.

I realize there are probably plenty of ready made templates to do this, but I want to do it all my self at least once before I use someone else's code. To date, the only thing I've used that isn't mine is the template for retrieving data via POST from HTML forms.

My idea is: Generate data files from the form inputs, and then use that data to generate HTML pages.

This is all quite preliminary so far.

The idea behind the following script is:

1. Gather input
2. Output data files
3. Rename the data files using the first line of data from the files.

The problem is, when I have multiple lines of text in the forms, it doesn't work for some reason. It outputs files named "pform6dat1.dat" and "pform6dat2.dat" with the form data, but I wanted them to be named with the first line of data from the form, and contain the form data.

I know this is probably cumbersome, but remember I've only been doing this a week.

Here's the code:

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@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;
}
use warnings;
use File::Copy;

{
open(pform6hndl1, ">pform6dat1.dat");
open(pform6hndl2, ">pform6dat2.dat");

select pform6hndl1;
print "$FORM{pform6frm1}";
open (pform6hndl1,"<pform6dat1.dat");
$pform6hndl1op=<pform6hndl1>;

select pform6hndl2;
print "$FORM{pform6frm2}";
open (pform6hndl2,"<pform6dat2.dat");
$pform6hndl2op=<pform6hndl2>;
}

$iosrc1="pform6dat1.dat.";
$iosrc2="pform6dat2.dat.";
$iodest1="$pform6hndl1op.dat";
$iodest2="$pform6hndl2op.dat";

copy($iosrc1,$iodest1);
copy($iosrc2,$iodest2);

select STDOUT;

#the following is printed simply to have an output. The HTML will be used more later.

print "content-type: text/html\n\n";
print "<html>

$pform6hndl1op</p>";
print"

$pform6hndl2op</p></html>";


Report •

#3
July 11, 2008 at 14:03:45
I don't know why the html got so messed up at the bottom, but everything else looks correct.

Also, I realize the { and } surrounding the block after Use::Copy are not required. I put them in as an aid to think about how I wanted the program to run.


Report •

Related Solutions

#4
July 11, 2008 at 16:46:11
Just because there aren't any ongoing classes in your area doesn't mean that's the case elswhere.
Summer session of one college in my area began June 29th
http://www.foothill.fhda.edu/schedu...


I've added some comments to your code which might help.


read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@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;
}

Using that method in the script to process a form submission has been out of date for more than 10 years.
Use the CGI module, it's the de facto standard for Perl cgi scripts.
http://search.cpan.org/~lds/CGI.pm-...


use CGI;
my $cgi = CGI->new;
my %form = $cgi->Vars;


use warnings;
use File::Copy;

You should also use the strict pragma, in every Perl script you write.
use strict;


{
open(pform6hndl1, ">pform6dat1.dat");
open(pform6hndl2, ">pform6dat2.dat");

select pform6hndl1;
print "$FORM{pform6frm1}";
open (pform6hndl1,"<pform6dat1.dat");
$pform6hndl1op=<pform6hndl1>;

select pform6hndl2;
print "$FORM{pform6frm2}";
open (pform6hndl2,"<pform6dat2.dat");
$pform6hndl2op=<pform6hndl2>;
}

Why are you sending the form value to a file and then immediately read it back in? That doesn't make any sense.

Whenever you make an open call, you should check the return code to make sure it succeded and take action if it didn't.

Also, since Perl 5.6 was released (which was about 10 years ago), it's better/prefered to use a lexical var for the filenadle instead of the bareword and to use the 3 arg form of open.


open my $pform6hndl1, '>', $iosrc1
or die "Can't write to '$pform6hndl1' $!";


$iosrc1="pform6dat1.dat.";
$iosrc2="pform6dat2.dat.";

These should be declared earlier and used in the open call


$iodest1="$pform6hndl1op.dat";
$iodest2="$pform6hndl2op.dat";

These assignments aren't needed.


copy($iosrc1,$iodest1);
copy($iosrc2,$iodest2);

select STDOUT;


#the following is printed simply to have an output. The HTML will be used more later.

print "content-type: text/html\n\n";
print "<html>

$pform6hndl1op</p>";
print"

$pform6hndl2op</p></html>";

There's nothing really wrong with using that approach to output the html, but using the CGI module can be cleaner and less error prone.


print $cgi->header,
$cgi->start_html,
$cgi->p($pform6hndl1op),
$cgi->end_html;


Report •

#5
July 11, 2008 at 18:05:28
Thanks so much for all the suggestions. I'll be using this post as a reference for a while, I believe.

The books I've been using are quite outdated. One of them is from 1996, the other is from 2000. I'm too cheap to buy a new one. But after reading your post, maybe it's not such a bad idea!

Thanks again - I'll try to keep you updated on my progress.


Report •

#6
July 11, 2008 at 18:41:31
Yes, you really should pick up at least 1 good book.


C:\>perldoc -q books
Found in C:\Perl\lib\pods\perlfaq2.pod
Perl Books
A number of books on Perl and/or CGI programming are available. A few of
these are good, some are OK, but many aren't worth your money. There is
a list of these books, some with extensive reviews, at
http://books.perl.org/ . If you don't see your book listed here, you can
write to perlfaq-workers@perl.org .

The incontestably definitive reference book on Perl, written by the
creator of Perl, is Programming Perl:

Programming Perl (the "Camel Book"):
by Larry Wall, Tom Christiansen, and Jon Orwant
ISBN 0-596-00027-8 [3rd edition July 2000]
http://www.oreilly.com/catalog/pperl3/
(English, translations to several languages are also available)

The companion volume to the Camel containing thousands of real-world
examples, mini-tutorials, and complete programs is:

The Perl Cookbook (the "Ram Book"):
by Tom Christiansen and Nathan Torkington,
with Foreword by Larry Wall
ISBN 0-596-00313-7 [2nd Edition August 2003]
http://www.oreilly.com/catalog/perl...

If you're already a seasoned programmer, then the Camel Book might
suffice for you to learn Perl. If you're not, check out the Llama book:

Learning Perl
by Randal L. Schwartz, Tom Phoenix, and brian d foy
ISBN 0-596-10105-8 [4th edition July 2005]
http://www.oreilly.com/catalog/lear...

And for more advanced information on writing larger programs, presented
in the same style as the Llama book, continue your education with the
Alpaca book:

Intermediate Perl (the "Alpaca Book")
by Randal L. Schwartz and brian d foy, with Tom Phoenix (foreword by
Damian Conway)
ISBN 0-596-10206-2 [1st edition March 2006]
http://www.oreilly.com/catalog/lrnp...

Addison-Wesley ( http://www.awlonline.com/ ) and Manning (
http://www.manning.com/ ) are also publishers of some fine Perl books
such as *Object Oriented Programming with Perl* by Damian Conway and
*Network Programming with Perl* by Lincoln Stein.

An excellent technical book discounter is Bookpool at
http://www.bookpool.com/ where a 30% discount or more is not unusual.

What follows is a list of the books that the FAQ authors found
personally useful. Your mileage may (but, we hope, probably won't) vary.

Recommended books on (or mostly on) Perl follow.

References
Programming Perl
by Larry Wall, Tom Christiansen, and Jon Orwant
ISBN 0-596-00027-8 [3rd edition July 2000]
http://www.oreilly.com/catalog/pperl3/

Perl 5 Pocket Reference
by Johan Vromans
ISBN 0-596-00032-4 [3rd edition May 2000]
http://www.oreilly.com/catalog/perl...

Tutorials
Beginning Perl
by James Lee
ISBN 1-59059-391-X [2nd edition August 2004]
http://apress.com/book/bookDisplay....

Elements of Programming with Perl
by Andrew L. Johnson
ISBN 1-884777-80-5 [1st edition October 1999]
http://www.manning.com/johnson/

Learning Perl
by Randal L. Schwartz, Tom Phoenix, and brian d foy
ISBN 0-596-10105-8 [4th edition July 2005]
http://www.oreilly.com/catalog/lear...

Intermediate Perl (the "Alpaca Book")
by Randal L. Schwartz and brian d foy, with Tom Phoenix (forewor
d by Damian Conway)
ISBN 0-596-10206-2 [1st edition March 2006]
http://www.oreilly.com/catalog/inte...

Mastering Perl
by brian d foy
ISBN 0-596-52724-1 [1st edition July 2007]
http://www.oreilly.com/catalog/9780...

Task-Oriented
Writing Perl Modules for CPAN
by Sam Tregar
ISBN 1-59059-018-X [1st edition Aug 2002]
http://apress.com/book/bookDisplay....

The Perl Cookbook
by Tom Christiansen and Nathan Torkington
with foreword by Larry Wall
ISBN 1-56592-243-3 [1st edition August 1998]
http://www.oreilly.com/catalog/cook...

Effective Perl Programming
by Joseph Hall
ISBN 0-201-41975-0 [1st edition 1998]
http://www.awl.com/

Real World SQL Server Administration with Perl
by Linchi Shea
ISBN 1-59059-097-X [1st edition July 2003]
http://apress.com/book/bookDisplay....

Special Topics
Perl Best Practices
by Damian Conway
ISBN: 0-596-00173-8 [1st edition July 2005]
http://www.oreilly.com/catalog/perlbp/

Higher Order Perl
by Mark-Jason Dominus
ISBN: 1558607013 [1st edition March 2005]
http://hop.perl.plover.com/

Perl 6 Now: The Core Ideas Illustrated with Perl 5
by Scott Walters
ISBN 1-59059-395-2 [1st edition December 2004]
http://apress.com/book/bookDisplay....

Mastering Regular Expressions
by Jeffrey E. F. Friedl
ISBN 0-596-00289-0 [2nd edition July 2002]
http://www.oreilly.com/catalog/regex2/

Network Programming with Perl
by Lincoln Stein
ISBN 0-201-61571-1 [1st edition 2001]
http://www.awlonline.com/

Object Oriented Perl
Damian Conway
with foreword by Randal L. Schwartz
ISBN 1-884777-79-1 [1st edition August 1999]
http://www.manning.com/conway/

Data Munging with Perl
Dave Cross
ISBN 1-930110-00-6 [1st edition 2001]
http://www.manning.com/cross

Mastering Perl/Tk
by Steve Lidie and Nancy Walsh
ISBN 1-56592-716-8 [1st edition January 2002]
http://www.oreilly.com/catalog/mast...

Extending and Embedding Perl
by Tim Jenness and Simon Cozens
ISBN 1-930110-82-0 [1st edition August 2002]
http://www.manning.com/jenness

Perl Debugger Pocket Reference
by Richard Foley
ISBN 0-596-00503-2 [1st edition January 2004]
http://www.oreilly.com/catalog/perl...

Pro Perl Debugging
by Richard Foley with Andy Lester
ISBN 1-59059-454-1 [1st edition July 2005]
http://www.apress.com/book/view/159...



Report •


Ask Question