in reply to Stumped with $_ being modified
This variation has a routine which reads in the stylesheet and returns a hash reference to the data that was read. Theoretically, then, you can read in more than one stylesheet and choose which one you look up from, something that a global variable doesn't really permit.use strict; use warnings; my $stylesheet_file = "stylesheet.txt"; sub read_stylesheet { open(STYLESHEET, "<", $stylesheet_file) || die "Cannot read stylesheet_file\n"; my (%tag, $header); while (<STYLESHEET>) { if (/^HEADER\s+(\d+):/) { $header = $1; } elsif (/^nm\s+:\s+(\w+)/ { # Make a note of what header this tag # appeared in. $tag{$1} = $header; } } close(STYLESHEET); return \%tag; # Returns a reference to the hash } # ... (Main routine) my $input_file = "taggedfile.txt"; my ($output_file) = @ARGV; my $tag = read_stylesheet(); open(FILE, "<", $input_file) || die "Could not read $input_file\n"; open(OUT, ">", $output_file) || die "Could not write $output_file\n"; while (<FILE>) { # Perform substitutions s/^\{(\w+)\}/\{$1:$tag->{$1}\}/g; # Write line ($_) to OUT print OUT; } close(FILE); close(OUT);
I'd really suggest steering away from using local declared variables.while (my $line = <FILE>) { # ... Use $line where you would normally use $_ }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Stumped with $_ being modified
by jdporter (Paladin) on Jan 09, 2003 at 15:30 UTC | |
|
Re: Re: Stumped with $_ being modified
by ihb (Deacon) on Jan 09, 2003 at 15:24 UTC | |
by tadman (Prior) on Jan 10, 2003 at 03:18 UTC |