강좌
클라우드/리눅스에 관한 강좌입니다.
기타 분류

Perl 사용법 제공하기

작성자 정보

  • 웹관리자 작성
  • 작성일

컨텐츠 정보

본문

icon01.giftitle.gif


Perl은 누차 말씀드린바와 같이 CGI프로그램제작에 가장쉽게 사용할 수 있는 스크립트언어입니다. 웹호스팅서비스에 가입하시면 업체에서는 이미 Perl을 설치하여두고 가입자가 사용할 수 있는 환경을 갖추고 있습니다. 물론 전용선으로 웹서버를 직접구축하였을 경우에는 Perl 소스를 가져와서 직접 컴파일과 설치를 해줘야하지만 웹호스팅서비스를 이용한다면 이미 설치가 되어있으므로 사용만 하시면됩니다. 이번장에서는 Perl의 실행방법과 실행시켰을때의 에러메시지의 원인에 대해서 알아볼 것이며 덤으로 웹서버를 직접 구축하여 사용하시는 분들을 위해 Perl 설치하는 방법에 대해서도 알아볼 것입니다.

1. 간단한 Perl 프로그램과 쉘모드에서 Perl 실행방법

일반적으로 Perl은 웹상에서 *.cgi파일의 형태로 웹브라우즈를 통해서 실행시키는 것으로 알고있지만 Perl은 원래 시스템의 보고서를 출력하는 용도로 개발된 언어이기 때문에 UNIX의 명령어모드(쉘모드)에서도 바로 실행할 수 있습니다.

아래와 같은 간단한 Perl 프로그램을 예로 들어보겠습니다.

#!/usr/local/bin/perl

$input = <STDIN>;

print($input);

위의 파일이름은 가장간단한 Perl소스이며 파일명은 simple.pl입니다. 원래 Perl의 확장자는 *.pl입니다. *.cgi라는 확장자는 원래 시스템보고서용으로 개발되었던 Perl이 웹에서 CGI프로그램으로 사용되기 시작하면서 확장자 *.cgi라는 것을 사용하기 시작했습니다. 일반적으로 웹에서 사용되는 CGI프로그램은 C로 된 것이든 Perl로 제작된 것이든 표준으로 *.cgi라는 확장자를 사용하기로 묵시적으로 약속이 되어있기 때문입니다.

위의 Perl 프로그램소스를 간단히 설명드린다면 다음과 같습니다.

첫 번째줄 : #!/usr/local/bin/perl은 Perl이 어디에 위치해 있는지를 Perl 인터프리터에게 알려줍니다. Perl의 위치가 다를 경우는 실행이 되지 않기 때문에 대부분 Perl의 경로는 /usr/local/bin/perl이거나 /usr/bin/perl입니다. 만약 서버에 설치되어 있는 Perl의 경로를 모른다면 유닉스 쉘모드에서 'which perl' 또는 'whereis perl'이란 명령어를 사용하여 Perl의 경로를 확인할 수 있습니다. 이 첫줄에 Perl 인터프리터의 경로를 지정해 둔 의미는 지정된 Perl로 하여금 다음줄 이하의 Perl프로그램을 번역하고 실행시키라는 의미입니다.

두 번째 줄 : $input = <STDIN>;에서 <STDIN>은 표준입력장치로 입력을 받으라는 것입니다. 표준입력은 당연히 키보드이며, $input은 표준입력장치(키보드)로 입력된 내용을 input이란 변수에 저장하라는 의미입니다.

세 번째 줄 : print($input);의 의미는 변수 input에 저장된 값을 표준출력장치로 출력하란 의미입니다. 표준출력장치는 모니터이므로 변수 input에 저장된 값을 모니터로 출력이 됩니다. 즉, 위의 Perl프로그램의 종합적인 의미는 표준입력장치(키보드)로 입력받은 데이터를 표준출력장치(모니터)로 출력하란 의미입니다.

그러면 위의 Perl 소스를 UNIX의 쉘모드에서 실행시키는 방법에 대해서 알아보겠습니다.

① 실행방법1

[hosting:/user1/sspark/perl ] perl simple.pl

This is a simple test. ← 키보드로 입력된 값(입력)

