Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I read 5 lines from a file that's read from a directory that's being listed

by peppiv (Curate)
on Dec 21, 2001 at 00:06 UTC ( #133591=perlquestion: print w/replies, xml ) Need Help??
peppiv has asked for the wisdom of the Perl Monks concerning the following question:

I have a folder of text files named as ../archive. I want to list the files in this directory and print the first 5 lines of each file.

As well, I'd like the name of the file to be a link to the original file. Here's what I have so far:
#!/usr/bin/perl -w print "Content-type: text/html\n\n"; open (HTMLFILE, ">../htdocs/template2.html") || die ("open HTMLFILE fa +iled"); print HTMLFILE "<HTML><HEAD><TITLE>Updated Page</TITLE></HEAD><BODY><C +ENTER><FONT SIZE='5' FACE='ARIAL'>List of files</FONT></CENTER><BR><f +ont size='2' face='arial'>\n\n"; opendir(HOMEDIR, "../htdocs/archive") || die ("Unable to open directo +ry"); @files = grep !/^\./, readdir(HOMEDIR); closedir(HOMEDIR); foreach $file (reverse sort @files) { print HTMLFILE "<a href=/archive/$file>$file</a><br>\n"; #the upper part of this code reads/prints the list of files and links +them #the next part of this code reads 5 lines of a file open(STUFF, "../htdocs/archive/$file") || die ("Unable to ope +n file"); while(<STUFF>) { 1 .. 5 ? print HTMLFILE: last; } close (STUFF); } print HTMLFILE "</font></BODY></HTML>"; close(HTMLFILE); print "hopefully this works";

Seperately these pieces of code work, but the integrated version doesn't.

Help! Please!

peppiv
  • Comment on How do I read 5 lines from a file that's read from a directory that's being listed
  • Download Code

Replies are listed 'Best First'.
(Ovid) Re: How do I read 5 lines from a file that's read from a directory that's being listed
by Ovid (Cardinal) on Dec 21, 2001 at 00:39 UTC

    The obvious question: what doesn't work? You don't tell us exactly what the error is. In any event, I cleaned up your code a bit (but just a bit):

    #!/usr/bin/perl -w use strict; use CGI qw/:all/; use URI::Escape; print header; use constant TEMPLATE => "../htdocs/template2.html"; use constant HOME => "../htdocs/archive/"; open HTMLFILE, ">", TEMPLATE or die "Open ".TEMPLATE." failed: $!" +; print HTMLFILE <<END_HTML; <HTML> <HEAD> <TITLE>Updated Page</TITLE> </HEAD> <BODY> <CENTER> <FONT SIZE='5' FACE='ARIAL'>List of files</FONT> </CENTER> <BR> <font size='2' face='arial'> END_HTML opendir HOMEDIR, HOME or die "Unable to open directory '".HOME."': + $!"; my @files = grep !/^\./, readdir(HOMEDIR); closedir HOMEDIR; foreach my $file (reverse sort @files) { print HTMLFILE a({ -href => uri_escape('archive/'.$file)}, esc +apeHTML($file)), br, "\n"; #the upper part of this code reads/prints the list of files an +d links them #the next part of this code reads 5 lines of a file open STUFF, "<", HOME.$file or die "Unable to open ".HOME.$fil +e.": $!"; while(<STUFF>) { 1 .. 5 ? print HTMLFILE: last; } close STUFF; } print HTMLFILE "</font></BODY></HTML>"; close HTMLFILE; print "hopefully this works";

    Note the URI::Escape routine for generating proper HREFs was taken from Dump a directory as links from CGI.

    The only thing that I can reasonably see that might be your problem is this line:

    print HTMLFILE "<a href=/archive/$file>$file</a><br>\n";

    Did you really mean to put that slash in front of archive? That makes that URL relative to root, whereas I think you want it relative to htdocs. I took the slash out in my version. I also left in your HTML (albeit reluctantly). It should probably be converted to CGI.pm's HTML generating functions or put into a *proper* template. I suggest that you look into HTML::Template or Template Toolkit.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      My mistake. What the program does is print the list of the file names and links them to the original file. It also prints the entire file below the link (not just the first 5 lines). Then it goes to the next file and does the same thing.

Re: How do I read 5 lines from a file that's read from a directory that's being listed
by Zaxo (Archbishop) on Dec 21, 2001 at 01:17 UTC

    I will assume you want to produce a static digest of your archive, to be run now and then from a cron job, command line, or admin cgi. Cron and cgi will be better with full paths. Here's how I'd do it:

    #!/usr/bin/perl -w use strict; open (HTMLFILE, "> /full/path/to/htdocs/template2.html") || die $!; print HTMLFILE <<'HTMLHEAD'; <html> <head><title>Updated Page</title></head> <body> <center> <font size="5" face="arial,sans">List of files</font> </center><br/> <font size="2" face="arial,sans"> HTMLHEAD # glob ignores dotfiles for (reverse sort glob("/full/path/to/htdocs/archive/*")) { s!/full/path/to/htdocs/archive/!!; print HTMLFILE qq(<a href="/archive/$_">$_</a><br/>\n); open(STUFF, "< /full/path/to/htdocs/archive/$_") || die #!;; print HTMLFILE ((<STUFF>)[0..4]); close (STUFF); } print HTMLFILE '</font></body></html>'; close(HTMLFILE);
    Hope that helps.

    Update: corrected some typos

    After Compline,
    Zaxo

Re: How do I read 5 lines from a file that's read from a directory that's being listed
by grep (Monsignor) on Dec 21, 2001 at 01:11 UTC
    Well to make it easier on me, I turned this into a command line function. Should be easy to convert back.
    The ideas were solid, but with some improvements you can make this more solid and much easier to debug problems like this.
    #!/usr/bin/perl -w #use CGI; # you should be using CGI.pm #use strict 'nuff said use strict; #You should factor out the directory name since you are using it in mu +ltiple places; my $HOMEDIR = '.'; #you should put the name of the file and a newline for errors (much ea +sier to understand) #you should avoid '||', use 'or' instead. 'or' will be what want most +of the time. # FYI '||' binds tighter than 'or; opendir(HOMEDIR, "$HOMEDIR") or die ("Unable to open directory $HOMEDI +R\n"); my @files = grep !/^\./, readdir(HOMEDIR); closedir(HOMEDIR); #You should learn to use $_, saves loads of typing foreach (reverse sort @files) { #check to see if it is actually a file (you don't want directories +) next if (!(-f "$HOMEDIR/$_")); print "--->$_<---\n"; #same as above open(STUFF, "./$_") or die ("Unable to open file $_\n"); while(<STUFF>) { 1 .. 5 ? print: last; } close (STUFF); print "\n"; }
    HTH
    grep

    grep> cd pub
    grep> more beer
Re: How do I read 5 lines from a file that's read from a directory that's being listed
by TGI (Parson) on Dec 21, 2001 at 00:39 UTC

    I started to clean up your code, and then I realized that I'm not sure what you are trying to accomplish here. Are you trying to print HTML to HTMLFILE (../htdocs/template2.html)? Or are you trying to output the results via CGI?

    P.S. Please, always use strict. You'll be glad you did.


    TGI says moo

Re: How do I read 5 lines from a file that's read from a directory that's being listed
by Juerd (Abbot) on Dec 21, 2001 at 00:39 UTC
    "Doesn't work"? It does nothing? It yields an error?
    Oh, and learn how to use strict. It'll catch many cases of "doesn't work".

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://133591]
Approved by root
help
Chatterbox?
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-04-25 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?