in reply to C idioms in Perl
Well, that's not too bad. The only obvious things that might make it more perlish (imho) would be to use the implicit argument ($_) rather than $line, and to process the file in chunks other than lines. I also wouldn't use FileHandle; it's not necessary in modern Perls.
Here's a first clean-up, which uses a hash index rather than a reference to a hash value:
Here's another approach which is pushing the idiomaticity:local $ARGV = ($templfile); my $i = ''; while (<>) { if ( /^\s*<\s*TEMPLATE\s+NAME="(.*)"\s*>/i ) { $i = $1; } elsif ( /^\s*<\s*\/\s*TEMPLATE\s*>/i ) { $i = ''; } else { $hashref->{$i} .= $_; } } # cleanup: delete $hashref->{''};
while (<>) { no warnings 'uninitialized'; /^\s*<\s*TEMPLATE\s+NAME="(.*?)"\s*>/i .. /^\s*<\s*\/\s*TEMPLATE\s*>/i and $h{$1} .= $_; } # cleanup delete $hashref->{''}; s/^\s*<\s*TEMPLATE\s+NAME="(.*?)"\s*>\s*//i for values %$hashref;
Beyond that, you could do things like
but then you have to worry about the size of the file in memory.local $/; local $_ = <>; %$hashref = /\s*<\s*TEMPLATE\s+NAME="(.*?)"\s*>(.*?)\s*<\s*\/\s*TEMPLA +TE\s*>/sig;
|
|---|