I'm moving some code into a module. In the process I'd like to make it a bit more versatile...

Basically I'd like to be able to pass either data or a filehandle or reference to one of those and still have the sub work ok on it (if it is passed a filehandle it should slurp the file). I've seen modules elsewhere that claim to work like that...

I figure the first thing is to determine what was passed. I'm a bit confused as to how to do this though, since file handles seem to take so many forms e.g. typeglobs, objects, references?

I tried seeing how someone else's program did it and now I'm more confused, it has two entry points one for strings and the other for files. The files version makes a local typeglob and opens a file, then passes the typeglob to the string handling routine. How does that work? is there some behind-the-scenes magic at work here?

Re: [KevinR] Accepting a string or a file handle or a reference?
[In reply to]

Can't Post

That's probably what I want, thanks. I figure if I can detect a filehandle I can add code to detect it and "slurp" the contents.

One thing that's really bugging me though is those wierd filehandles that some CGI modules return, that act as filehandles when used as filehandles but return a string (the filename of the remote file) when used in scalar context.

Added: Looks like I was right to be worried, CGI filehandles are objects and running "ref" on them returns "Fh", the name of the package, instead of "GLOB". Since I'm planning on passing handles obtained from CGI and CGI::FormBuilder this might complicate things.