Stupid Q: Read complete file into variable or string

Discussion in 'Perl Misc' started by Franzl Wisseworst, Jul 9, 2006.

  1. 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?
     
    Franzl Wisseworst, Jul 9, 2006
    #1
    1. Advertising

  2. 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.
     
    Franzl Wisseworst, Jul 9, 2006
    #2
    1. Advertising

  3. Franzl Wisseworst, Jul 9, 2006
    #3
  4. Franzl Wisseworst <> wrote in
    news:e8qrgh$6dh$02$-online.com:

    > How can I store the contents of a complete file in a variable for some
    > other use? My failed attempt is as follows:


    http://search.cpan.org/~uri/File-Slurp-9999.12/lib/File/Slurp.pm

    Sinan

    PS: Setting X-No-Archive means fewer people will see your posts.

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jul 9, 2006
    #4
  5. Franzl Wisseworst

    goho Guest

    Franzl Wisseworst wrote:
    > 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.
     
    goho, Jul 9, 2006
    #5
  6. 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.
     
    Franzl Wisseworst, Jul 9, 2006
    #6
  7. Franzl Wisseworst <> 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
     
    Sherm Pendley, Jul 9, 2006
    #7
  8. Franzl Wisseworst

    DJ Stunks Guest

    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
     
    DJ Stunks, Jul 9, 2006
    #8
  9. Franzl Wisseworst <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 9, 2006
    #9
  10. Franzl Wisseworst

    Dr.Ruud Guest

    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."
     
    Dr.Ruud, Jul 9, 2006
    #10
  11. Sherm Pendley wrote:

    [..]

    > answers to the same (or similar) questions as you asked.


    You are totally right. Point taken and acted upon!
     
    Franzl Wisseworst, Jul 9, 2006
    #11
  12. 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.
     
    Franzl Wisseworst, Jul 9, 2006
    #12
  13. 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.
     
    Franzl Wisseworst, Jul 9, 2006
    #13
  14. Franzl Wisseworst

    DJ Stunks Guest

    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
     
    DJ Stunks, Jul 9, 2006
    #14
  15. 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.
     
    Franzl Wisseworst, Jul 9, 2006
    #15
  16. Franzl Wisseworst

    Dr.Ruud Guest

    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."
     
    Dr.Ruud, Jul 10, 2006
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    3
    Views:
    10,874
    Hans Kesting
    Feb 23, 2006
  2. mike
    Replies:
    3
    Views:
    384
    Paul Lutus
    Sep 10, 2004
  3. Brandon McCombs
    Replies:
    4
    Views:
    529
    Richard Wheeldon
    Aug 28, 2006
  4. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,049
  5. Alex Dowad
    Replies:
    4
    Views:
    300
    Michel Demazure
    May 1, 2010
Loading...

Share This Page