I've been having problems appending text to a file. I can't figure out
what is going on.
Here is a simple script that illustrates the problem. I have a file
named " extractedFiles". (The first character in its name is a space.)
It lives in the directory indicated in this script.
#!/usr/bin/perl -w
my $headdir = "$ENV{HOME}/Perl/MyPerl/Virus/New Viruses/Headers";
# my $headdir = "$ENV{HOME}/Desktop";
open EXTRACTED, ">>$headdir/ extractedFiles"
or die "cannot open extractedFiles: $!";
print EXTRACTED "Extracting head from somefile\n";
close EXTRACTED;
When I run the script, nothing is appended to " extractedFiles". The
content of the file is unchanged. But its "Date modified" time has changed
to the time when the script was run.
If I comment out the first "my $headdir" line and uncomment the second,
the script works just as it should. Every time it is run another line is
appended to the " extractedFiles" file on my desktop.
BTW, both " extractedFiles" files have the same permissions, "-rw-r--r--".
Any ideas would be greatly appreciated!
Regards,
Vic

Signature
Vic Norton
vic at norton dot name
John Delacour - 23 Oct 2003 00:35 GMT
All I can tell you is that it works perfectly here without my setting
any permissions and simply adding the mkdir line to emulate your dir
structure:
#!/usr/bin/perl -w
my $headdir = "$ENV{HOME}/Perl/MyPerl/Virus/New Viruses/Headers";
# my $headdir = "$ENV{HOME}/Desktop";
`mkdir -p "$headdir"` ;
open EXTRACTED, ">>$headdir/ extractedFiles"
or die "cannot open extractedFiles: $!";
print EXTRACTED "Extracting head from somefile\n";
close EXTRACTED;
JD
> When I run the script, nothing is appended to " extractedFiles". The
> content of the file is unchanged. But its "Date modified" time has changed
[quoted text clipped - 5 lines]
>
> BTW, both " extractedFiles" files have the same permissions, "-rw-r--r--".
Drieux - 23 Oct 2003 00:44 GMT
[..]
> my $headdir = "$ENV{HOME}/Perl/MyPerl/Virus/New Viruses/Headers";
> # my $headdir = "$ENV{HOME}/Desktop";
[quoted text clipped - 3 lines]
> print EXTRACTED "Extracting head from somefile\n";
> close EXTRACTED;
[..]
I modified your script to run with
my $headdir = "$ENV{HOME}/lib/some folder/Headers";
#my $headdir = "$ENV{HOME}/lib/bob/some folder/Headers";
and then used the Finder to create the "some folder" and Headers
below that. And have been able to 'update' the " extractedFiles"
each time I ran it. I used the commented out line to point at
a directory that I knew did not exist. As expected it causes the
die line
[jeeves: 12:] ./test*
cannot open extractedFiles: No such file or directory at ./test.plx
line 11.
[jeeves: 13:]
rather than updating the file:
[jeeves: 14:] ls -ltr ; cat *ex*
total 16
-rw-r--r-- 1 drieux house 90 Oct 22 16:40 extractedFiles
-rwxr-xr-x 1 drieux house 496 Oct 22 16:41 test.plx
Extracting head from somefile
Extracting head from somefile
Extracting head from somefile
[jeeves: 15:]
I am running 10.2.8 and the default perl build.
How are you checking that the the time stamp changes
but the contect does not?
ciao
drieux
---
Vic Norton - 23 Oct 2003 01:28 GMT
Problem solved. There was something wrong with the file. It was
encoded in Unicode(UTF-16), whatever that means. I have no idea how
it got that way. Apparently my script was just adding invisible lines
to the file.
(The Encoding option is at the bottom of the File Options icon in the
BBEdit Status Bar.)
Solution. I just copied what I needed to a new " extractedFiles"
(encoded in Mac Roman). Now I have no problems appending lines.
Regards,
Vic
>I've been having problems appending text to a file. I can't figure out
>what is going on.
[quoted text clipped - 32 lines]
>Vic Norton
>vic at norton dot name
John Delacour - 23 Oct 2003 02:37 GMT
> Solution. I just copied what I needed to a new " extractedFiles"
> (encoded in Mac Roman). Now I have no problems appending lines.
You might do better to write the System's standard utf8. MacRoman is
now a legacy encoding and the sooner it and the rest of them cease to
plague us the better.
shell> perldoc encoding
#!/usr/bin/perl -w
use encoding "MacRoman", STDOUT => "utf8";
my $dir = "$ENV{HOME}/Perl/MyPerl/Virus/New Viruses/Headers";
`mkdir -p "$dir"` ;
my $f = "$dir/ extractedFiles.txt";
open F, ">>:encoding(utf8)", $f
or die "cannot open extractedFiles: $!";
print F "Extracting head from ³somefile²\n" ;
The curly quotes will be encoded in utf8 and the file must be opened as utf8=2E