On Oct 20, Dave Adams said:
I am having troubles understanding how perl sees carriage returns and line
feeds. I am not sure if it is a windows and unix thing.
*Scenario:* I have a HTML form that uploads a text file (via a POST) to a
unix server who then passes it to a perl cgi. This cgi parses through the
file, puts it in an array and if it encounters a paragraph break, I want it
to insert "^P".
By "paragraph break", do you mean where a person has pressed enter two or
more times? There's no special character for that. It's just a
consecutive sequence of newlines.
This is my first paragraph.
This is my Second paragraph.
#!/usr/bin/perl -w
use CGI qw(:standard);
use IO::Handle;
use File::Temp qw /tempfile /;
use Text::Wrap;
my $cgiobject = new CGI;
my $upload_dir = "/temp/";
my $filename_txt = $cgiobject->param("UPLOADFILE");
$filename_txt =~ s/.*[\/\\](.*)/$1/;
open (UPLOADFILE, ">$upload_dir/$filename_txt");
binmode UPLOADFILE;
while ( <$upload_filehandle_txt> )
Where'd $upload_filehandle_txt come from?
{
print UPLOADFILE;
}
close UPLOADFILE;
open(UPLOADFILE, "$upload_dir$filename_txt") || die "problem: $!";
You're missing a / between $upload_dir and $filename_txt. Oh, wait, no
you're not, because you've put a '/' at the end of $upload_dir. That's
bound to lead to confusion. I would suggest NOT putting /'s at the end of
directory names when storing them in variables. "$dir/$file" looks much
cleaner than "$dir$file".
my @textfile_contents = <UPLOADFILE>;
close UPLOADFILE;
Why didn't you just create this array as you were reading from the
uploaded file?
##When perl reads in the scalar to push it on to my array, how do I
##get it to replace the paragraph breaks with ^P?
Do you mean "I want to insert a '^P' where there are two or more newlines
in a row"? If so, Perl has a convenient short cut for you.
open UPLOADFILE, "> $upload_dir/$filename_txt"
or die "cannot write to $upload_dir/$filename_txt: $!";
binmode UPLOADFILE;
{
# setting $/ to "" (the empty string) means that Perl will read
# a group of lines at a time, ending when it reaches a sequence of
# newlines. this is called "paragraph" mode.
local $/ = "";
while (<$upload_filehandle_txt>) {
# we remove any trailing newlines from the end of the paragraph
# and record if there were any in the $newlines variable
my $newlines = chomp;
print UPLOADFILE "$_\n";
print UPLOADFILE "^P\n" if $newlines;
}
}
close UPLOADFILE;
As an example, here's what the contents of the file being uploaded are:
==========
This is a small
paragraph. It is
not much to talk
about.
Here is a "long" paragraph, but I only mean that the
length of its lines are considerably longer.
And here
is the last
paragraph
which I have
made taller
than all the
other ones.
Remember when
paragraphs were
more than two
sentences long?
==========
The resulting file on your machine should look like this:
==========
This is a small
paragraph. It is
not much to talk
about.
^P
Here is a "long" paragraph, but I only mean that the
length of its lines are considerably longer.
^P
And here
is the last
paragraph
which I have
made taller
than all the
other ones.
Remember when
paragraphs were
more than two
sentences long?
==========
If this is not what you meant, then please be clearer.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>