This is a simple test. ← 모니터로 출력된값(출력)

'perl simple.pl'이라고 간단히 실행시키시면 됩니다.

② 실행방법2

또다른 실행방법은 simple.pl에 실행권한을 줘서 바로 실행을 시키는 것입니다.

[hosting:/user1/sspark/imsi 25 ] chmod 705 simple.pl

[hosting:/user1/sspark/imsi 26 ] simple.pl

This test is another. ← 키보드로 입력된 값(입력)

This test is another. ← 모니터로 출력된값(출력)

UNIX에서 파일을 바로 실행시키려면 실행권한이 있어야합니다. 따라서 simple.pl이란 파일에 실행권한(705)을 준 후에 바로 실행을 시킨 것입니다.

2. 웹브라우즈로 Perl프로그램 실행하기

Perl프로그램을 웹브라우즈로 실행을 시키려면 반드시 확장자를 *.cgi로 해주셔야합니다. 웹서버에서는 mime.types에 perl인터프리터가 인식할 수 있는 확장자를 나열해두고 있으며 *.pl과 *.cgi이지만 일반적으로 웹에서는 *.cgi를 사용하기로 약속이 되어있으며 이를 지켜주는 것이 불필요한 에러를 줄이는 길입니다.

다음은 websimple.cgi라는 파일명을 가진 perl프로그램을 웹서버에서 실행하기위해 작성한 것입니다.

#!/usr/local/bin/perl

print "Content-type: text/html ";

print "<html> ";

print "<head> ";

print "<TITLE> Perl test program </TITLE> ";

print "</head> ";

print "<body bgcolor=#ffffff> ";

print "<br>웹브라우즈로 실행되는 Perl프로그램입니다. ";

print " ";

print "<br>이 글자가 잘 보인다면 웹브라우즈에서 성공적으로 실행된 것입니다. ";

print "</body></html> ";

첫라인은 앞에서 설명을 드렸으며 두 번째부터 나오는 print라는 Perl문장은 "와 "사이에 있는 문자들을 그대로 출력하라는 의미입니다. 위의 문장을 앞서와 마찬가지로 쉘에서 실행을 시켜보면 다음과 같습니다.

[hosting:/user1/sspark/html/cgi-bin ] websimple.cgi

Content-type: text/html

<html>

<head>

<TITLE> Perl test program </TITLE>

</head>

<body bgcolor=#ffffff>

<br>웹브라우즈로 실행되는 Perl프로그램입니다.

<br>이 글자가 잘 보인다면 웹브라우즈에서 성공적으로 실행된 것입니다.

</body></html>

이 Perl 프로그램을 웹브라우즈를 통해서 실행을 시켜보면 다음과 같이 보이게 됩니다.

perl01.bmp

[그림 perl01]



웹브라우즈에서는 웹서버로부터 받아온 HTML태그를 해석하여 그 결과를 보여주기 때문에 위와 같은 결과를 보여줍니다. 즉, HTML태그의 해석결과를 화면에 보여줍니다.

의외로 간단하지 않습니까?

여기서 한가지 주의하실 것은 웹호스팅서버의 자기계정에 있는 Perl 소스프로그램은 반드시 cgi-bin 디렉토리에 저장해 두셔야 한다는 것입니다. 이는 웹서버에서 cgi를 실행시킬수 있는 권한을 거의 대부분 cgi-bin디렉토리에 두고있기 때문입니다. 물론 꼭 cgi-bin 디렉토리가 아니더라도 실행이 되는 경우도 있습니다. 하지만 웹호스팅업체에서는 서버의 보안과 관리를 위해서 대부분 이렇게 설정해 두고 있기 때문에 지켜두는 것이 또한 프로그램의 에러를 줄이는 길임을 유의하시기 바랍니다.

Perl의 전반적인 문법에 대해서 논하는 것은 너무 광범위할 뿐아니라 이책에서 설명코자하는 범위를 넘어서는 것이므로 Perl 전문서적을 참조해 주십시요.


TOP.gif




4. Perl 설치하기

인터넷전용선으로 독자적인 웹서버를 사용하시는 분들을 위해 Perl소스를 받아와서 설치하는 방법에 대한 설명을 하고 있습니다. 일반적으로 Perl은 다음과 같은 순서로 설치가 됩니다.

 

