Angel has asked for the wisdom of the Perl Monks concerning the following question:

Hi having an odd problem I am doing this:
my $outputpage; open(TEMPLATE, "default_redirect.html") or die "can't open redirect page $!"; while( <TEMPLATE> ) { $outputpage .= <TEMPLATE>; } close(TEMPLATE);
with this text file:
<head> <META HTTP-EQUIV="Refresh" CONTENT="1;URL="http://my_server.com/new_us +er.cgi?id=<var>ID</var>&template=index.shtml"> </head> <body> Enter appropriate school page here </body>
but it is skipping the odd numbered lines any clue why?

Replies are listed 'Best First'.
Re: Reading in Text File and skipping lines
by insensate (Hermit) on Nov 07, 2002 at 22:05 UTC
    The <FILEHANDLE> construct is actually a shortcut for reading in a line from the filehandle and storing it in $_. By effectively calling <FILEHANDLE> two times in your block you are only appending every other line. Try this:
    while( <TEMPLATE> ) { $outputpage .= $_; }
      This is a great example of why i discourage the use of $_ and other implicit variables. It perpetuates misunderstanding of what is really going on. Do others feel that this shorthand leads to confusion, or am i too anal?
      I like to go one step further in reading files:
      while( my $line = <TEMPLATE> ) { $outputpage .= $line; }
      or even
      my @lines = <TEMPLATE>; foreach my $line (@lines) { $outputpage .= $line; }
        Do others feel that this shorthand leads to confusion, or ...

        It's only confusing until you get it. =) After that, it leads to simplification and reduced clutter in scripts.

Re: Reading in Text File and skipping lines
by DamnDirtyApe (Curate) on Nov 07, 2002 at 22:15 UTC

    I prefer a different approach when reading a filehandle into a scalar.

    my $outputpage; do { local $/; $outputpage = <TEMPLATE>; } ;

    If you've not seen this before, it essentially says, "forget about what you thought an end-of-line character was, and read the whole thing." It only "forgets" for the scope of the do block, so the effect is totally isolated.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      If you just want to slurp a file into a scalar then this is a pretty simple way to do it, no need to worry about do's, while loops undefs etc.
      my $outputpage = join('',<TEMPLATE>) ;

      Dingus


      Enter any 47-digit prime number to continue.

        True. But you do have to worry about opening the file, checking the return code, outputing a sensible error message if it fails.

        The nice thing about juerd's slurp idiom (as shown in wonko the sane's post above) is that it takes care of that for you.


        Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy
Re: Reading in Text File and skipping lines
by Wonko the sane (Curate) on Nov 07, 2002 at 22:13 UTC
    Nice prior discussion on doing this same type of thing.

    http://perlmonks.org/index.pl?node_id=50525

    Which has one of my favorite snippets:

    my $contents = do { local( *ARGV, $/); @ARGV = $file; <> }; # Slurp Fi +le contents