For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2005 > Carriage Return and LineFeeds Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Carriage Return and LineFeeds Question
Dave Adams

2005-10-20, 9:55 pm

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".


*My Sample text file:
*
This is my first paragraph.

This is my Second paragraph.


*My code:*
#!/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> )
{
print UPLOADFILE;
}
close UPLOADFILE;

open(UPLOADFILE, "$upload_dir$filename_txt") || die "problem: $!";
my @textfile_contents = <UPLOADFILE>;
close UPLOADFILE;


######Here is where my problem lies################
##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?

my @body = ();
for (my $i=0; $i<=$#textfile_contents; $i++) {
push(@body, $textfile_contents[$i]);
}
Thanks again for all the contributors on the list.
Dave Adams

Paul Lalli

2005-10-20, 9:55 pm

Dave Adams wrote:
> 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.


If I recall correctly, for a perl compiled on unix, "\n" means a
linefeed ("\012") character. For a perl compiled on windows, "\n"
means a carriage return + linefeed sequence ("\015\012"). I'm not sure
if that's what you're asking, however.

> *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".


You need to define the terms you're using. What is a "paragraph
break"? There is no such actual character, so you need to tell us what
you mean by it.

I will assume for the remainder of this message that by "paragraph
break", you mean a sequence of two newlines.

In that case, perhaps the following example will help get you started:
#!/usr/bin/perl
use strict;
use warnings;

local $/ = "\n\n";
while (my $line = <DATA> ){
chomp $line;
print "$line^P";
}

__DATA__
This is paragraph 1

here is the second paragraph, which
contains multiple lines.

And here is the end.

Output:
This is paragraph 1^Phere is the second paragraph, which
contains multiple lines.^PAnd here is the end.^P


Take a look at $/ in
perldoc perlvar
for more information

Paul Lalli

Jeff 'japhy' Pinyan

2005-10-20, 9:55 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com