file name of file that reference to type glob points to

Discussion in 'Perl Misc' started by ed, Jul 1, 2003.

  1. ed

    ed Guest

    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
     
    ed, Jul 1, 2003
    #1
    1. Advertising

  2. ed wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jul 1, 2003
    #2
    1. Advertising

  3. ed

    ed Guest

    On Tue, 01 Jul 2003 00:40:15 GMT, "John W. Krahn" <>
    wrote:

    >
    >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
     
    ed, Jul 1, 2003
    #3
  4. ed

    ed Guest

    On Mon, 30 Jun 2003 19:45:10 -0500, (Tad
    McClellan) wrote:

    >
    >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
     
    ed, Jul 1, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    733
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    366
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    350
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    404
    Neil Hodgson
    Aug 2, 2004
  5. qanda
    Replies:
    1
    Views:
    122
    Tassilo v. Parseval
    Sep 15, 2003
Loading...

Share This Page