① PERL 소스 가져오기

② 압축풀기

③ Configuration

④ make

⑤ 시스템에 설치(install)

⑥ Perl 설치확인 및 정보(버전등)를 확인




① PERL 소스 가져오기

[root@ciss9 /root]#

[root@ciss9 /root]# ftp 168.126.62.78

Connected to 168.126.62.78.

220 kornet22 Microsoft FTP Service (Version 4.0).

Name (168.126.62.78:sspark): anonymous

331 Anonymous access allowed, send identity (e-mail name) as password.

Password: ********

230 Anonymous user logged in.

Remote system type is Windows_NT.

ftp> cd pub

250 CWD command successful.

ftp> ls -l

200 PORT command successful.

150 Opening ASCII mode data connection for /bin/ls.

drwxrwxrwx 1 owner group 0 Jul 24 10:18 perl

226 Transfer complete.

ftp>

ftp> cd perl

250 CWD command successful.

ftp> ls -l

200 PORT command successful.

150 Opening ASCII mode data connection for /bin/ls.

-rwxrwxrwx 1 owner group 3679040 Jul 24 10:14 latest_tar.gz

226 Transfer complete.

ftp> bi

200 Type set to I.

ftp> hash

Hash mark printing on (1024 bytes/hash mark).

ftp> get latest_tar.gz ← perl소스가져오기

local: latest_tar.gz remote: latest_tar.gz

200 PORT command successful.

150 Opening BINARY mode data connection for latest_tar.gz(3679040 bytes).

#############################################

#############################################

#############################################

#############################################

#############################################

226 Transfer complete.

3679040 bytes received in 6.2 secs (5.8e+02 Kbytes/sec)

ftp> quit

221 안녕히 가십시요..

[root@ciss9 /root]#

[root@ciss9 /root]# ls -l

total 3612

drwx------ 2 root root 4096 Jun 14 16:08 Mail

drwxr-xr-x 2 root root 4096 Jun 18 11:19 imsi

-rw-rw-r-- 1 root root 3679040 Jul 24 10:19 latest_tar.gz

drwx------ 2 root root 4096 Jun 14 13:12 nsmail

[root@ciss9 /root]# mv latest_tar.gz ~sspark

[root@ciss9 /root]#

[root@ciss9 /root]# cd ~sspark

[root@ciss9 sspark]# ls -l

total 3604

drwxr-xr-x 5 sspark sspark 4096 Jun 14 14:30 Desktop

-rw-rw-r-- 1 root root 3679040 Jul 24 10:19 latest_tar.gz

 

TOP.gif



② 압축풀기

[root@ciss9 sspark]# gzip -d latest_tar.gz ← gzip으로 압축된 파일 압축해제

[root@ciss9 sspark]# ls -l

total 13496

drwxr-xr-x 5 sspark sspark 4096 Jun 14 14:30 Desktop

-rw-rw-r-- 1 root root 13793280 Jul 24 10:19 latest_tar

[root@ciss9 sspark]# tar xvfp latest_tar ← tar로 묶인파일 풀기

perl5.005_03/

perl5.005_03/x2p/proto.h

.........................

....중략..........

..........

perl5.005_03/x2p/util.h

perl5.005_03/x2p/walk.c

[root@ciss9 sspark]# ls -l

total 13500

drwxr-xr-x 5 sspark sspark 4096 Jun 14 14:30 Desktop

-rw-rw-r-- 1 root root 13793280 Jul 24 10:19 latest_tar

drwxr-xr-x 25 sspark users 4096 Mar 29 07:36 perl5.005_03

[root@ciss9 sspark]# cd perl5.005_03

[root@ciss9 perl5.005_03]# ls -l

total 4972

-r--r--r-- 1 sspark users 6111 Jul 30 1998 Artistic

-r--r--r-- 1 sspark users 1037169 Mar 29 07:33 Changes

-r--r--r-- 1 sspark users 7711 Jul 30 1998 Changes5.000

-r--r--r-- 1 sspark users 48081 Jul 30 1998 Changes5.001

