Automating System Administration with Perl 서평

| | Comments (0) | TrackBacks (0)
예전에 기대되는 Perl 신간 서적에서




Automating System Administrationwith Perl: Tools to Make You More Efficient 란 책에 대해서 언급한적이 있는데 오늘 책을 구해서 대강 훑어보았다.

이번 2판이 1판과 다른 점은 내용이 보강된 점도 있지만 가장 큰 변화는 뭐니뭐니해도 Perl 코드 형식이 최신경향으로 전부 바뀌었다는 점이다. Perl은 범용언어이지만 시스템관리 쪽에서도 많이 쓰여왔는데 특히 시스템관리쪽 코드들은 과거 Perl 4시절 코드형태를 띄는 경우가 많으며 요즘도 그런 식으로 Perl 코딩하는 사람들을 종종 본다. 일례로 1판과 2판의 같은 기능을 하는 코드를 비교해보자.

<1판 코드>

use User::pwent;

$shells = "/etc/shells";
open (SHELLS,$shells) or die "Unable to open $shells:$!\n";
while(<SHELLS>){
    chomp;
    $okshell{$_}++;
}
close(SHELLS);

while($pwent = getpwent()){
   warn $pwent->name." has a bad shell (".$pwent->shell.")!\n"
       unless (exists $okshell{$pwent->shell});
}
endpwent();


<2판 코드>

use User::pwent;

my $shells = '/etc/shells';
open my $SHELLS, '<', $shells or die "Unable to open $shells:$!\n";

my %okshell;
while (<$SHELLS>) {
    chomp;
    $okshell{$_}++;
}
close $SHELLS;
while ( my $pwent = getpwent() ) {
    warn $pwent->name . ' has a bad shell (' . $pwent->shell . ")!\n"
        unless ( exists $okshell{ $pwent->shell } );
}
endpwent();


현대 Perl코드는 거의 필수적으로 strict와 warnings 프래그마를 사용하여

#!/usr/bin/perl
use strict;
use warnings;

로 코드를 시작한다. 1판의 소스에서 이 두 줄을 추가한다면 $shell = "/etc/shells"; 에서부터

Global symbol "$shells" requires explicit package name at ...

이런 에러가 나면서 동작하지 않을 것이다. 보통 여기서 strict와 warnings를 쓰라고 하는 건 어디서 들어본 것 같은데 쓰면 변수선언부터 에러가 나고 my는 서브루틴 같은 곳에서 렉시컬 변수로 쓴다고 대강 알고 있지만 이것이 정확히 뭘 의미하는지 모르니 1판 같은 소스를 계속 쓰게 되는 악순환을 겪게 된다. 2판의 소스도 strict와 warnings 프래그마를 넣지는 않았지만 넣어도 완벽하게 아무런 문제를 일으키지 않고 동작하는 코드이다. 특히 전역변수랍치고 $shells = "/etc/shells"; 이런 식으로 my,our,local같은 변수 범위선언자를 붙이지 않고 변수를 선언해서 사용하는 Perl코드를 쓰는 사람을 보면 이 사람 Perl 기초를 제대로 이해하지 못하고 나도는 예제코드들 보고 대강 베껴 쓰는구나 하고 생각하게 된다.

1판 소스가 어떻게 2판 소스처럼 바뀌게 되는지 알고 싶으면 최신 스타일 Perl로 개과천선하기를 읽어보시고 이제 제발 1판 같은 Perl코드는 양산하지 말아 주셨으면 좋겠다.

하지만 2판에서도 최신경향 Perl방식을 따르지 않는 부분이 간혹 있는데 그것은 다음과 같은 객체의 메소드를 호출하는 방법이다.

use Win32::Perms;
$acl  = new Win32::Perms();

위 코드는 Win32::Perms라는 객체를 간접호출 방식으로 생성하는 코드인데 마치 Java,C++의 new생성자와 유사한 형식이라 익숙해 보일 수도 있지만 Perl에서는 여러 가지 side effect를 회피하기 위해 다음과 같이 직접호출 방식을 쓰는 게 좋으며 그것이 최신 형식이다.

use Win32::Perms;
my $acl  = Win32::Perms->new();

Perl OOP에 관한 내용은 새로운 Perl OOP로 개과천선하기를 참고


이상 코드형식에 대해서 얘기했지만, 2판은 요즘 새롭게 등장한 Windows용 Strawberry Perl도 언급하는 등 Perl의 최신정보들을 최대한 반영하려는 노력을 보이고 있으며 1판에 비해 시스템관리 전 영역에 걸쳐 많은 내용이 보강되었고 Perl이 다양한 플랫폼에 포팅된 언어 인만큼 UNIX/LINUX뿐만 아니라 Windows 플랫폼에 관해서도 많은 부분을 할애하여 Perl을 플랫폼을 가리지 않는 시스템관리용 언어로 사용할 수 있도록 배려하고 있다.

책이 포함하고 있는 자세한 내용은 다음과 같으며

    * Manage user accounts
    * Monitor filesystems and processes
    * Work with configuration files in important formats such as XML and YAML
    * Administer databases, including MySQL, MS-SQL, and Oracle with DBI
    * Work with directory services like LDAP and Active Directory
    * Script email protocols and spam control
    * Effectively create, handle, and analyze log files
    * Administer network name and configuration services, including NIS, DNS and DHCP
    * Maintain, monitor, and map network services, using technologies and tools such as SNMP, nmap, libpcap, GraphViz and RRDtool
    * Improve filesystem, process, and network security

This edition includes additional appendixes to get you up to speed on technologies such as XML/XPath, LDAP, SNMP, and SQL. With this book in hand and Perl in your toolbox, you can do more with less -- fewer resources, less effort, and far less hassle.

시스템관리자 뿐만 아니라 개발자들에게도 시스템을 이해하는데 아주 도움이 되는 책이라고 생각한다.