Eric Lucas on 4 May 2005 13:03:17 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Not understanding this behavior


I've written a short perl program for a friend to substitute
city, state, and zip in a template file and write each file out
with a new name.

My problem is that each file contains the city, state, and zip
of the first row in the zips.csv file even though the output file
name changes.

Anybody have an idea why the $city $state or $zip appear to not be
re-evaluated in the s///?  I thought I had to add a trailing "o" to
force the PATTERN or REPLACEMENT regular expression to compile only
once.  Looks to me like I get that behavior by default.  I've read
and re-read pages 72-74 of "camel" but don't see what my error is.

Here is the relevant section...
------------------------------------------------------------
  open ZIPS, "zips.csv" or die "ERROR: Must have file \"zips.csv\" in current directory!\n" ;
  while (<ZIPS>) {
    chomp ;
    ($city,$state,$zip) = split (/,/) ;
    $outfilename = "welcome_" . substr($city,0,5) . $zip . ".txt" ;
    $outfilename =~ s/ /_/g ;
    open OF, ">../$outfilename" or die "ERROR: cannot open ../$outfilename for some reason!\n" ;
    foreach $line (@template) {
        $line =~ s/ZCITY/$city/ ;
        $line =~ s/ZSTATE/$state/ ;
        $line =~ s/ZZIP/$zip/ ;
        printf OF $line ;
    }
    close OF ;
  }
  close ZIPS ;
------------------------------------------------------------

zips.csv:
Adamstown,Pennsylvania,19501
Arcola,Pennsylvania,19420
Ardmore,Pennsylvania,19003
Aston,Pennsylvania,19014
Atglen,Pennsylvania,19310


Thanks
Eric
-- 
------------------------------------------------------------------------
#  Eric A Lucas                        |             __|__
#   "Oh, I have slipped the surly bond |          *---oOo---*
#    of earth and danced the skies on  |    __|__             __|__
#    laughter-silvered wings...        | *---oOo---*       *---oOo---*
#        -- John Gillespie Magee Jr.   |
-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**