Next | Lightweight Databases | 11 |
Using the -i facilities from inside a program requires a little trick
The files that -i operates on are the ones named in @ARGV
The special $^I variable holds the backup file suffix
(Empty string if no backup)
To engage -i, set up @ARGV and $^I and run a while <> loop:
sub delete_user { my ($file, $target_user) = @_; local $^I = ".bak"; local @ARGV = ($file); while (<>) { my ($user) = split /:/; print unless $user eq $target_user; } }
Now the opening and renaming are all implicit
Use local so that $^I and @ARGV recover their old values when the function is done
Next | Copyright © 2003 M. J. Dominus |