1 my ($key, $val) = split(/\t/);
2 $val =~ s/\{\(//;
3 $val =~ s/\)\}//;
4 my @fields = split(/\),\(/, $val);

위와 같은 코드에서  아래와 같은 에러가 발견되었을 경우

Use of uninitialized value in substitution (s///) at

 

초기화 되지 않은  변수가 사용되었다는 경고 메세지인데

이유는 단순하다

1번 라인에 의해 값이 할당된 val에 아무런 것도 들어가지 않았을 경우

즉 split에 의해 리턴되는 값이 없을때 발생하는 경고 메세지이다.

회피하는 방법은 간단히

2,3,4 번행을

if( $val ) 문으로 감싸면 해결된다.

 

 

1 my ($key, $val) = split(/\t/);

2 if( $val )

3 {
4    $val =~ s/\{\(//;
5    $val =~ s/\)\}//;
6    my @fields = split(/\),\(/, $val);

7 } 

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

[PERL] Wide character in print at  (0) 2014.06.20
[PERL] Unmatched ) in regex; marked by <-- HERE in  (0) 2014.06.20
PERL 기본함수 q, qq qw  (0) 2014.06.20
PERL MAP 사용법  (0) 2014.06.20
PERL 파일 열기 읽기 닫기 삭제  (0) 2014.06.20
Posted by 고요한하늘
,

my $temp = "테스트";

 

이런식으로 코드 중간에 한글 문자열을 넣었을때 다음과 같은 경고 메세지가 뜬다.

 

Wide character in print at

 $direction  = encode('utf-8',"이전:어절:");

해결 방법 :

use Encode;

...

 my $temp = encode('utf-8',"테스트");

Posted by 고요한하늘
,

Unmatched ) in regex; marked by <-- HERE in 위와 같은 에러를 만났을때


매칭을 원하는 문자열 앞과 뒤에

\Q문자열\\E 

\Q와 \E로 문자열을 감싼 상태에서 매칭을 하면 에러가 발생하지 않는다.


예를 들면


if( $buffer =~ /\Qpattern\E/ )

{

}

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

PERL Use of uninitialized value in substitution  (0) 2014.06.20
[PERL] Wide character in print at  (0) 2014.06.20
PERL 기본함수 q, qq qw  (0) 2014.06.20
PERL MAP 사용법  (0) 2014.06.20
PERL 파일 열기 읽기 닫기 삭제  (0) 2014.06.20
Posted by 고요한하늘
,
  • 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 고요한하늘
,