• q() quote
  • qq() double quote
  • qw() quote( split by space )
  • qqw() double quote( split by space )
  • double quote 안에서는 interpolation 가능


Posted by 고요한하늘
,

 MAP 함수는 배열을 INPUT으로 받아서 배열을 OUTPUT으로 만든다.



my @OUTPUT = map { PROCESSING } @INPUT

ex> 


$input 이라는 스칼라 변수를 공백 단위로 분리한다. split의 리턴 값은 배열이기 때문에
map함수의 입력조건을 만족한다.
map함수에 배열이 입력으로 들어가더라도 하나씩 처리하게 되는데 그 값은 $_에 저장되어 넘어온다.
$_에 대한 임의의 조작을 하고나면 keyword_with_clue라는 배열에 차곡차곡 저장된다.

my @keyword_with_clue = map { $name.' '.$_ } split(/\s+/, $input );


Posted by 고요한하늘
,

파일 삭제


if ( -e $output_file ) {
  unlink $output_file or die "cannot remove output file: $!";
}


파일 열기 , 닫기

open(INPUT, $keywords_file) or die "cannot open input file: $!";
close(INPUT);

파일 읽기
while ( <INPUT> ) {
  chomp; $total++;
  my ($unique_id, $named_entity, $clues) = split(/\t/);
 
  #next unless exists $top_persons->{$named_entity};
  $valid++;
  $persons{$named_entity} = [] unless exists $persons{$named_entity};
  push @{$persons{$named_entity}}, { id=>$unique_id, clues=>$clues };
}


'프로그램밍언어 > PERL' 카테고리의 다른 글

PERL 기본함수 q, qq qw  (0) 2014.06.20
PERL MAP 사용법  (0) 2014.06.20
PERL SUB 모듈에서 파라미터 받기  (0) 2014.06.20
PERL 옵션 설정  (0) 2014.06.20
PERL 변수 선언 및 특이 표현  (0) 2014.06.20
Posted by 고요한하늘
,

SHIFT를 사용해서 하나씩 assign 할수도 있고

@_를 가지고 하나씩 변수를 할당할수도 있다.


my $keywords_file = shift;
my $output_file   = shift  || "news.output.txt";

my $keywords_file=@_[0]
my $keywords_file=@_[1]


'프로그램밍언어 > PERL' 카테고리의 다른 글

PERL MAP 사용법  (0) 2014.06.20
PERL 파일 열기 읽기 닫기 삭제  (0) 2014.06.20
PERL 옵션 설정  (0) 2014.06.20
PERL 변수 선언 및 특이 표현  (0) 2014.06.20
PERL 선언시 모듈 import  (0) 2014.06.20
Posted by 고요한하늘
,

HELP 옵션과 VERBOSE 설정을 예로



my $options = {}; # `my' defines a local variable.
getopts("hv", $options);
$debug_mode = $options->{v};
 
sub print_usage_and_exit {
  my $fh = shift || *STDOUT;
  my $message = shift;
 
  print $fh $message, "\n" if defined $message;
  print $fh <<END;
Usage: $0 [-h] [-v] [-v] <keywords_file> [<output_file>]
 keywords_file             - input filename which lists keywords
 output_file               - output filename to write
 
 Options -
  -h          shows this help message
  -v          enables verbose mode
END
  exit;
}
 
print_usage_and_exit if $options->{h};

'프로그램밍언어 > PERL' 카테고리의 다른 글

PERL MAP 사용법  (0) 2014.06.20
PERL 파일 열기 읽기 닫기 삭제  (0) 2014.06.20
PERL SUB 모듈에서 파라미터 받기  (0) 2014.06.20
PERL 변수 선언 및 특이 표현  (0) 2014.06.20
PERL 선언시 모듈 import  (0) 2014.06.20
Posted by 고요한하늘
,

3개의 변수에 대해서 초기화

my ($valid, $total, $request) = (000);

해시 변수 초기화
my %persons = ();


배열 초기화
my @array1 = ( 123456789);
my @array2 = ( 1..9);
 
특이표현1 : 배열 개수
scalar @{$persons{$a}} <-배열개수

특이표현2 : 대소 비교 연산자
<=> 비교연산자 리턴값 : -101


'프로그램밍언어 > PERL' 카테고리의 다른 글

PERL MAP 사용법  (0) 2014.06.20
PERL 파일 열기 읽기 닫기 삭제  (0) 2014.06.20
PERL SUB 모듈에서 파라미터 받기  (0) 2014.06.20
PERL 옵션 설정  (0) 2014.06.20
PERL 선언시 모듈 import  (0) 2014.06.20
Posted by 고요한하늘
,
#!/usr/bin/perl
 
#엄격한 문법 검사
use strict;  

#경고 메세지 출력
use warnings;

# UTF8 기본 문자열 사용
use utf8;
 
# 특정 파일의 경로를 찾을때
use FindBin;
FindBin::again();

# 옵션 파서
use Getopt::Std;

# HTTP 유틸리티
use LWP::Simple qw(!head);

# XML 파서
use XML::Simple;

# 여러가지 변수 DEBUGGING
use Data::Dumper


'프로그램밍언어 > PERL' 카테고리의 다른 글

PERL MAP 사용법  (0) 2014.06.20
PERL 파일 열기 읽기 닫기 삭제  (0) 2014.06.20
PERL SUB 모듈에서 파라미터 받기  (0) 2014.06.20
PERL 옵션 설정  (0) 2014.06.20
PERL 변수 선언 및 특이 표현  (0) 2014.06.20
Posted by 고요한하늘
,