[28077] in Perl-Users-Digest
Perl-Users Digest, Issue: 9441 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jul 9 21:10:12 2006
Date: Sun, 9 Jul 2006 18:10:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 9 Jul 2006 Volume: 10 Number: 9441
Today's topics:
Re: Stupid Q: Read complete file into variable or strin <penryu@saiyix.ath.cx>
Re: Stupid Q: Read complete file into variable or strin <franzl.wisseworst@mailinator.net>
Re: Stupid Q: Read complete file into variable or strin <sherm@Sherm-Pendleys-Computer.local>
Re: Stupid Q: Read complete file into variable or strin <DJStunks@gmail.com>
Re: Stupid Q: Read complete file into variable or strin <tadmc@augustmail.com>
Re: Stupid Q: Read complete file into variable or strin <rvtol+news@isolution.nl>
Re: Stupid Q: Read complete file into variable or strin <franzl.wisseworst@mailinator.net>
Re: Stupid Q: Read complete file into variable or strin <franzl.wisseworst@mailinator.net>
Re: Stupid Q: Read complete file into variable or strin <franzl.wisseworst@mailinator.net>
Re: Stupid Q: Read complete file into variable or strin <DJStunks@gmail.com>
Re: Stupid Q: Read complete file into variable or strin <franzl.wisseworst@mailinator.net>
Re: Stupid Q: Read complete file into variable or strin <rvtol+news@isolution.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 09 Jul 2006 15:58:39 GMT
From: Tim Hammerquist <penryu@saiyix.ath.cx>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <slrneb29tf.l9o.penryu@ruri.saiyix>
Franzl Wisseworst <franzl.wisseworst@mailinator.net> wrote:
> Subject: Re: Stupid Q: Read complete file into variable or string
>
> How can I store the contents of a complete file in a variable for
> some other use? My failed attempt is as follows:
>
> open (BLA,"bla.txt");
> $myfile = <BLA>;
> close(BLA);
Not a stupid question, but certainly a frequently asked one. Hence,
the answer is in the perlfaq? docs.
perlfaq5 "How can I read in an entire file all at once?"
also see
perlfaq3 "How can I make my Perl program take less memory?"
The answer to the former has already been posted in this thread.
> If I try to print it for example only the first line is displayed,
> as I'm sure everyone here would be aware of:
>
> print $myfile;
>
> The file itself however contains several lines of text. How exactly
> can I read in the complete file into the string or variable?
Your sample code implies you've seen Perl code before, but don't know
what it does just yet. I've had good luck with both "Learning Perl"
(Schwartz et al.; pub O'Reilly) and "Elements of Programming with
Perl" (Johnson; pub Manning), though both my copies are a few years
old by now.
HTH,
Tim Hammerquist
------------------------------
Date: Sun, 9 Jul 2006 18:14:08 +0200
From: Franzl Wisseworst <franzl.wisseworst@mailinator.net>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <e8ravq$3dc$00$1@news.t-online.com>
Thanks for the various replies. Below is my initial crap code.
Its simply a cgi that when accessed via the web shows the contents of
"bla.txt" on a page inset in a form textarea, which when
modified/resubmitted, is updated.
The " page display" and "form edit" fills the same functionality, which I
guess is unlike what is typically done on the web, whereby separate edit
and display pages usually exist.
Only problem is when reloading or accessing the page, without form
parameters when otherwise hitting submit, a whitespace is insterted before
each line of the textarea. It occurs after alternating reload and edit
actions, with an effect such as for example:
<textarea>
Line 1
Line 2
Line3
</textarea>
I guess it has something to do with how "my @lines" is read into the
textarea. Can anyone enlighten me on the exact cause and solution?
I only use CGI.pm as its such a small script. No non-standard modules.
-------- start of crap code ------------
use CGI qw (:standard);
print header;
$mytext = param('mytext');
$myfile = "bla.txt";
open (BLA,$myfile);
my @lines = <BLA>;
close(BLA);
print start_form, 'notes',br,
textarea('mytext',"@lines"),br,
submit, end_form;
# If script is accessed without submit action, i.e.: direct URL
# access/reload of page, do not write to file.
# This is detected by the empty form parameter (a better way must exist!?).
# Otherwise, write whatever input was entered (or pre-entered) in textarea
# into bla.txt.
if ($mytext ne ""){
open(BLA, ">$myfile");
print BLA $mytext;
close(BLA);
}
------------ end of crap code ------------
Any quick tips how to improve, expand and correct otherwise crap code
would be much appreciated.
The purpose is simply a quick notes utility for myself, but possibly also
to be used by more than one person, in which case file locking would
somehow need to be added to prevent overwriting simultaneous edit actions.
------------------------------
Date: Sun, 09 Jul 2006 12:37:31 -0400
From: Sherm Pendley <sherm@Sherm-Pendleys-Computer.local>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <m2hd1qlook.fsf@Sherm-Pendleys-Computer.local>
Franzl Wisseworst <franzl.wisseworst@mailinator.net> writes:
> A. Sinan Unur wrote:
>
> [..]
>
>> http://search.cpan.org/~uri/File-Slurp-9999.12/lib/File/Slurp.pm
>
> Many thanks for this tasty looking module!
>
>> PS: Setting X-No-Archive means fewer people will see your posts.
>
> And less spam, or flames....
Wishful thinking. Spammers are dishonest to begin with, and they work from
live news feeds, not archives - in other words, they'll simply ignore that
flag and harvest your address anyway.
The only people who will be affected by the X-No-Archive header are the folks
who search Google Groups archives a year for now, looking for answers to the
same (or similar) questions as you asked.
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: 9 Jul 2006 10:15:38 -0700
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <1152465338.726340.118510@m73g2000cwd.googlegroups.com>
Franzl Wisseworst wrote:
> $myfile = "bla.txt";
>
> open (BLA,$myfile);
> my @lines = <BLA>;
> close(BLA);
>
> print start_form, 'notes',br,
> textarea('mytext',"@lines"),br,
> submit, end_form;
> Only problem is when reloading or accessing the page, without form
> parameters when otherwise hitting submit, a whitespace is insterted before
> each line of the textarea.
> <snip>
> I guess it has something to do with how "my @lines" is read into the
> textarea. Can anyone enlighten me on the exact cause and solution?
>From perldoc perlvar:
$LIST_SEPARATOR
$"
This is like $, except that it applies to array and slice values
interpolated into a double-quoted string (or similar interpreted
string). Default is a space.
So that's why you're getting spaces. You could alter that built-in
variable, or you could change your slurp to slurp into a scalar instead
of an array. Though a module was suggested earlier (File::Slurp) you
can do it using pure Perl syntax as outlined in Uri's article which was
also pointed out to you.
-jp
------------------------------
Date: Sun, 9 Jul 2006 12:22:09 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <slrneb2eq1.63r.tadmc@magna.augustmail.com>
Franzl Wisseworst <franzl.wisseworst@mailinator.net> wrote:
> A. Sinan Unur wrote:
>> PS: Setting X-No-Archive means fewer people will see your posts.
>
> And less spam,
How does setting X-No-Archive reduce spam?
> or flames....
How does setting X-No-Archive reduce flames?
Setting X-No-Archive reduces the number of people that will
see your question.
Setting X-No-Archive reduces the value of the thread for the rest
of the communitiy because it is full of "holes".
Spammers and trolls often set X-No-Archive, looking like a spammer
or troll will reduce your readership yet some more.
Setting X-No-Archive is likely to *increase* flames, because you
want to "take" your answer, but don't want to "give" by having
a complete thread for others to refer to when they have the same problem.
Setting X-No-Archive is anti-social.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sun, 9 Jul 2006 20:31:44 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <e8rpbk.1do.1@news.isolution.nl>
Franzl Wisseworst schreef:
> How can I store the contents of a complete file in a variable for some
> other use? My failed attempt is as follows:
>
> open (BLA,"bla.txt");
> $myfile = <BLA>;
> close(BLA);
There are many ways, but you haven't told us the why. One variant that I
find OK for small files:
#!/usr/bin/perl
use strict ;
use warnings ;
my $filename = 'bla.txt' ;
my $filedata ;
open my $fh, '<', $filename
or die "open $filename: $!" ;
{ # slurp mode
local $/ ;
$filedata = <$fh> ;
}
close $fh
or die "close $filename: $!" ;
print $filedata ;
__END__
But what holds you back from processing the file line-by-line?
See also:
http://search.cpan.org/search?module=Text::Balanced
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Sun, 9 Jul 2006 23:39:13 +0200
From: Franzl Wisseworst <franzl.wisseworst@mailinator.net>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <e8rtv2$349$03$1@news.t-online.com>
Sherm Pendley wrote:
[..]
> answers to the same (or similar) questions as you asked.
You are totally right. Point taken and acted upon!
------------------------------
Date: Sun, 9 Jul 2006 23:51:02 +0200
From: Franzl Wisseworst <franzl.wisseworst@mailinator.net>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <e8rul8$1tl$02$1@news.t-online.com>
DJ Stunks wrote:
[...]
> also pointed out to you.
Thank you, I did glance over the various documentations of course, but only
with a fraction of understanding... At least now I know where to look.
------------------------------
Date: Sun, 9 Jul 2006 23:53:37 +0200
From: Franzl Wisseworst <franzl.wisseworst@mailinator.net>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <e8ruq2$1tl$02$2@news.t-online.com>
Dr.Ruud wrote:
[...]
> But what holds you back from processing the file line-by-line?
Nothing except my lack of know-how. I did get the general impression this
would be the better methology however, and will take your advise. Thank you.
------------------------------
Date: 9 Jul 2006 15:31:15 -0700
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <1152484275.590556.114910@p79g2000cwp.googlegroups.com>
Franzl Wisseworst wrote:
> Dr.Ruud wrote:
>
> [...]
>
> > But what holds you back from processing the file line-by-line?
>
> Nothing except my lack of know-how. I did get the general impression this
> would be the better methology however, and will take your advise. Thank you.
I think your case is a good use of slurp; you need the complete
contents of the file, and your file is relatively small. All I
suggested was slurping into a scalar rather than into an array.
Line by line processing is generally "better" if you are, in fact,
processing the lines. Simply concatenating strings together, line by
line, is not as efficient as a slurp.
Anyway, it sounds like you're there. Enjoy.
-jp
------------------------------
Date: Mon, 10 Jul 2006 00:32:41 +0200
From: Franzl Wisseworst <franzl.wisseworst@mailinator.net>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <e8s13b$n6l$00$1@news.t-online.com>
DJ Stunks wrote:
[..]
> line, is not as efficient as a slurp.
Yes, I generally understood from articles that line-by-line may be slower.
> Anyway, it sounds like you're there. Enjoy.
Thanks for the words of encouragement! I shall try the slurpy route too.
------------------------------
Date: Mon, 10 Jul 2006 01:35:57 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Stupid Q: Read complete file into variable or string
Message-Id: <e8sb1k.1c4.1@news.isolution.nl>
Franzl Wisseworst schreef:
> I generally understood from articles that line-by-line may be
> slower.
Decide on what you need to do per line. If what you need to do is easier
on the whole file at once, then slurp. See also Tie::File.
Don't assume there is a disk-io for every line: normally data is read
from disk in big chunks (by the gnomes).
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9441
***************************************