Raku의 annotations — 콜프레임의 메타데이터를 담은 Map

Raku의 annotations — 콜프레임의 메타데이터를 담은 Map

현재 실행 위치에 대한 부가 정보(소스 파일 경로, 줄 번호 같은)가 필요할 때가 있어요. annotations 메서드는 콜프레임(callframe)이 담고 있는 어노테이션들을 Map으로 돌려주는 메서드예요.

출처: Raku 공식 문서 — routine annotations

시그니처

method annotations()

invocant의 어노테이션, 즉 linefile을 담은 Map을 돌려줘요.

어떻게 쓰는지

say callframe.annotations.^name;                   # OUTPUT: «Map␤»
say callframe.annotations<file> eq callframe.file; # OUTPUT: «True␤»

annotations<file>로 파일 경로를, annotations<line>으로 줄 번호를 꺼낼 수 있어요.

편의 메서드가 더 쉬워요

이렇게 Map을 직접 다루는 대신, 자주 쓰는 정보는 전용 편의 메서드가 있어요. 예를 들어 callframe.fileannotations<file>을 바로 꺼내 주는 줄임 표현이에요.

my $frame = callframe(0);
say $frame.file eq $frame.annotations<file>;

그래서 파일 경로만 필요하다면 annotations를 거치지 않고 file 메서드를 쓰는 게 더 간결해요.