Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Some problems with my code - Help...

by SuperCruncher (Pilgrim)
on May 26, 2003 at 20:43 UTC ( [id://260886]=note: print w/replies, xml ) Need Help??


in reply to Some problems with my code - Help...

First of all, don't worry about asking questions that you think are stupid.

Your code has a number of problems:

  • The first open call will, AFAIK, always fail because you are chdir()ed to the directory that contains the directory you've just found. So you're trying to write to a file with the same name as a directory. See the section about "the wanted() function" in the File::Find manpage.
  • I don't thint the space in "> $dir" is a good idea - do ">$dir" instead.
  • What is $file? You only use it once as far as I can see. This is where use warnings; can help you.

Anyway, none of this really helps you solve your problem. I would put the directory listings in a separate directory. Take a look at this code (untested):

use strict; use warnings; use File::Find; my $listings_dir = 'c:/listings'; find(\&callback, 'c:/tmp'); sub callback { my $file = $_; # Check to see if $file is a directory if (-d $file) { # Get a list of all the files in the directory (inefficient) # (If you only want the filenames rather than paths as well, # investigate using File::Basename. If you want just the files # in a directory, and directories within the directory, # consider putting grep { -f $_ } before the glob. my (@files) = glob($File::Find::dir . "/$_/*"); # Save this list to a file open LISTING, ">$listings_dir/list$_.txt"; print LISTING join ("\n", @files); close LISTING; } }
Good luck with learning Perl. I strongly recommend learning Perl properly--get yourself a copy of "Learning Perl". Your investment in learning Perl will pay back very quickly.

Replies are listed 'Best First'.
Re: Re: Some problems with my code - Help...
by BazB (Priest) on May 26, 2003 at 21:46 UTC

    There is nothing wrong with open(FH, "> $file");

    perldoc -f open says:

    The filename passed to 2-argument (or 1-argument) form of open() will have leading and trailing whitespace deleted
    I prefer to put some whitespace between the permissions operator and the filename, others do not. It's a style thing, but there is nothing wrong with it.

    Preferably use warnings instead of perl -w. There is a thread here explaining why, but I can't track it down right now. See perldoc perllexwarn for more info.
    You should consider using the strict pragma as well as the warnings pragma.

    Another minor point - I personally find

    my $dir = $entry; open(FILE, "> $dir") or die "Can't open file, $dir: $!";
    slightly grating.
    It's a file you want to create - I initially couldn't figure out if you really wanted opendir to open a directory handle, or if you wanted:
    my $file = $entry; open(FILE, "> $file") or die "Can't open file, $file: $!";
    It's little things like that which make maintenance programmers go insane :-)

    As another poster has already pointed out, you can't go creating files, directories, pipes etc, in the same directory with the same name as an existing file/dir/pipe.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: Re: Some problems with my code - Help...
by star7 (Novice) on May 26, 2003 at 22:11 UTC
    Sorry, $file is wrong.- correct:$entry. thank you for your help. to the other people: thank you, too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://260886]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found