in reply to Using $"
I'd find a different tutorial because that one is just dumb. $" is used as a separator, but "the start of every" is not a job you do with a separator. A sane way to do "start of every line" is:
print "#$_" while defined $_ = <>;
with appropriate file handling as required. Speaking of which, always use the three parameter open and lexical file handles:
open my $info, '<', $file or die "Can't open '$file': $!\n";
and remember that in general you can't "in place edit" files - they just don't work that way. Instead you need to make an updated copy of the file then rename or remove the old version and rename the new file to the original file's name. Perl can help a lot with that using the $^I ($INPLACE_EDIT) special variable that allows you to set the file extension to be appended to the backup (original) version of the file so you can write code like:
local @ARGV = $file; local $^I = '.bak'; print "#$_" while defined $_ = <>;
Which will "inplace edit" $file and create a backup copy of the original file by appending '.bak' to the original file name. Note that you could process a batch of files using the same code simply by adding the file names to the list assigned to @ARGV.
|
|---|