CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Accepting a string or a file handle or a reference?

 



BorisE
Novice

May 18, 2008, 12:08 PM

Post #1 of 3 (659 views)
Accepting a string or a file handle or a reference? Can't Post

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?


(This post was edited by BorisE on May 18, 2008, 12:45 PM)


KevinR
Veteran


May 19, 2008, 10:00 AM

Post #2 of 3 (642 views)
Re: [BorisE] Accepting a string or a file handle or a reference? [In reply to] Can't Post

To answer the part about determining what is what:


Code
open (my $fh, 'test.txt') or die "$!"; 
print ref $fh;


$fh is an indirect filehandle that is a reference of the type "GLOB". You can check what type of thing a reference is like so:


Code
if (ref $fh eq 'GLOB') { 
its a filehandle
}


You can also look into the IO class of modules:

# IO::Dir - supply object methods for directory handles
# IO::File - supply object methods for filehandles
# IO::Handle - supply object methods for I/O handles
# IO::Pipe - supply object methods for pipes

And the UNIVERSAL module.
-------------------------------------------------


BorisE
Novice

May 19, 2008, 12:26 PM

Post #3 of 3 (638 views)
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.


(This post was edited by BorisE on May 20, 2008, 12:13 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives