perl sprintf 질문...

doodoo의 이미지

펄 함수형에 관한 글을 보고 있습니다.
원 문은 여기...
http://perl.jonallen.info/talks/functionalprogramming
아래 코드는 위 예문중 달력 만드는 프로그램인데...

#!/usr/bin/perl
 
use strict;
use warnings;
 
use Time::Piece;
use Time::Seconds;
 
my $year = 2000;
my $month = 2;
 
 
sub day_iterator{
        my $day = shift;
        return sub{
                my $today = $day;
                $day += ONE_DAY;
                return $today;
        }
}
 
my $first_day = Time::Piece->strptime("$year-$month-01", "%Y-%m-%d");
my $every_day = day_iterator($first_day);
my $days_in_month = iterate_while($every_day, sub {
                $_->mon == $month and $_->year == $year });
 
sub iterate_while {
        my ($iterator, $condition) = @_;
        return sub{
                local $_;
                while (defined ($_ = $iterator->())) {
                        return ($condition->()) ? $_ : undef;
                }
        }
}
 
sub cal_week_function {
        my $next_day = shift;
        my @week;
        return sub{
                while(1){
                        my $day = $next_day->() or return splice @week;
                        @week[$day->day_of_week] = $day->day_of_month;
                        return splice @week if (@week == 7);
                }
        }
}
 
my $calendar_week = cal_week_function($days_in_month);
 
print $first_day->fullmonth, " $year\n\n";
print join '   ', Time::Piece::day_list,"\n";
 
while ( my @week = $calendar_week->()) {
        print join '   ',map{ sprintf("%3s", $_ // '')} @week, "\n";
}

실행은 잘 돼지만....
에서 sprintf("%3s", $_ // '') 의 // 와 '' 의 뜻이 무엇인지 당췌 이해가 안가는군요.
아시는 분 계시나요?
익명 사용자의 이미지

doodoo의 이미지

감사합니다.
세상에...or 연산자가 그렇게 생겨먹었는지 첨 알았습니다.

익명 사용자의 이미지

In fact, it's exactly the same as ||

aero의 이미지

|| 와 // 는 다릅니다.

my $a = 0;
my $var = $a || 'default';
my $var2 = $a // 'default';

에서 $var는 'default'가 되지만 $var2는 0 이 됩니다.
|| 연산자는 단순히 그 좌측이 Perl에서 거짓으로 평가되는 것
(정의되지(defined) 않은 것, 0, ''문자 등) 이면 그 오른쪽 값을 취합니다.
반면 //는 Perl 5.10부터 추가된 것으로 정의되지(defined) 않은 것만 거짓으로 평가하죠

$a // 'default; 는 풀어쓰면
defined $a ? $a : 'default'; 와 같습니다.

Python과 Ruby는 변수를 쓰는 순간 동시에 정의가 되지만 Perl에서는 use strict;를
이용해서 strict모드를 쓰면 모든 변수는 my, our ,local등의 키워드로
사용전에 변수의 스코프를 명확하게 지정해야 합니다.

raymundo의 이미지

뒤에
except that it tests the left hand side's definedness instead of its truth.

이건 왜 안 가져오시고;;;;;

좋은 하루 되세요!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.