Stupid Q: Read complete file into variable or string

  • Thread starter Franzl Wisseworst
  • Start date
F

Franzl Wisseworst

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);

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?
 
F

Franzl Wisseworst

ks wrote:

[..]
it depends on how you want to use it later.

Many thanks for the answer to my trivial question.

The idea is just to include the contents of the file in a form textarea,
generated through CGI.pm:

textarea('text',$myfile)

I guess the while loop is a good way to go as the script won't know how
many lines there are.
 
A

A. Sinan Unur

G

goho

Franzl said:
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);

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?

Read the article from Uri Guttman at
http://www.perl.com/pub/a/2003/11/21/slurp.html
- it may now be a few years old, but its still good.
 
F

Franzl Wisseworst

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

Sherm Pendley

Franzl Wisseworst said:
A. Sinan Unur wrote:

[..]

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--
 
D

DJ Stunks

Franzl said:
$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
 
T

Tad McClellan

Franzl Wisseworst said:
A. Sinan Unur wrote:


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

Dr.Ruud

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
 
F

Franzl Wisseworst

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

Franzl Wisseworst

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

DJ Stunks

Franzl said:
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
 
F

Franzl Wisseworst

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

Dr.Ruud

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top