-r--r--r-- 1 sspark users 121367 Jul 30 1998 Changes5.002

-r--r--r-- 1 sspark users 4193 Jul 30 1998 Changes5.003

-r--r--r-- 1 sspark users 512719 Jul 30 1998 Changes5.004

-r-xr-xr-x 1 sspark users 270107 Mar 29 01:12 Configure

-r--r--r-- 1 sspark users 12515 Oct 25 1998 Copying

-r--r--r-- 1 sspark users 1492 Mar 28 02:57 EXTERN.h

-r--r--r-- 1 sspark users 65504 Mar 26 14:45 INSTALL

-r--r--r-- 1 sspark users 1192 Mar 28 02:57 INTERN.h

-r--r--r-- 1 sspark users 51832 Mar 29 01:12 MANIFEST

-r-xr-xr-x 1 sspark users 22035 Mar 4 11:35 Makefile.SH

-r-xr-xr-x 1 sspark users 5179 Jul 30 1998 Policy_sh.SH

drwx------ 2 sspark users 4096 Mar 29 07:35 Porting

-r--r--r-- 1 sspark users 4732 Mar 28 02:47 README

-r--r--r-- 1 sspark users 5691 Jul 30 1998 README.amiga

-r--r--r-- 1 sspark users 552 Mar 18 09:05 README.apollo

-r--r--r-- 1 sspark users 1283 Jan 29 12:19 README.beos

-r-xr-xr-x 1 sspark users 3105 Jul 30 1998 cflags.SH

-r-xr-xr-x 1 sspark users 68750 Mar 4 11:35 config_h.SH

-r-xr-xr-x 1 sspark users 11132 Jul 30 1998 configpm

-r--r--r-- 1 sspark users 65535 Oct 29 1998 configure.com

-r-xr-xr-x 1 sspark users 2407 Jul 30 1998 configure.gnu

-r--r--r-- 1 sspark users 11122 Mar 28 02:57 cop.h

-r--r--r-- 1 sspark users 3871 Mar 28 02:57 cv.h

drwx------ 2 sspark users 4096 Mar 29 07:35 cygwin32

-r--r--r-- 1 sspark users 2866 Mar 28 02:57 deb.c

drwx------ 2 sspark users 4096 Mar 29 07:36 djgpp

-r--r--r-- 1 sspark users 36861 Mar 28 02:57 doio.c

-r--r--r-- 1 sspark users 10938 Mar 28 02:57 doop.c

-r--r--r-- 1 sspark users 4146 Oct 31 1998 dosish.h

-r--r--r-- 1 sspark users 10026 Mar 28 02:57 dump.c

-r--r--r-- 1 sspark users 917 Aug 5 1998 ebcdic.c

drwx------ 7 sspark users 4096 Mar 29 07:36 eg

drwx------ 2 sspark users 4096 Mar 29 07:36 emacs

-r--r--r-- 1 sspark users 37575 Jan 17 1999 embed.h

-r-xr-xr-x 1 sspark users 5581 Nov 5 1998 embed.pl

drwx------ 2 sspark users 4096 Mar 29 07:36 vos

-r-xr-xr-x 1 sspark users 2734 Jul 30 1998 writemain.SH

drwx------ 2 sspark users 4096 Mar 29 07:36 x2p

[root@ciss9 perl5.005_03]#

TOP.gif


③ Configuration

[root@ciss9 perl5.005_03]# sh Configure -des -Dcc=gcc ← Configuration하기

First let's make sure your kit is complete. Checking...

Locating common programs...

Checking compatibility between /bin/echo and builtin echo (if any)...

Symbolic links are supported.

Good, your tr supports [:lower:] and [:upper:] to convert case.

Using [:upper:] and [:lower:] to convert case.

3b1 dynix irix_6_1 next_3_0 stellar

aix dynixptx isc next_4 sunos_4_0

altos486 epix isc_2 openbsd sunos_4_1

amigaos esix4 linux opus svr4

apollo fps lynxos os2 ti1500

aux_3 freebsd machten os390 titanos

beos genix machten_2 powerux ultrix_4

bsdos gnu mint qnx umips

convexos greenhills mips sco unicos

cxux hpux mpc sco_2_3_0 unicosmk

