ctp has asked for the wisdom of the Perl Monks concerning the following question:
Inside the foreach loop I have two print statements. I want that output to also go to my OUTFILE. I have tried printing them to the filehandle directly, I have tried pushing the output into an array, I have tried a few other things, all of which produced syntax errors. None of my books have any examples that are even close...not that I can find anyway.#!/usr/bin/perl use warnings; use strict; open (OUTFILE, ">>midterm.html"); my $dir = "/Users/ctp/PERL_work"; my @dirs; my @files; # read all entries from dir and skip '.' and '..' directories opendir (DIRHANDLE, $dir) or die "can't open $dir: $!"; my @list=grep !/^\.\.?\z/, readdir DIRHANDLE; print (OUTFILE "<HTML>\n<HEAD></HEAD>\n<BODY>\n"); # now we sort the list on directory basis foreach my $file (sort {-d "$dir/$b" <=> -d "$dir/$a"} @list) { print "<B>$file</B><BR>\n" if -d "$dir/$file"; print "$file<BR>\n" if -T "$dir/$file"; } closedir (DIRHANDLE); print @dirs; print (OUTFILE "</BODY>\n</HTML>\n"); close (OUTFILE);
#!/usr/bin/perl use warnings; use strict; #open file for output open (OUTFILE, ">>dircontents.html"); #declare $dir variable and set our directory my $dir = "/Users/ctp/PERL_work"; # create dir handle read all entries from directory opendir (DIRHANDLE, $dir) or die "can't open $dir: $!"; #skip "." and ".." directories my @list=grep !/^\.\.?\z/, readdir DIRHANDLE; #print HTML header to our file print (OUTFILE "<HTML>\n<HEAD></HEAD>\n<BODY>\n"); # sort the list on directory basis and close dir handle foreach my $file (sort {-d "$dir/$b" <=> -d "$dir/$a"} @list) { print (OUTFILE "<B>$file</B><BR>\n") if -d "$dir/$file"; print (OUTFILE "$file<BR>\n") if -T "$dir/$file"; } closedir (DIRHANDLE); #print HTML footer to file and close filehandle print (OUTFILE "</BODY>\n</HTML>\n"); close (OUTFILE);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: reading files and directories, part two
by davido (Cardinal) on Jan 08, 2004 at 07:46 UTC | |
Re: reading files and directories, part two
by chimni (Pilgrim) on Jan 08, 2004 at 07:52 UTC | |
Re: reading files and directories, part two
by Hofmator (Curate) on Jan 08, 2004 at 13:18 UTC | |
by ctp (Beadle) on Jan 08, 2004 at 15:34 UTC | |
by Hofmator (Curate) on Jan 08, 2004 at 15:42 UTC |