file name of file that reference to type glob points to

E

ed

Hey all. This is probably a elementary question, but I can't
seem to be able to find a builtin function or combination of functions
that will let me achieve this.

I want to get the full path to a file that is pointed to from a
reference to a type glob.
Below is a simple example of what I want to do.


## start perl script ##
use strict;
use warnings;

open(FH, '<', './testees/noname4.html')
or die("can't open file: $!");
&doSomething(\*FH);
close FH;

sub doSomething
{ my $FH = shift;
# I need the name of the file that
# $FH points to, to do something!
# something like filename($FH) would be nice.
}
## end perl script ##


There's a few things I can do to get around this problem.
But now I'm just curious of how this could be done.

tia,
--ed
 
J

John W. Krahn

ed said:
Hey all. This is probably a elementary question, but I can't
seem to be able to find a builtin function or combination of functions
that will let me achieve this.

I want to get the full path to a file that is pointed to from a
reference to a type glob.
Below is a simple example of what I want to do.

## start perl script ##
use strict;
use warnings;

open(FH, '<', './testees/noname4.html')
or die("can't open file: $!");
&doSomething(\*FH);
close FH;

sub doSomething
{ my $FH = shift;
# I need the name of the file that
# $FH points to, to do something!
# something like filename($FH) would be nice.
}
## end perl script ##

There's a few things I can do to get around this problem.
But now I'm just curious of how this could be done.

Why not just pass the file name to the sub and open it in the sub?

If you are running this on Linux then you can find the file name in the
/proc directory.



John
 
E

ed

Why not just pass the file name to the sub and open it in the sub?

Well I'm creating a class where one of the arguments to the
constructor
can either be the path to the file to open, or an open filehandle.
Since more than one object is likely to work with the file I thought
it would be more efficient to have the option of passing the
filehandle
around instead of having each object manually open/close the file.

The reason I need the path is not just to open the file, I need to use
the path to make some modifications to it, then do something with it.

I could make it so the first parameter is either undef or a
filehandle, then they could pass the path in the second argument.

I could offer two ways of creating the object:

method 1:

#this would open the file using $path
$obj = new Obj(undef, $path, 'param3');

method 2:

#this doesn't need to open the file, but it still gets the
#path to the file it's working on from $path supplied in
#the second argument
$obj = new Obj(\*FH, $path, 'param3');

So if the first argument is undefined I'll open the file using the
value
supplied in the second argument, and then I also have the path to the
stuff I need to do with it.

If the file's already opened they can send the filehandle instead of
an undefined
value.

--ed
 
E

ed

Why do you want to get the full path to a file that is pointed to from a
reference to a type glob?

I just posted trying to explain it.

What "name" should it use when the FH is not associated with any file?

eg:

open FH, 'ls|' ...
In the context of my object the files will always be text/* type
files. Primarily Html. So the person using the object should only
pass a filehandle that points to an appropriate type of file.

But I get your point that filehandles can point to things that aren't
really files. So I guess because of that it probably wouldn't make
much sense for the language to provide a way to get the file name when
there doesn't necessarily have to be a name associated with it.

I think I should be ok though using the method I described in my last
post.
If you see any flaws in the method I described in my last post, please
let me know.

thanks,
--ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top