Passing entire file to a variable in a single command

Discussion in 'Perl Misc' started by solutions@gmail.com, Jul 7, 2005.

  1. Guest

    Hi,

    I was wondering if anyone knows how to pass the contents of an entire
    file to a variable IN A SINGLE LINE/COMMAND? It's trivial to do with a
    few lines, but I am wondering if there is a more clever way to do this.

    my $file_text = some_command( filehandle ) ???

    Thanks,
    David
    , Jul 7, 2005
    #1
    1. Advertising

  2. wrote in
    news::

    > I was wondering if anyone knows how to pass the contents of an entire
    > file to a variable IN A SINGLE LINE/COMMAND? It's trivial to do with a
    > few lines, but I am wondering if there is a more clever way to do
    > this.


    That is called slurping a file.

    > my $file_text = some_command( filehandle ) ???


    One way of doing that is:

    #!/usr/bin/perl

    use strict;
    use warnings;

    open my $in, '<', 'ttt.pl'
    or die "Cannot open ttt.pl: $!";

    my $text = do { local $/; <$in> };

    print $text;

    __END__

    For details, see perldoc perlvar, and read about $/

    These days, I prefer using File::Slurp by Uri Guttman
    (we miss you Uri :)

    You can find it on CPAN.

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

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 7, 2005
    #2
    1. Advertising

  3. wrote:
    > I was wondering if anyone knows how to pass the contents of an entire
    > file to a variable IN A SINGLE LINE/COMMAND?


    my $file_text = do { undef $/; <FH> };

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 7, 2005
    #3
  4. Gunnar Hjalmarsson wrote:
    > wrote:
    >> I was wondering if anyone knows how to pass the contents of an entire
    >> file to a variable IN A SINGLE LINE/COMMAND?

    >
    >
    > my $file_text = do { undef $/; <FH> };

    ---------------------------^^^^^

    Oops, please disregard my reply; Sinan showed how it can be done.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 7, 2005
    #4
  5. Gunnar Hjalmarsson wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> my $file_text = do { undef $/; <FH> };

    > --------------------------^^^^^
    >
    > Oops, please disregard my reply; Sinan showed how it can be done.


    Since the above code also 'works', I thoght I'd say *why* (I think) it's
    not good.

    Just saying undef $/ affects the value of $/ globally, which may cause
    surprises later on in the program. The whole point with using a block is
    to limit this value of the $/ variable to the block, which you do by
    instead saying

    my $file_text = do { local $/; <FH> };
    -------------------------^^^^^

    Hopefully my explanation was not as bad as the first suggestion. ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 7, 2005
    #5
    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. Jordan
    Replies:
    1
    Views:
    305
    Karl Seguin
    Oct 20, 2004
  2. Parvinder
    Replies:
    6
    Views:
    730
    Thomas G. Marshall
    Feb 27, 2005
  3. Crispy
    Replies:
    5
    Views:
    846
    e n | c k m a
    Aug 10, 2003
  4. David Southwell
    Replies:
    3
    Views:
    146
    Phrogz
    Nov 4, 2009
  5. Mothra
    Replies:
    10
    Views:
    466
    Anno Siegel
    Mar 11, 2005
Loading...

Share This Page