FAQ
I am trying to copy/move files under WinNT from remote server(s) to
a central server using File::Copy --see code snippet below:

snippet begins------
use File::Copy;
......

open (LOGFILE, ">>testlog") or die (print "$! problem opening testlog\n");
print LOGFILE "$date: \n";
print LOGFILE "This is a test of moving a handle--ONCE AGAIN\n";
print LOGFILE "the end\n";

#my $result = copy ($fromdir, $todir) or die ("Error--can't copy LogFile :
$!\n");
my $result = move (\*LOGFILE, $todir) or die ("Error--can't move the
LogFile: $!\n");


close (LOGFILE);

.......





snippet ends----

It works fine when I pass it a literal reference for the source and
destination using copy or move commands. However, I can't get a reference
to a file handle to work. There must be something really simple that I'm
missing. I know that it's not a permissions issue and I know the file does
get created once I've closed the File Handle. I'd like to keep the file
handle open though so that I can grab the contents of the File Handle for
another routine. the move command always returns 0 when referencing the
LOGFILE file handle. I'd like to use move so I don't have to copy the
files and remove them from the source directory afterwards.

Thanks

Jeff Smith

Search Discussions

  • Dan Muey at Mar 4, 2003 at 8:11 pm
    Not sure about the fulehandle problem but you may want to look at Net::FTP
    For transfering file samongst servers. Quite easy to use and fast and all of that.

    DMuey
    -----Original Message-----
    From: Smith Jeff D
    Sent: Tuesday, March 04, 2003 1:53 PM
    To: 'beginners@perl.org'
    Subject: Help with File Handle


    I am trying to copy/move files under WinNT from remote
    server(s) to a central server using File::Copy --see code
    snippet below:

    snippet begins------
    use File::Copy;
    ......

    open (LOGFILE, ">>testlog") or die (print "$! problem opening
    testlog\n"); print LOGFILE "$date: \n"; print LOGFILE "This
    is a test of moving a handle--ONCE AGAIN\n"; print LOGFILE
    "the end\n";

    #my $result = copy ($fromdir, $todir) or die ("Error--can't
    copy LogFile : $!\n"); my $result = move (\*LOGFILE, $todir)
    or die ("Error--can't move the
    LogFile: $!\n");


    close (LOGFILE);

    .......





    snippet ends----

    It works fine when I pass it a literal reference for
    the source and destination using copy or move commands.
    However, I can't get a reference to a file handle to work.
    There must be something really simple that I'm missing. I
    know that it's not a permissions issue and I know the file
    does get created once I've closed the File Handle. I'd like
    to keep the file handle open though so that I can grab the
    contents of the File Handle for another routine. the move
    command always returns 0 when referencing the
    LOGFILE file handle. I'd like to use move so I don't have
    to copy the
    files and remove them from the source directory afterwards.

    Thanks

    Jeff Smith

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • Wagner, David --- Senior Programmer Analyst --- WGO at Mar 4, 2003 at 8:19 pm

    Smith Jeff D wrote:
    I am trying to copy/move files under WinNT from remote server(s) to
    a central server using File::Copy --see code snippet below:

    snippet begins------
    use File::Copy;
    ......

    open (LOGFILE, ">>testlog") or die (print "$! problem opening
    testlog\n"); print LOGFILE "$date: \n";
    print LOGFILE "This is a test of moving a handle--ONCE AGAIN\n";
    print LOGFILE "the end\n";

    #my $result = copy ($fromdir, $todir) or die ("Error--can't copy
    LogFile : $!\n");
    my $result = move (\*LOGFILE, $todir) or die ("Error--can't move the
    LogFile: $!\n");


    close (LOGFILE);

    .......





    snippet ends----

    It works fine when I pass it a literal reference for the source and
    destination using copy or move commands. However, I can't get a
    reference to a file handle to work. There must be something really
    simple that I'm missing. I know that it's not a permissions issue
    and I know the file does get created once I've closed the File
    Handle. I'd like to keep the file handle open though so that I can
    grab the contents of the File Handle for another routine. the move
    command always returns 0 when referencing the LOGFILE file handle.
    I'd like to use move so I don't have to copy the files and remove
    them from the source directory afterwards.

    Thanks

    Jeff Smith
    Jeff, I am running 5.6.0 but when I look at copy using filehandle I see :


    use File::Copy cp;
    $n=FileHandle->new("/dev/null","r");
    cp($n,"x");'

    which is not what you are trying. You are trying to use an already open file and that I don't believe is the context of what I see in the doc. The group can correct me ( and they will) if I am wrong.

    Wags ;)


    **********************************************************
    This message contains information that is confidential
    and proprietary to FedEx Freight or its affiliates.
    It is intended only for the recipient named and for
    the express purpose(s) described therein.
    Any other use is prohibited.
    ****************************************************************
  • Rob Dixon at Mar 4, 2003 at 9:22 pm
    Hi Jeff & Wags.

    David --- Senior Programmer Analyst --- Wgo Wagner wrote:
    Smith Jeff D wrote:
    I am trying to copy/move files under WinNT from remote server(s) to
    a central server using File::Copy --see code snippet below:

    snippet begins------
    use File::Copy;
    ......

    open (LOGFILE, ">>testlog") or die (print "$! problem opening
    testlog\n"); print LOGFILE "$date: \n";
    print LOGFILE "This is a test of moving a handle--ONCE AGAIN\n";
    print LOGFILE "the end\n";

    #my $result = copy ($fromdir, $todir) or die ("Error--can't copy
    LogFile : $!\n");
    This looks as though you're trying to copy a directory? Where did
    $fromdir and $todir come from? Glad to see it's commented out.
    my $result = move (\*LOGFILE, $todir) or die ("Error--can't move the
    LogFile: $!\n");
    You're using the original write-only filehandle you used to append to
    the log file.

    a) You won't be able to read from this handle to copy the data.
    b) The data is more than likely still buffered as it hasn't been
    flushed.
    c) While the file is open you can't delete it, and so can't move it.

    close (LOGFILE);

    .......





    snippet ends----

    It works fine when I pass it a literal reference for the source and
    destination using copy or move commands. However, I can't get a
    reference to a file handle to work. There must be something really
    simple that I'm missing. I know that it's not a permissions issue
    and I know the file does get created once I've closed the File
    Handle. I'd like to keep the file handle open though so that I can
    grab the contents of the File Handle for another routine.
    You mean you want to read the contents of the log file that you've
    already written?
    The move
    command always returns 0 when referencing the LOGFILE file handle.
    I'd like to use move so I don't have to copy the files and remove
    them from the source directory afterwards.
    As I said, I don't believe you can delete a file while it's open,
    although under VMS you can mark it to be deleted when it is
    closed. That may be possible on your platform.
    Jeff, I am running 5.6.0 but when I look at copy using filehandle I
    see :


    use File::Copy cp;
    $n=FileHandle->new("/dev/null","r");
    cp($n,"x");'

    which is not what you are trying. You are trying to use an already
    open file and that I don't believe is the context of what I see in
    the doc. The group can correct me ( and they will) if I am wrong.
    cp() is a synonym for copy(). The 'new' method of FileHandle is similar
    to open(), so the above is like:

    open $n, "< /dev/null";
    copy ($n, "x");

    and $n is a glob reference. But what good you would be doing by
    copying from the null device I don't know!

    Cheers,

    Rob
  • R. Joseph Newton at Mar 5, 2003 at 5:46 am
    Hi Jeff,

    DONT!

    Smith Jeff D wrote:
    I am trying to copy/move files under WinNT from remote server(s) to
    a central server using File::Copy --see code snippet below:

    snippet begins------
    use File::Copy;
    ......

    open (LOGFILE, ">>testlog") or die (print "$! problem opening testlog\n");
    print LOGFILE "$date: \n";
    print LOGFILE "This is a test of moving a handle--ONCE AGAIN\n";
    print LOGFILE "the end\n";

    #my $result = copy ($fromdir, $todir) or die ("Error--can't copy LogFile :
    $!\n");
    my $result = move (\*LOGFILE, $todir) or die ("Error--can't move the
    LogFile: $!\n");
    Really, just don't try to mix file access and file handling. These processes should be completely insulated from each other. In fact, the system should disallow access for file-handling to any file currently opened. For one thing, you have no way of even knowing your test statements are contained in the logfile. Filehandles generally don't promise to flush until they are closed, or they receive some explicit fluxh command.
    close (LOGFILE); #should come BEFORE any file-handling procedures

    .
    It works fine when I pass it a literal reference for the source and
    destination using copy or move commands. However, I can't get a reference
    to a file handle to work. There must be something really simple that I'm
    missing. I know that it's not a permissions issue and I know the file does
    get created once I've closed the File Handle. I'd like to keep the file
    handle open though so that I can grab the contents of the File Handle for
    another routine. the move command always returns 0 when referencing the
    LOGFILE file handle. I'd like to use move so I don't have to copy the
    files and remove them from the source directory afterwards.

    Thanks

    Jeff Smith
    Whatever the reason, this is the wrong way to go about it. Even if you could make this work, it would be the equivalent of taking a plane aloft for a midair refeulking, when you could roll it over to the pump. Don't shoot yourself in the foot with fancy tricks. Do file-handling only on closed files.

    If your planned design requires you to keep a file handle open through a move, re-examine your design premises--one of them is wrong.

    Joseph

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 4, '03 at 7:53p
activeMar 5, '03 at 5:46a
posts5
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase