Daniel Kurtz wrote: > From: Daniel Kurtz [mailto:[EMAIL PROTECTED] > >>>>Why does @after end up looking exactly like @before? Is there some > > buffering going on here? And if so, how do I clear it? <<< > > Never mind, I figured it out. The file copying operation is another > shell operation (c'mon, I'm a newby, and I've only read as far as > perlopentut!) and the file handle needs to be closed before the second > opendir(). > > daniel >
Ok, but that is why you are here to learn. And a reason why using a shell command to copy a file is a bad idea. You didn't do any error checking, so you didn't know where your problem was. I assume you are using 'strict' and 'warnings'. If you aren't, you need to be, and want to be. If you are reading a book, and it hasn't advised you to do so, get a new book, most likely the Llama. If you are reading the perldocs directly, then I would suggest getting the Llama as it will make for much better use of your time. opendir DIR1, '.' or die "Can't open directory for reading: $!"; In the above, you don't need the extra parens, there is no need to use double quotes unless you are using interpolation and you weren't, so I switched those to singles. And any operation that could fail should be checked for failure, in the above case opendir will return nothing if it fails, so we tack on a message, we include $! to learn *why* it failed. my @before = readdir DIR1; Declare our variables with the proper scope. closedir DIR1; # Execute a command that backs up every file in the directory # with a .bak extension. Here I would suggest using the File::Copy module, as it provides a convenient 'copy' function which is all you are really doing. It is also designed to be as portable as possible, and happens to be standard on newer Perl versions. use File::Copy; foreach my $file (@before) { if (-f $file) { copy($file, "$file\.bak") or die "Can't backup file: $!"; } } opendir DIR2, '.' or die "Can't open directory for reading a second time: $!"; my @after = readdir DIR2; closedir DIR2; No need for shell commands... perldoc -f opendir perldoc -f readdir perldoc -f closedir perldoc File::Copy perldoc -f -e perldoc strict perldoc warnings As an aside, if you must use a shell command you are better off sticking with system, until you need the output which is provided by backticks, only use the piped open form when you need to communicate with the command you are running. http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>