Perl 게시판 소스
ChatGPT
Perl과 CGI를 이용한 간단한 게시판(Bulletin Board System) 예제를 제공하겠습니다. 이 예제는 글 작성, 글 목록 보기, 글 상세 보기를 포함합니다. 또한, 텍스트 파일을 데이터 저장소로 사용하여 간단한 구조를 유지합니다.
1. 환경 설정
1.1 Perl 및 웹 서버 설치
Perl이 설치되어 있어야 합니다. Strawberry Perl을 사용하거나, 대부분의 Unix 계열 시스템에서는 기본적으로 설치되어 있습니다. 웹 서버는 Apache를 사용할 수 있습니다.
1.2 Apache 설정 (Windows 기준)
Apache 설정 파일에 Perl CGI를 활성화해야 합니다. httpd.conf 파일에 다음을 추가합니다:
apache
코드 복사
ScriptAlias /cgi-bin/ "C:/path/to/your/cgi-bin/"
<Directory "C:/path/to/your/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
2. 파일 구조
bash
코드 복사
/bbs
/cgi-bin
bbs.pl
/data
posts.txt
/templates
index.html
post.html
create.html
3. bbs.pl - CGI 스크립트
perl
코드 복사
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
use File::Slurp;
my $data_file = "../data/posts.txt";
my $cgi = CGI->new;
print $cgi->header('text/html');
my $action = $cgi->param('action') || 'list';
if ($action eq 'list') {
list_posts();
} elsif ($action eq 'view') {
view_post($cgi->param('id'));
} elsif ($action eq 'create') {
if ($cgi->request_method eq 'POST') {
create_post($cgi->param('title'), $cgi->param('content'));
} else {
show_create_form();
}
}sub list_posts {
my @posts = read_file($data_file);
print read_file("../templates/index.html");
foreach my $line (@posts) {
my ($id, $title, $created_at) = split /::/, $line;
print qq{<li><a href="?action=view&id=$id">$title</a> ($created_at)</li>};
}
print qq{</ul><a href="?action=create">새 글 쓰기</a></body></html>};
}sub view_post {
my ($id) = @_;
my @posts = read_file($data_file);
foreach my $line (@posts) {
my ($post_id, $title, $content, $created_at) = split /::/, $line;
if ($post_id == $id) {
my $template = read_file("../templates/post.html");
$template =~ s/{title}/$title/;
$template =~ s/{content}/$content/;
$template =~ s/{created_at}/$created_at/;
print $template;
last;
}
}
}sub create_post {
my ($title, $content) = @_;
my $id = time;
my $created_at = localtime;
my $line = join("::", $id, $title, $content, $created_at);
append_file($data_file, "$line\n");
print qq{<meta http-equiv="refresh" content="0; url=?action=list">};
}sub show_create_form {
print read_file("../templates/create.html");
}4. index.html - 글 목록 페이지 템플릿
html
코드 복사
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>글 목록</title>
</head>
<body>
<h1>글 목록</h1>
<ul>
5. post.html - 글 상세 보기 페이지 템플릿
html
코드 복사
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
<p>{content}</p>
<small>Posted on {created_at}</small>
<p><a href="../cgi-bin/bbs.pl?action=list">뒤로</a></p>
</body>
</html>
6. create.html - 글 작성 페이지 템플릿
html
코드 복사
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>새 글 쓰기</title>
</head>
<body>
<h1>새 글 쓰기</h1>
<form method="post" action="../cgi-bin/bbs.pl?action=create">
<label for="title">제목:</label>
<input type="text" id="title" name="title" required>
<br>
<label for="content">내용:</label>
<textarea id="content" name="content" required></textarea>
<br>
<button type="submit">글 작성</button>
</form>
<p><a href="../cgi-bin/bbs.pl?action=list">뒤로</a></p>
</body>
</html>
7. posts.txt - 데이터 파일
이 파일은 처음에는 비어 있어야 하며, 각 게시글은 아래 형식으로 저장됩니다:
arduino
코드 복사
id::title::content::created_at
8. 실행 방법
파일 배치: bbs.pl 스크립트는 웹 서버의 cgi-bin 디렉토리에, templates와 data 디렉토리는 웹 서버의 적절한 위치에 복사합니다.
파일 권한 설정: CGI 스크립트에 실행 권한을 부여합니다.
bash
코드 복사
chmod +x /path/to/cgi-bin/bbs.pl
웹 브라우저: 웹 브라우저에서 http://localhost/cgi-bin/bbs.pl?action=list를 엽니다.
테스트: 게시판을 사용하여 글을 작성하고, 목록을 보고, 상세 보기를 테스트합니다.
참고
Perl과 CGI를 사용한 이 예제는 단순한 기능만을 제공하며, 실제 사용 시에는 보안 및 확장성을 고려한 더 복잡한 시스템이 필요합니다.
텍스트 파일 대신 데이터베이스(DBD::mysql 모듈 사용)를 사용하면 더 나은 성능과 기능을 제공할 수 있습니다.