Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

(This is part of a larger program. I stripped the code example down.)

I'd like to get a file from somewhere (HTTP, SCP, FTP, local file) and make a local copy via fetch(), pass the file descriptor around, modify the content and later on write the file content to one or more other files.

Alternative a) It would be easy to do this via actual file names but I would like to use open filehandles without an actual filename. This way I wouldn't have to worry about someone tampering with my data while I'm working on it.

Alternative b) I don't want to slurp the file content into main memory.

Script output:

FETCH The file '/etc/services' has inode 2105201 and size 19398
FETCH The file 'GLOB(0x10c7c78)' has inode 142159 and size 19398
STORE The file 'GLOB(0x10c7c78)' has inode 142159 and size 19398
STORE The file '/tmp/services.d1JH' has inode 142160 and size 0
STORE Copying filehandle tmp1 (GLOB(0x10c7c78)) to tmp2 (/tmp/services.d1JH) file.
STORE The file 'GLOB(0x10c7c78)' has inode 142159 and size 19398
STORE The file '/tmp/services.d1JH' has inode 142160 and size 0
-rw------- 1 thomas thomas 0 Jan 16 21:15 /tmp/services_copy

As you can see, the store() function seems to receive the correct data to the file object, but copy does not actually copy the file. I seriously scratched my head on this, but so far no enlightenment has occured. Buffering issue? Copy not happy with the GLOBs? Something totally different?

Time to confess my non-knowledge and ask a senior monk. Hopefully I get something more then just a koan. ;)

#!/usr/bin/env perl use strict; use warnings; use 5.010_001; use English qw( -no_match_vars ); # avoid regex performance penalty use File::Copy; use File::Temp qw(tempfile); use Readonly; Readonly my $STAT_INODE => '1'; Readonly my $STAT_SIZE => '7'; my $tmp_fh = fetch(); store($tmp_fh); exit 0; sub fetch { my $src_file = '/etc/services'; show_filestats('FETCH', $src_file); my ( $tmp1_fh, $tmp1_filename ) = tempfile(); unlink $tmp1_filename; # copy $src_file, $tmp1_fh or die "$ERRNO\n"; show_filestats('FETCH', $tmp1_fh); return $tmp1_fh; } sub store { my $tmp1_fh = shift; my $tmp2_file = File::Temp->new( TEMPLATE => '/tmp/services.XXXX', + UNLINK => 0 ); my $dst_file = '/tmp/services_copy'; show_filestats('STORE', $tmp1_fh); show_filestats('STORE', $tmp2_file); print "[STORE] Copying filehandle tmp1 ($tmp1_fh) to tmp2 ($tmp2_f +ile) file.\n" or croak(); copy $tmp1_fh, $tmp2_file or die "E: $ERRNO\n"; show_filestats('STORE', $tmp1_fh); show_filestats('STORE', $tmp2_file); # apply permissions and ACLs rename $tmp2_file, $dst_file or die "E: $ERRNO\n"; } sub show_filestats { my $id = shift; my $fh = shift; my ( $inode, $size ) = ( stat $fh )[ $STAT_INODE, $STAT_SIZE ]; print "[$id] The file '$fh' has inode $inode and size $size\n" or croak(); return; }

In reply to 'Anonymous' copy via file handle fails by Monk::Thomas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (10)
    As of 2014-09-22 15:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (198 votes), past polls