Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

opening files using handles, objects etc

by ftumsh (Scribe)
on Jul 06, 2006 at 10:00 UTC ( #559531=perlquestion: print w/replies, xml ) Need Help??

ftumsh has asked for the wisdom of the Perl Monks concerning the following question:

Lo all, I have seen some modules that allow the opening of a file by passing one parameter to just the one method. The param can be one of file name, open file handle or an IO::File object. I want to do the same sort of thing. What sort of tests do I do to see what type of paramter has been passed? Thx John
  • Comment on opening files using handles, objects etc

Replies are listed 'Best First'.
Re: opening files using handles, objects etc
by davorg (Chancellor) on Jul 06, 2006 at 10:11 UTC

    You want to look at the ref function. For more complex checks (to see if an object is a subclass of another object of if an object implements a certain method) then look at the isa and can methods from UNIVERSAL and the blessed function from Scalar::Util.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thinking about it... It would make sense to convert all possibilities to just the one in my object, would it be better to store it as a file handle or an IO::File object? I'm only writing text to the file, not doing anything special. Oh and reading from another file.

        Well thought; I recommend expecting a IO::Handle object... That way, you leave it up to the users if they're passing plain old perl handles, or IO::Scalar or their own implementation on a handle.

      For some reason I thought that there might be a bit of magic involved! Thanks for your time
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://559531]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-09-25 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?