vrempire has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,please help me with my program.
opendir (DIR,$dir) or die "Can't open folder $dir: $!\n"; @files = readdir DIR or die "Couldn't read from $dir: $!\n"; foreach $file (@files) { $/=undef; open MYFILE, "$file" or die "Cannot open :$!\n"; print MYFILE "$file \n"; close FILE; $/="\n"; } closedir DIR;
it returns :permission denied pleaseeeee...

Replies are listed 'Best First'.
Re: problem with print content of file
by ColtsFoot (Chaplain) on Aug 07, 2000 at 10:57 UTC
    Your problem is that you are trying to open the current directory "."
    and the parent directory ".." which are returned in @files when you
    perform the readdir(). The following code will solve your problem
    #!/usr/bin/perl $dir = "/temp"; opendir (DIR,$dir) or die "Can't open folder $dir: $!\n"; @files = readdir DIR or die "Couldn't read from $dir: $!\n"; foreach $file (@files) { if (($file ne ".") and ($file ne "..")) { $/=undef; open MYFILE, "$file" or die "Cannot open :$!\n"; print MYFILE "$file \n"; close FILE; $/="\n"; } } closedir DIR;
    Hope this is of help.
      A few suggestions.

      Localize *DIR before opening a directory. Ditto for *MYFILE.

      Test -f "$dir/$file" to eliminate any other folders in that directory. Else you will see the same problem.

      And finally start your script with "use strict", and throw in "my" as appropriate. Might as well get started on the good habits early. :-)

      Cheers,
      Ben

      If you're going to write to the file, you might want to add one or more > characters in the open statement, as appropriate. Opening the file for reading and then trying to write to it won't work very well, either. (Follow the link to open or type perldoc -f open at the command prompt).

      I think you might want to close MYFILE in the loop, too. :) local is good, as well, as you don't have to reset $/ manually.

(jjhorner)problem with print content of file
by jjhorner (Hermit) on Aug 07, 2000 at 19:51 UTC

    In the interest of feedback:

    I voted you down because you didn't use "warnings" or "strict".

    You have been here for half a month and you haven't picked this up, yet.

    While "warnings" and "strict" are not necessary, they are good common practice by a perl programmer. They make bug fixing easier, and they save time.

    Look at Some things that will make your life easier as a Perl coder.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
      While I agree with your general idea of voting down experienced monks who don't use -w or strict, I can't say that it's always appropriate.

      For example, this post doesn't even have a shebang line. That suggests to me that this is a code snippet and not the entire program. Hence, no -w or strict. Also, sometimes we're just writing a quick throwaway that won't be reused. While I tend to use -w and strict even for my throwaways (because I've been known to shoot myself in the foot), I wouldn't argue that you always need to be that careful on a one-shot deal.

      Despite the above comments, I do accept the general rule about using -w and strict and have voted monks down for that in the past.

      And kudos to you for telling this monk why you voted --.

      Cheers,
      Ovid