in reply to HELP!!!! VB code to PERL?

Your starter for 10:
for $varpart ( 54000 .. 55120) { open FILE,">c:/A$varpart.min" or die "c:/A$varpart.min error $!"; close FILE; }
Bear in mind that you won't learn anything from copying code: try reading the very good documentation in Tutorials and experimenting.

rdfield

Replies are listed 'Best First'.
Re^2: HELP!!!! VB code to perl?
by diotalevi (Canon) on Jan 08, 2003 at 15:11 UTC

    Oh *that* is a particularly poor spot of code. Someone else noted that it has the side effect of removing all the contents of the listed files. I think that the problem needs re-thinking. Consider what happens when you just open a file and save it without changing anything - atime and mtime are altered. I surmize that the poster's problem is really to make the file appear "new" or to simulate the effect of the common `touch' program.

    To that end - either the user should try using an honest to goodness real `touch' command from one of the unix tool sets. There are some good examples mentioned over at Looking for command-line UNIX-like 'tar' for Windows.

    A perl implementation is also very simple (which you well know if you redefine the problem this way). Here's the poster's original code snippet redone with this taken into account.

    use constant MIN_FILES => "C:/*.min"; touch_files( MIN_FILES ); sub touch_files { # The touch_files subroutine accepts a single # parameter which is passed to the glob() function. # All files matched by the file specification are # noted as having been "touched" or updated. This # routine does not actually alter the files so the # contents are not altered. # Get the filenames to search for from the # parameter list. my $filenames = shift; # Get the current timestamp. All files will be stamped # with this time. my $now = time; # Loop once for each file. See [perldoc] on glob() # to see what is acceptable input. for my $touch_file ( glob $filenames ) { # Update the mtime/atime values and don't bother # actually opening the file. This function (like # all the others) is documented in [perlfunc]. utime $now, $now, $touch_file; } }

    Update: I futzed with the code some so it's now a subroutine and there are more comments. I also added a 'my' to the $now variable. The sense of the code remains the same.


    Fun Fun Fun in the Fluffy Chair

Re: Re: HELP!!!! VB code to PERL?
by Hofmator (Curate) on Jan 08, 2003 at 14:32 UTC
    I hope what you have here is a typo and not trying to teach a newbie a lesson about why not to copy code. You are clobbering the files while he wanted to open them to append. It's not a nice thing to loose data ...

    So it should be:

    open FILE, ">>c:/A$varpart.min" or die $!;
    Note the '>>' instead of '>'.

    -- Hofmator