cygwin32 i386 mpeix sco_2_3_1 unisysdynix

dcosx irix_4 ncr_tower sco_2_3_2 utekv

dec_osf irix_5 netbsd sco_2_3_3 uts

dgux irix_6 newsos4 sco_2_3_4 uwin

dos_djgpp irix_6_0 next_3 solaris_2

Which of these apply, if any? [linux]

You appear to have ELF support. I'll try to use it for dynamic loading.

If dynamic loading doesn't work, read hints/linux.sh for further information.

Operating system name? [linux]

Operating system version? [2.2.5-15smp]

Build a threading Perl? [n]

What is your architecture name [i686-linux]

AFS does not seem to be running...

Installation prefix to use? (~name ok) [/usr]

Getting the current patchlevel...

Pathname where the private library files will reside? (~name ok)

[/usr/lib/perl5/5.00503]

Where do you want to put the public architecture-dependent libraries? (~name ok)

[/usr/lib/perl5/5.00503/i686-linux]

Other username to test security of setuid scripts with? [none]

Well, the recommended value is *not* secure.

Does your kernel have *secure* setuid scripts? [n]

Do you want to do setuid/setgid emulation? [n]

System manual is in /usr/man/man1.

Which memory models are supported? [none]

Use which C compiler? [gcc]

Checking for GNU cc in disguise and/or its version number...

Now, how can we feed standard input to your C preprocessor...

Directories to use for library searches? [/usr/local/lib /lib /usr/lib]

What is the file extension used for shared libraries? [so]

Checking for optional libraries...

Any additional libraries?

[-lnsl -lndbm -lgdbm -ldb -ldl -lm -lc -lposix -lcrypt]

What optimizer/debugger flag should be used? [-O2]

Any additional cc flags? [-Dbool=char -DHAS_BOOL]

Let me guess what the preprocessor flags are...

Any additional ld flags (NOT including libraries)? [ -L/usr/local/lib]

Checking your choice of C compiler and flags for coherency...

Computing filename position in cpp output for #include directives...

<malloc.h> found.

<stdlib.h> found.

Do you wish to attempt to use the malloc that comes with perl5? [n]

Your system wants malloc to return 'void *', it would seem.

Your system uses void free(), it would seem.

Checking out function prototypes...

Pathname where the public executables will reside? (~name ok) [/usr/bin]

Checking for GNU C Library...

nm probably won't work on the GNU C Library.

Shall I use /usr/bin/nm to extract C symbols from the libraries? [n]

<dld.h> NOT found.

dlopen() found.

Do you wish to use dynamic loading? [y]

Source file to use for dynamic loading [ext/DynaLoader/dl_dlopen.xs]

Any special flags to pass to gcc -c to compile shared library modules?

[-fpic]

What command should be used to create dynamic libraries? [gcc]

Any special flags to pass to gcc to create a dynamically loaded library?

[-shared -L/usr/local/lib]

Any special flags to pass to gcc to use dynamic loading? [-rdynamic]

Build a shared libperl.so (y/n) [n]

Where do the main Perl5 manual pages (source) go? (~name ok) [/usr/man/man1]

What suffix should be used for the main Perl5 man pages? [1]

You can have filenames longer than 14 characters.

Where do the perl5 library man pages (source) go? (~name ok)

[/usr/lib/perl5/5.00503/man/man3]

What suffix should be used for the perl5 library man pages? [3]

Figuring out host name...

Your host name appears to be "ciss9". Right? [y]

What is your domain name? [.ciss9.kornet.net]

What is your e-mail address? [sspark@ciss9.ciss9.kornet.net]

Perl administrator e-mail address [sspark@ciss9.ciss9.kornet.net]

What shall I put after the #! to start up perl ("none" to not use #!)?

[/usr/bin/perl]

Where do you keep publicly executable scripts? (~name ok) [/usr/bin]

Pathname for the site-specific library files? (~name ok)

[/usr/lib/perl5/site_perl/5.005]

Pathname for the site-specific architecture-dependent library files? (~name ok)

[/usr/lib/perl5/site_perl/5.005/i686-linux]

Use the experimental PerlIO abstraction layer? [n]

