$path = "bar";
use FileCache;
cacheout ($path); # each time you use a filehandle
print $path "output";
And it falls over with the wonderful error message:
Can't use string ("bar") as a symbol ref while "strict refs" in use at
filecache line 22.
So I spend ages trying to work out why the code isn't replacing $path with a ref to a filehandle like it should and it's leaving a bare string in there. I post to London.pm. I ask on IRC. I'm stumped...and then I look at the source. Of course! So FileCache doesn't create reference as store them in $path. It opens a filehandle in the calling package with the same name as the $path. This means that $path in the print statement is treated as a symbolic reference for a variable name and finds the right filehandle.
Okay, this is quite neat, but really really dangerous. use strict quite rightly barfs all over the place when I do this and I have to turn it off.
So it looks like I'm going to have to write my own module. Anyone got any better suggestions? Alternative modules on CPAN to do this?
DIY (Score:2, Insightful)
And if she isn't responsive, upload the new version yourself!
Re:DIY (Score:2)
Ummm (Score:1)
FileCache out of the picture, string filehandles
just don't work under strict refs. They're
essentially a form of symbolic reference, if
you know what you're doing it's perfectly
reasonable to run with no strict 'refs'.
PS> I patched FileCache for perl 5.8, which still
has this "problem", but more features. If you
don't want to get all of 5.8 you can retrieve the
module from ftp://pthbb.org/pub/pm/FileCache/ [pthbb.org]
Enjoy
Were that I say, pancakes?
Re:Ummm (Score:2)
As for running without use strict, I'm not sure I trust myself that much ;-) I guess I could just turn it off lexically for the tight loop, but I get really worried about flicking the safety off the loaded gun pointing at my feet.
Anyway, I've been busy since I posted my journal - I had a go at implementing my own modified version of FileCache whic
Re:Ummm (Score:1)
I'll consider addng a line to the CAVEATS about absolute paths. It's really somehting the user should just keep in mind, again I think of FileCache (not that I am the oringal author) as simply an abstraction over open. It has one sole purpose and is not to catch every tiny faux pas the user might commit ;-)
As for the roll-over, that wouldn't be an issue. So the file gets closed, and then might soon be reopened. If you look closely, the hash element is deleted when the
Were that I say, pancakes?
Re:Ummm (Score:2)
Good point. Should have thought about that a bit more.
Yeah, though if I use open to open a second file with the same name from a different directory then it opens a different file. FileCache will use the first file it opened.
However, I don't think I can think of any way to work around this with the current method of creating filehandles in the parent's namespace which
Re:Ummm (Score:2)
While ++ will not then have exactly the same semantics, it will keep the value increasing, and not have the "random" effect you describe.
Re:Ummm (Score:2)
Interesting - does this mean that ++ and += 1 are not always the same operation?
Re:Ummm (Score:1)
perl -MO=Terse -e '$foo++; $bar += 1;'and note the ops used++gets folded toPP(preinc)which will do a simple++SvIVX(sv)if it's a clean IVIf it's not it calls
sv_inc(sv)which does checks for magic and, if the value is an NV, performsSvNVX(sv) += 1.0;. How long the sv retains a value like a tight-jawed integer will be dependant on the implementation of the floating point codeRe:Ummm (Score:1)
I think a better name would be IO::FileCache.
One could also subclass FileCache to provide the
rel2abs masking.
Were that I say, pancakes?
Re:Ummm (Score:2)
I need to tidy everything up a bit first and maybe run some benchmarks.
Fun.
Re:Ummm (Score:1)
and it isn't exactly caching files
either. IO::FileCache highlights
the relationship.
Were that I say, pancakes?
Re:Ummm (Score:2)
Confuscioun (sic) (Score:1)
And you did say CORE earlier, no?
And what do you mean you were talking
about? You've been talking about
cacheout, that's in FileCache...
Were that I say, pancakes?