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

Getting filename from IO::File object

by arunhorne (Pilgrim)
on Jan 30, 2003 at 11:56 UTC ( [id://231280]=perlquestion: print w/replies, xml ) Need Help??

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

I am subclassing the Graph::Writer module to provide output for a new graph format and must work within the confines of the following function.

sub _write_graph { my $self = shift; my $graph = shift; my $FILE = shift; #while (<$FILE>) #{ #} return 1; }

The function receives a file handle (IO::File=GLOB(0x8333a68)) to a single file ($FILE). However, I need to create two output files for this graph format and thus I need to get the filename from the $FILE file handle and use this as the base name from which I can generate two new filenames and open these for output (and close $FILE). I know this is a bit of a kludge, but I'm within the confines of original function that is being overriden. How can I do this, or has anyone got a better idea how I could do it without changing the parameters accepted by the function?

Note that I need concurrent access to the two new files rather than sequential so will need two open file handles at once.

Thanks, Arun

____________
Arun

Replies are listed 'Best First'.
Re: Getting filename from IO::File object
by Jaap (Curate) on Jan 30, 2003 at 12:22 UTC
    To my knowledge, the IO::File module has no method for retrieving the filename of a IO::File object, so no luck there.

    Then you have to think about how to extend the _write_graph object with backwards compatibility. I suggest adding an optional parameter. When this parameter is not set, just use the original code. If it is set, switch to your new code (which can then use the new parameter).
•Re: Getting filename from IO::File object
by merlyn (Sage) on Jan 30, 2003 at 15:45 UTC
    There may not even be a filename for the IO::File object. For example:
    my $handle = IO::File->open("/tmp/my$$", "w+"); unlink "/tmp/my$$";
    This handle functions completely nicely (at least in Unix), but has no "name". When the last reference to this handle is closed, the disk space in /tmp is reclaimed.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Getting filename from IO::File object
by derby (Abbot) on Jan 30, 2003 at 13:45 UTC
    I don't know much about Graph::Writer, but getting a filename from a filehandle (or filedescriptor) is quite a thorny issue that cannot be solved in a platform independent way. If Graph::Writer supplies methods for opening files, your best bet is to also subclass that method and store the filename in the object.

    -derby

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found