Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Carriage Return and LineFeeds Question
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


Report this thread to moderator Post Follow-up to this message
Old Post
Dave Adams
10-21-05 02:55 AM


Re: Carriage Return and LineFeeds Question
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 i
t
> 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


Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
10-21-05 02:55 AM


Re: Carriage Return and LineFeeds Question
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 i
t
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff 'japhy' Pinyan
10-21-05 02:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:26 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.