in reply to problem with print content of file

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.

Replies are listed 'Best First'.
RE: Re: problem with print content of file
by tilly (Archbishop) on Aug 07, 2000 at 11:09 UTC
    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

RE: Re: problem with print content of file
by chromatic (Archbishop) on Aug 09, 2000 at 07:55 UTC
    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.