简体   繁体   中英

Perl — how to open / read / print — directory and file content

QUESTION (April 24, 2014): I'm looking for a way to avoid manually revising a webpage to include links to files in a directory, in order to have the contents of those files displayed as code-snippets using Syntaxhighlighter. Short of using a file-manager script (eg, *.cgi or *.php), is there an easy way to evaluate files in a specific directory and include their content in a webpage?

In other words, I'd like to be able to manually add / delete / modify a code-snippet file from a directory and have the script evaluate the directory of snippets (anew) each time the wepage is loaded. As it stands now, I would need to manually edit my webpage to change the name of license.txt if I renamed that file on the server. I am hoping to simply specify a directory (eg, /home/lawlist/public_html/code_snippets ) and have the script evaluate the contents of that directory to populate the webpage with the contents of the files in that directory. The behavior is similar to what a *.cgi or *.php file manager can do.

That's server technology. In Server-Side JavaScript you can use nodejs' fs module and output the content.

"SyntaxHighlighting" is client-side technology, and you can use whatever library you like for code-coloring.

Perl Script -- test.cgi :

#!/usr/bin/perl

use CGI qw(:standard);

print "Content-type: text/html\n\n";

print <<HTML;

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title>Document Title</title>

</head>

<body>

HTML

my $dir = '/home/lawlist/www';

my $query = new CGI;

my $name = $query->param('name');

my $file = $dir . "/" . $name;

opendir(DIR, $dir) or die $!;

while (my $file = readdir(DIR)) {

  next unless (-f "$dir/$file");

  next unless ($file =~ m/\.txt|\.el$/);

  print '<a href="/cgi-bin/test.cgi?name=' . $file . '">' . $file . "</a>" . "<br>" . "\n\n"; }

closedir(DIR);

if ($name) {

  open (DATA, $file) or return $self->print_json_error($self->language('ERR_CANNOT_OPEN', $file->{name}, $!));

  read (DATA, my $file, -s DATA);

  close DATA;

  print '<pre class="brush:  lisp">' . "\n\n" . $file . "\n" . '</pre>'; }

print <<HTML;

</body>

</html>

HTML

exit 0;

.htaccess Configuration

DirectoryIndex index.html index.htm index.php index.cgi

SSLOptions +StdEnvVars

# Customized server error messages:
# ErrorDocument 404 /page.not.found.basic.html

AddHandler cgi-script .pl .cgi
Options +Includes +ExecCGI

AddType text/html .shtml .shtm .html .htm
AddHandler server-parsed .shtml .shtm .html .htm

# if you want to see the list of files in a directory
# without an index.html file, then uncomment the following line:

# Options +Indexes

Options -Indexes

# Options +FollowSymLinks

# Options Indexes Includes FollowSymLinks ExecCGI

Webpage Insert :

<hr COLOR="#CCCCCC" size=1 NOSHADE>

<!--#exec cgi="/cgi-bin/test.cgi"-->

<hr COLOR="#CCCCCC" size=1 NOSHADE>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM