The Tool and Its Master
by Anonymous Monk on Jan 08, 2003 at 13:26 UTC
|
Hi,
A word to the wise, learn to use languages, don't learn to let them use you. Asking for a literal translation of code without understanding it will only severely limit you in the long run. Learning how any given technology works will grant you skills you didn't even consider.
Or as Ivan Illich states in 'Tools for a Convivial Society':
An individual relates himself in action to his society through the use of tools that he actively masters, or by which he is passively acted upon. To the degree that he masters his tools, he can invest the world with his meaning; to the degree that he is mastered by his tools, the shape of the tool determines his own self-image. Convivial tools are those which give each person who uses them the greatest opportunity to enrich the environment with the fruits of his or her vision.
Free your mind.
| [reply] |
Re: HELP!!!! VB code to PERL?
by derby (Abbot) on Jan 08, 2003 at 13:47 UTC
|
rdfield++ for the comments on learning and copying. Reading this really got me thinking (and ++ for that). Why would you need to open/save/close just to have the machines run? My guess is just the timestamp of the file needs updating. I can understand the unwillingness to play with production machines but if you need to just update the file's timestamp, take a look at utime. The code will be better reflective of what needs to be done (and will probably run a bit faster).
#!/usr/local/bin/perl -w
use IO::Dir;
use strict;
use vars qw( %dir $now );
$now = time;
tie %dir, "IO::Dir", ".";
foreach (keys %dir) {
next unless /min$/;
utime $now, $now, $_;
}
caveat - not sure how this works on platforms other than unix.
-derby | [reply] [d/l] |
Re: HELP!!!! VB code to PERL?
by {NULE} (Hermit) on Jan 08, 2003 at 13:27 UTC
|
...And we'll forgive you for being a n00b, but the language is called "Perl" and the executable is called "perl". No need to scream "PERL" at us. :)
You've already got one good answer to get you started, so I won't elaborate, except to reinforce checking out the tutorials and maybe picking up Learning Perl, or the Perl Black Book.
Update: your reply was uncalled for - I was just kidding around. You and your views are more than welcome in the community. However referring to Perl as PERL is simply wrong. It is not your language. Thank you.
Update 2: Boy - lots of trolls today. I was trying to be helpful - specifically by pointing out that for a new user of Perl, tutorials or a Learning Perl book would be more useful than posting "Fix this for me, I'm lazy". And I do not own Perl, but Mr. Larry Wall does and his preferred designation for the language is Perl. If you don't believe me, RTFM. Create your own language and call it what you want and I'll respect that as well. Thanks again.
{NULE}
--
http://www.nule.org | [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: HELP!!!! VB code to PERL?
by rdfield (Priest) on Jan 08, 2003 at 12:51 UTC
|
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 | [reply] [d/l] |
|
|
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 | [reply] [d/l] |
|
|
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 | [reply] [d/l] |
Re: HELP!!!! VB code to PERL?
by elbow (Scribe) on Jan 08, 2003 at 14:15 UTC
|
If all the files to be opened are in the same directory why not try this?
$path = "C:\\A";
my $file;
opendir (DIR, "c:\\A") || die "Cannot open file directory: $!";
while (defined ($file = readdir(DIR) )) {
next if $file =~ /^\.\.?$/; #This ignores the parent directory that
+readdir returns
open (FILE, "$path\\$file") || die "Cannot open file: $!";
close FILE;
}
elbow | [reply] [d/l] |
Re: HELP!!!! VB code to PERL?
by Anonymous Monk on Jan 08, 2003 at 13:51 UTC
|
I need to open, save and close 1200 very short files.
What do you need to save in them?
Also, you should write your code out in pseudo-code because many people here won't know VB.
Cheers.
| [reply] |
|
|
There actually are a lot of people here who use VB and VB-alikes. Personally I spend most of my paid working time in a language called LotusScript. While not exactly Visual Basic it's very, very close.
Fun Fun Fun in the Fluffy Chair
| [reply] |
|
|
I didn't say there weren't a lot of people here who use VB, I said "many people here won't know VB." :)
At any rate, one posting to a Perl forum should not assume its inhabitants know languages other than Perl (even if it's a moderately safe assumption). This limits the number of people who will even bother looking at the node. Why not take an extra 5 seconds to convert it to pseudo-code? You'll get better replies.
As for your LotusScript situation, I'm sorry to hear that and hope you and your employer have a speedy recovery ;-)
| [reply] |
|
|
|
|
|