기본
Hello World
use strict/warnings가 모범 사례입니다
#!/usr/bin/perl
use strict;
use warnings;
print "Hello, World!\n";
# variables: scalar ($), array (@), hash (%)
my $name = "Alice";
my @nums = (1, 2, 3);
my %ages = (Alice => 30, Bob => 25);
print "Name: $name\n";주석
=pod =cut는 여러 줄 주석용
#!/usr/bin/perl
# This is a single-line comment
print "Hello\n"; # inline comment
=pod
This is a multi-line
comment using POD (Plain Old Documentation)
=cut
print "After POD\n";스크립트 실행
perl 명령줄 옵션
my $str = "Hello";
my $num = 42;
my $float = 3.14;
my $bool = 1; # true (0, "", "0", undef are false)
# String repetition
my $line = "-" x 20;
# String concatenation
my $greeting = "Hello, " . $str;
print "$greeting\n"; # Hello, Hellosay 함수
say는 자 동으로 줄바꿈을 추가합니다
my $name = "Alice";
my $age = 30;
print "Name: $name\n"; # interpolates
print 'Name: $name\n'; # literal (no interpolation)
# Disambiguate variable name with braces
my $fruit = "apple";
print "I have many ${fruit}s.\n"; # I have many apples.
# Array interpolation (joins with $")
my @arr = (1, 2, 3);
print "Array: @arr\n"; # Array: 1 2 3shebang
env perl이 더 이식성이 좋습니다
use strict;
use warnings;
# Without strict, this would create a global:
# $foo = 42; # ERROR under strict
my $foo = 42; # OK with my
# Warnings catch common mistakes
my $undef_var;
# print $undef_var; # warns: uninitialized value
# Modern bundle (v5.36+ enables strict, warnings, features)
use v5.36;
# Disable specific warnings locally
no warnings 'uninitialized';say & features
say is like print but appends a newline. Enable features with 'use feature' or a version bundle like 'use v5.36'. Signatures give named parameters instead of unpacking @_.
use v5.10;
use feature 'say';
say "Hello!"; # adds newline automatically
say "No need for \n";
# Equivalent to:
print "Hello!\n";
# Signatures (v5.20+, not default)
use feature 'signatures';
no warnings 'experimental::signatures';
sub greet ($name) {
say "Hi, $name";
}스칼라
스칼라 변수
$ 접두사는 스칼라를 나타냅니다
my $sum = 5 + 3; # 8
my $diff = 10 - 4; # 6
my $prod = 6 * 7; # 42
my $div = 10 / 3; # 3.3333...
my $int = int(10 / 3); # 3
my $mod = 10 % 3; # 1
my $pow = 2 ** 10; # 1024
# Augmented assignment
my $x = 5;
$x += 3; # $x = 8
$x **= 2; # $x = 64
$x++; # $x = 65
$x--; # $x = 64문자열 연산
일반 문자열 함수
my $a = "Hello";
my $b = "World";
# Concatenation
my $combined = $a . ", " . $b; # Hello, World
# Repetition
my $line = "=" x 30;
# Augmented concatenation
my $str = "abc";
$str .= "def"; # abcdef
# Numeric vs string comparison
print "match" if "abc" eq "abc"; # string equal
print "match" if 123 == 123; # numeric equal문자열 보간
이중 따옴표는 보간, 단일 따옴표는 하지 않음
my $x; # undef
print "undef\n" if !defined $x;
$x = undef; # explicitly undef
undef $x; # another way
# Defined-or operator
my $val = $x // "default";
# Warning if used as string:
# print $x; # Use of uninitialized value
if (defined $val) {
print "Defined: $val\n";
}숫자 연산
숫자 함수
my $name = "Alice";
# q() = single quote (no interpolation)
my $s1 = q(Hello $name); # Hello $name
# qq() = double quote (interpolation)
my $s2 = qq(Hello $name); # Hello Alice
# qw() = quoted words (produces a list)
my @words = qw(apple banana cherry);
# equivalent to ("apple", "banana", "cherry")
# Custom delimiters
my $s3 = q{don't worry};
my $s4 = q!bang!;undef
undef는 정의되지 않음을 의미합니다
my $name = "Alice";
# Single-quote style (no interpolation)
my $text = <<'END';
Hello $name,
This is literal.
END
# Double-quote style (interpolation)
my $msg = <<"EOF";
Hello $name,
Interpolated here.
EOF
print $msg;
# Indented heredoc (v5.26+)
my $code = <<~EOT;
line 1
line 2
EOTBoolean Truthiness
Only undef, 0, '', and '0' are false; even '00' and '0.0' are true! Use || for truthy defaults and // for defined defaults. References (even empty arrays) are always true.
# False values: undef, 0, "", "0"
# Everything else is true
print "true\n" if 1; # true
print "true\n" if "hello"; # true
print "true\n" if "0"; # NOT printed (false!)
print "true\n" if ""; # NOT printed (false)
print "true\n" if []; # true (reference is true)
# Logical operators
my $x = 0 || "default"; # default (|| tests truthiness)
my $y = 0 // "default"; # default (// tests definedness)
my $z = "a" && "b"; # b (returns last evaluated)배열
배열 연산
@ 접두사는 배열, $는 요소 접근
my @nums = (1, 2, 3, 4, 5);
my @strs = qw(a b c);
my @mixed = (1, "two", 3.14);
# Access by index (0-based)
print $nums[0]; # 1
print $nums[-1]; # 5 (last element)
# Modify an element
$nums[0] = 100;
# Range operator
my @range = (1 .. 10); # 1 to 10
# Length in scalar context
my $count = scalar @nums; # 5
my $count2 = @nums; # 5배열 함수
push/pop/unshift/shift
my @stack = (1, 2, 3);
# push/pop operate on the END
push @stack, 4, 5; # (1, 2, 3, 4, 5)
my $last = pop @stack; # 5
# unshift/shift operate on the START
unshift @stack, 0; # (0, 1, 2, 3, 4)
my $first = shift @stack; # 0
# @stack is now (1, 2, 3, 4)
print "@stack\n";
# splice is the general-purpose tool
splice(@stack, 1, 0, 'x'); # insert at index 1배열 슬라이싱
@ 슬라이스는 리스트를 반환합니다
my @nums = (10, 20, 30, 40, 50);
# Array slice (returns a list)
my @slice = @nums[1, 3]; # (20, 40)
my @range = @nums[0 .. 2]; # (10, 20, 30)
# Modify multiple elements via slice
@nums[1, 3] = (200, 400);
# Last index
my $last_idx = $#nums; # 4
# Negative indices count from the end
print $nums[-1]; # 50
print $nums[-2]; # 400정렬
<=> 숫자 비교, cmp 문자열 비교
my @arr = (1, 2, 3, 4, 5);
# splice ARRAY, OFFSET, LENGTH, LIST
my @removed = splice(@arr, 1, 2); # removes (2, 3)
# @arr is now (1, 4, 5)
# Insert without removing (LENGTH = 0)
splice(@arr, 1, 0, 'a', 'b'); # (1, 'a', 'b', 4, 5)
# Replace elements
splice(@arr, 0, 2, 'x'); # replace first 2 with 'x'
# Remove everything from offset to end
my @rest = splice(@arr, 2);map과 grep
map은 변환, grep은 필터
my @nums = (3, 1, 4, 1, 5, 9);
# Default sort is lexicographic (string)
my @s1 = sort @nums;
# Numeric ascending
my @s2 = sort { $a <=> $b } @nums;
# Numeric descending
my @desc = sort { $b <=> $a } @nums;
# String sort
my @words = qw(banana apple Cherry);
my @alpha = sort { lc($a) cmp lc($b) } @words;
# Schwartzian transform (sort by computed key)
my @sorted = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_, length($_)] } @words;Array length & scalar context
An array's behavior depends on context: scalar context gives length, list/string context gives elements joined by $" (default space). Use scalar() to force scalar context when needed.
my @arr = (1, 2, 3, 4, 5);
# Scalar context returns length
my $len = @arr; # 5
print "Length: $len\n";
# String context joins with $"
print "Array: @arr\n"; # 1 2 3 4 5
# Change separator
local $" = ", ";
print "@arr\n"; # 1, 2, 3, 4, 5
# Last index
print "Last index: $#arr\n"; # 4
# Force scalar context
print scalar(@arr); # 5해시
해시 연산
% 접두사는 해시를 나타냅니다
my %ages = (Alice => 30, Bob => 25, Carol => 35);
# Fat comma => auto-quotes the left side
my %h2 = (name => 'Alice', age => 30);
# Same as list of pairs:
my %h3 = ('name', 'Alice', 'age', 30);
# Access / modify a value
print $ages{Alice}; # 30
$ages{Dave} = 40; # add
$ages{Alice} = 31; # modify
# Delete a key
delete $ages{Bob};해시 반복
each/keys/values