Professional Web Applications Themes

Is this possible? (file handles) - PERL Beginners

Once you close the file, memory for the file handle will be deallocated. You can unlink file using the original file name. unlink ("abc"); -----Original Message----- From: Jeff Westman [mailto:com] Sent: Thursday, April 01, 2004 2:02 AM To: perl_help Subject: Is this possible? (file handles) Hi, I want to remove an empty file using the file handle and not a variable name or literal name referencing it. Something like this: 1 #!/bin/perl 2 use warnings; 3 use strict; 4 no strict 'subs'; 5 6 open(F, "> abc") or die "cant create file: $!"; 7 close(F) or die "cant close file: ...

  1. #1

    Default RE: Is this possible? (file handles)

    Once you close the file, memory for the file handle will be deallocated.
    You can unlink file using the original file name.
    unlink ("abc");

    -----Original Message-----
    From: Jeff Westman [mailto:com]
    Sent: Thursday, April 01, 2004 2:02 AM
    To: perl_help
    Subject: Is this possible? (file handles)


    Hi,

    I want to remove an empty file using the file handle and not a
    variable name or literal name referencing it. Something like this:

    1 #!/bin/perl
    2 use warnings;
    3 use strict;
    4 no strict 'subs';
    5
    6 open(F, "> abc") or die "cant create file: $!";
    7 close(F) or die "cant close file: $!";
    8
    9 unlink(\*F) if -z (F);

    But, in this case, I get an error:

    $ runme.pl
    -z on closed filehandle F at ./x line 9.


    TIA,

    Jeff

    __________________________________
    Do you Yahoo!?
    Yahoo! Finance Tax Center - File online. File on time.
    http://taxes./filing.html

    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Venugopal Guest

  2. #2

    Default Re: Is this possible? (file handles)

    On Fri, 2 Apr 2004 10:55:07 +0530
    "Venugopal P" <com> wrote:
     

    On Unix systems, you may unlink a file after opening it. The directory
    entry for the file is deallocated, but the file is still open. When the
    file is closed, the disk allocation for the file is reclaimed.

    This is a handy method for creating temporary files that you want to be
    sure get deallocatted when the program using the temporary file exits.

    --
    Smoot Carl-Mitchell
    Systems/Network Architect
    email: com
    cell: +1 602 421 9005
    home: +1 480 922 7313
    Smoot Guest

  3. #3

    Default RE: Is this possible? (file handles)

    Venugopal P <com> wrote:
     

    This doesn't answer my question. I wanted to know if it is
    possible to remove a file using the FH name, not the variable name
    referencing it. Please see my original post below.

    Thanks again,

    JW

     


    __________________________________
    Do you Yahoo!?
    Yahoo! Small Business $15K Web Design Giveaway
    http://promotions./design_giveaway/
    Jeff Guest

  4. #4

    Default Re: Is this possible? (file handles)

    Jeff Westman wrote: 
    >
    >
    > This doesn't answer my question. I wanted to know if it is
    > possible to remove a file using the FH name, not the variable name
    > referencing it. Please see my original post below.[/ref]


    No, you cannot delete a "file" via it's handle;
    you can do this however -

    use 5.004;
    use Fcntl qw(:DEFAULT :flock);
    sysopen(BLACKHOLE, "$path", O_WRONLY | O_CREAT)
    or die "can't create $path: $!";
    flock(BLACKHOLE, LOCK_EX)
    or die "can't lock $path: $!";
    truncate(BLACKHOLE, 0)
    or die "can't truncate $path: $!";

    Then use unlink(filename) to delete it...
    -Sx-

    However, don't take my word for it -
    test all this yourself:

    open(TESTFILE, ">atestfile") or die "cant $!";
    print TESTFILE "Im data - not the android.";
    close(TESTFILE);

    # try to delete it by FH only -
    open(TF2, "atestfile") or die "cant $!";
    unlink(TF2);
    close(TESTFILE);

    # File still exists -
    print `ls -l`;

    Wc Guest

  5. #5

    Default Re: Is this possible? (file handles)

    Jeff Westman wrote:
     

    Okay
     

    No.

    Joseph

    R. Guest

Similar Threads

  1. Handles itemCreated
    By Joriz in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: June 27th, 07:30 PM
  2. Replies: 0
    Last Post: January 30th, 12:32 PM
  3. Replies: 3
    Last Post: January 30th, 01:47 AM
  4. Holding File handles in a {} anonymous hash
    By Dan Anderson in forum PERL Beginners
    Replies: 5
    Last Post: October 9th, 07:46 AM
  5. Replies: 3
    Last Post: December 17th, 03:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139