NOWS/Perl
day 1.
emzei
2016. 1. 5. 23:00
nutshell 시리즈 따라서 공부하기, 1일차
Perl Script : Perl 문과 정의의 묶음을 파일에 집어 넣은 것
- Perl 시작
#!/usr/bin/perl
- 주석 : 파운드기호(#) 이용, (No multiple-line comment)
- Examples : Hello World
#!/usr/bin/perl -w
print ("Hello, world!\n");
- #!/usr/bin/perl -w
- -w : warning (추가적인 경고메시지 생성)
- print(); : 내장함수
- Examples : 스칼라 변수를 이용한 Hello World
#!/usr/bin/perl -w
print "What is your name?";
$name=<STDIN>;
chomp($name);
print "Hello, $name\n";
- $name : name 이란 이름을 갖는 스칼라 변수
- <STDIN> 으로 입력받은 변수 값은 c의 string 처럼 읽어오게 됨 (Ex. "abcd\n" ) 식으로 읽어오게 됨
- chomp() : 문자열의 \n 제거하는 기능
- Examples : If-else stmt 이용하기
#!/usr/bin/perl -w
print "What is your name?";
$name=<STDIN>;
chomp($name);
if($name eq "Randal"){
print "Hello, Randal! How good of you to be here!\n";
}else{
print "Hello, $name\n"; #common greeting
}
- eq 연산자 : equal
- Examples : while stmt 이용하기
while($guess ne $secretword)
{
print "Wrong, Try again! what is the secret word? ";
$guess=<STDIN>;
chomp($guess);
}
- ne 연산자 : not equal
- Examples : 리스트 / 배열
@words=("minji","min","ji");
# same as @words=qw(minji min ji);
...
$i=0;
if( $words[$i] eq $guess )
...
- 배열 : @로 시작하는 변수 (@로 시작하기때문에 스칼라 변수와 구분 가능)
- 배열 선언 방법 2가지 (위 네모박스 참고)
- 개별적인 선언 혹은 qw()연산자 이용
- words[0] = "minji", words[1] = "min", words[2] = "ji"
- Examples : 해시
%words = qw(
alice strange
bob rice
clare blare
);
$person = "alice";
$words{$person};
$words{bob};