
그래서 저는 puppet의 불변 변수에 적응하고 있으며 이제 제가 이미 올바른 길을 가고 있는지에 대한 조언이나 설명을 찾고 있습니다.
동기화하고 싶은 디렉터리가 있지만 그 하위 디렉터리도 여러 개 있을 수 있습니다. 그래서 저는 재귀 함수에 전달할 하나 이상의 경로 배열을 구축하고 싶습니다.파일자원. 이상적으로는 다음과 같은 것이 있습니다.
$paths = ['fist/path'] # assume this is provided via class parameter
# this should work, since it's only overriding what was provided from higher scope
if($condition) {
$paths += 'second/path'
}
# this won't fly, since $paths has already been set once locally
if($another_condition) {
$paths += 'third/path'
}
하지만 변수는 불변이기 때문에 그렇게 할 수 없습니다. 지금까지 내가 '최상의' 접근 방식으로 생각해낸 것은 다음과 같습니다.
$paths = ['fist/path']
if($condition) {
$condition_1_path = ['second/path']
} else {
$condition_1_path = []
}
if($another_condition) {
$condition_2_path = ['third/path']
} else {
$condition_2_path = []
}
$paths = concat($paths, $condition_1_path, $condition_2_path)
나는 확실하지 않다연결인수 중 하나에 대해 빈 배열이 제공되었지만 일단 테스트를 기다리는 경우 결과에서 항목을 생략합니다.stdlib를 로드하는 방법을 알아보세요.
어느 쪽이든 이 코드를 보면 나에게는 정말 끔찍한 일이다. 이와 같은 작업을 수행하는 더 깨끗한 방법이 있습니까?
답변1
못생겼지만 이렇게 해봤습니다.
if($condition) {
$condition_1_path = 'first/path'
} else {
$condition_1_path = ''
}
if($another_condition) {
$condition_2_path = 'second/path'
} else {
$condition_2_path = ''
}
# split into array based on whitespace
$mylistofpaths = split("$condition_1_path $condition_2_path", '\s+')