any help?

From: Matthew Siddall (ms003h3818_at_blueyonder.co.uk)
Date: 08/13/04


Date: Fri, 13 Aug 2004 10:54:10 GMT

Hiya all,

I'm trying to teach myself some perl so I can write cgi script eventually to
modify files on a remote server. I'm trying to grasp certain things, so I
have written a subroutine to create a file and pre-fill it with certain
values, based on the arguments given to the subroutine. Each file I create
has the file name as it's first line (for ease of access among other things)
and I'm trying to write a subroutine to let me change the name of the file,
including changing the first line of the file.

So far, i have been able to do all that by creating a new file and writing
the data across one line at a time (using a for loop) but I can't get it to
delete the old file. I've looked everywhere I could think of on the
documentation, and I can't see a delete file command (there is a delete
command, but it seemed to reference variables, and I don't want to screw
things up). I tried renaming the file so I could then delete it manually
later, but for some reason it won't rename it, even if I close the file
before trying (I thought it may be because the file is still open).

I'm running win2000 with Activestate ActivePerl 5.8, and I'm almost
completely new to it, so if you can offer any help I'd be grateful.

The code I have so far is:

open(LOG, ">>log.txt") or die "can't open logfile: $!";

# ren(a, b) creates a copy of file a called b and changes the first line to
b then marks a for deletion
sub ren {
 my $old = shift;
 my $new = shift;
 print LOG "rename $old -> $new \n";
 open OLD, "< $old" or die "cannot open $old :$!";
 open NEW, "> $new" or die "cannot open $new :$!";
 my $line = <OLD>;
 print NEW "$new \n";
 while (<OLD>)
  {
  print NEW "$_";
  }
 my $newname = "deleted";
 renf($old, $newname);
 }

# renf(a, b) renames file a to b
sub renf {
 $a = shift;
 $b = shift;
 rename($a, $b);
 print LOG "renamed $a to $b \n";
 }

ren(mike, martin);

____

mike was the name of a random file I created to test this.

Thanks,
Matt



Relevant Pages

  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • RE: back to orininal question
    ... hampered to a problem with passing shift I think. ... Inside the methodsubroutine we write this. ... sub GetOfficers { ...
    (perl.beginners)
  • Re: Replacing a line
    ... #Using core module Tie::File to process a file in this subroutine ... sub process_one_file { ... $cpp_file = shift; ... for (@array) #Each line should come one by one ...
    (comp.lang.perl.misc)
  • RE: passing database data to a sub
    ... Second, GetOfficersis called as a subroutine, not as a method of an object. ... I not completely sure about this 'shift' thing anyway :-) ... sub teardown ... # Note...this subroutine uses the template 'db_mainmenu.tmpl.htm' to give ...
    (perl.beginners)
  • A little bit of SOAP
    ... soaptest has a subroutine called ReNameTiff which needs 2 arguments, ... In this example the args are quoted together and to access them I ... my $self = shift; ...
    (perl.beginners)