Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to process multiple input files?

by jwkrahn (Abbot)
on May 22, 2011 at 20:42 UTC ( [id://906199]=note: print w/replies, xml ) Need Help??


in reply to How to process multiple input files?

You need to reset $count for each file.    Something like this (UNTESTED):

#!/usr/bin/perl use strict; use warnings; $^I = ".bak"; undef $/; my $count = 0; while ( my $line = <> ) { $line =~ s{ (<\/div>) } { ++$count == 2 ? "\t<?php include(\$_SERVER['DOCUMENT_ROOT'].\"\/includes +\/footer.php\"); ?>\n\n$1" : $1 }gex; print $line; $count = 0; }

Replies are listed 'Best First'.
Re^2: How to process multiple input files?
by John M. Dlugosz (Monsignor) on May 23, 2011 at 00:22 UTC
    I worry that an empty file will stop it prematurely. Or a file might contain just "0" or somesuch, but that's less likely. Since he's slurping whole files rather than reading lines, I think it would be prudent to test for defined. (Hmm, what does the normal line-oriented read do if an empty file is in the list? Maybe it's always an issue.)

    update: never mind. In production code I would have simply written defined to be sure, but looking through the docs I see that this construct is special even in the case of explicit assignment. I know that the quick while(<>) tests for defined, or started to at some specific version of Perl (I remember the classic Camel book explaining how lines are never False because they end in "\n"), but wasn't sure that applied when assignment was being made.

    In general, I rely less on special cases and magical meanings in well-written production code than in a quick one-liner. Declaring variables, and not using $_ much falls into the same category, so I somehow was thinking the magic was not in effect.

      I think it would be prudent to test for defined.

      The code I posted:

      while ( my $line = <> ) {

      does test for defined.

        No, it tests for normal Truth. An undef, an empty string, a "0" etc. will all test as False after being assigned to $line.
Re^2: How to process multiple input files?
by rnaeye (Friar) on May 22, 2011 at 21:50 UTC

    thanks for so much. works great.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://906199]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found