gconvert NOT found.

gcvt found.

I'll use gcvt to convert floats into a string.

access() found.

<sys/file.h> defines the *_OK access constants.

alarm() found.

Checking whether your compiler can handle __attribute__ ...

bcmp() found.

bcopy() found.

<unistd.h> found.

getpgrp() found.

You have to use getpgrp() instead of getpgrp(pid).

setpgrp() found.

You have to use setpgrp() instead of setpgrp(pid,pgrp).

bzero() found.

Checking to see how big your integers are...

You have void (*signal())().

Checking whether your C compiler can cast large floats to int32.

Checking whether your C compiler can cast negative float to unsigned.

vprintf() found.

Your vsprintf() returns (int).

chown() found.

chroot() found.

chsize() NOT found.

Checking to see if your C compiler knows about "const"...

crypt() found.

cuserid() found.

<limits.h> found.

<float.h> found.

DBL_DIG found.

difftime() found.

<sys/stat.h> found.

make[1]: Entering directory `/home/sspark/perl5.005_03/ext/Errno'

../../miniperl -I../../lib -I../../lib -I../../lib -I../../lib Errno_pm.PL Errno.pm

cp Errno.pm ../../lib/Errno.pm

make[1]: Leaving directory `/home/sspark/perl5.005_03/ext/Errno'

Everything is up to date. 'make test' to run test suite.

make[1]: Leaving directory `/home/sspark/perl5.005_03/x2p'

Now you must run a make.

If you compile perl5 on a different machine or from a different object

directory, copy the Policy.sh file from this object directory to the

new one before you run Configure -- this will help you with most of

the policy defaults.


TOP.gif


④ make

[root@ciss9 perl5.005_03]# make ← 컴파일하기

`sh cflags libperl.a miniperlmain.o` miniperlmain.c

CCCMD = gcc -DPERL_CORE -c -Dbool=char -DHAS_BOOL -O2

`sh cflags libperl.a perl.o` perl.c

CCCMD = gcc -DPERL_CORE -c -Dbool=char -DHAS_BOOL -O2

`sh cflags libperl.a gv.o` gv.c

CCCMD = gcc -DPERL_CORE -c -Dbool=char -DHAS_BOOL -O2


TOP.gif


⑤ 시스템에 설치(install)


[root@ciss9 perl5.005_03]# make install ← 인스톨하기

AutoSplitting perl library

./miniperl -Ilib -e 'use AutoSplit;

../../perl -I ../../lib ../../pod/pod2man --section=1 --official xsubpp > /usr/man/man1/xsubpp.tmp

ln /usr/man/man1/xsubpp.tmp /usr/man/man1/xsubpp.1

unlink /usr/man/man1/xsubpp.tmp

chdir /home/sspark/perl5.005_03

Installation complete

TOP.gif


⑥ Perl 설치확인 및 정보(버전, 위치등)를 확인

[root@ciss9 perl5.005_03]# perl -v

This is perl, version 5.005_03 built for i686-linux

Copyright 1987-1999, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the

GNU General Public License, which may be found in the Perl 5.0 source kit.

Complete documentation for Perl, including FAQ lists, should be found on

this system using `man perl' or `perldoc perl'. If you have access to the

Internet, point your browser at http://www.perl.com/, the Perl Home Page.

설치된 펄의 위치를 확인한다.

[root@ciss9 perl5.005_03]# which perl

/usr/bin/perl

설치된 펄관련 파일들의 위치를 확인한다.

[root@ciss9 perl5.005_03]# whereis perl

perl: /usr/bin/perl5.00503 /usr/bin/perl /usr/man/man1/perl.1

[root@ciss9 perl5.005_03]# cd /usr/bin

[root@ciss9 bin]# ls -l perl

-rwxr-xr-x 2 root root 577337 Jul 24 10:45 perl

이상으로 Perl에 대한 설명을 모두 마칩니다. CGI제작에 필요한 부분을 모두 설명드린 것 같으나 부족할 수 있사오니 Perl의 문법에 관한 사항은 Perl 전문서적을 참조해 주시기 바랍니다.

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,032 명
  • 현재 강좌수 :  35,773 개
  • 현재 접속자 :  180 명