Reputation: 9035
I am trying to append a path to the end of my PERL5LIB
environment variable, but I am not sure that this variable will always exist. If it doesn't exist I would like to simply initialize it to the value that I am trying to append.
Here is an example that works:
if ( $?PERL5LIB ) then
setenv PERL5LIB ${PERL5LIB}:/some/other/path
else
setenv PERL5LIB /some/other/path
endif
While this works, it still seems pretty clunky since I have to basically write the same line twice. What I would like to do is come up with a more efficient one line solution (possibly using parameter expansion).
Is there a way to consolidate this to one line? (Or a couple lines that don't involve writing out "/some/other/path" multiple times)
For example this could be done in bash:
export PERL5LIB=${PERL5LIB:+${PERL5LIB}:}/some/other/path
Upvotes: 11
Views: 17018
Reputation: 113
If the environment variable is not defined, set it to an empty string. This avoids duplication.
if ( ! $?PERL5LIB ) then
setenv PERL5LIB ""
else
setenv PERL5LIB "${PERL5LIB}:"
endif
setenv PERL5LIB ${PERL5LIB}/some/other/path
Upvotes: 2
Reputation: 9035
For lack of a better answer I will post a slightly improved version of what I had in the question. The following at least eliminates writing out the path twice...
set perl5lib = "/some/other/path"
if ( $?PERL5LIB ) then
setenv PERL5LIB ${PERL5LIB}:${perl5lib}
else
setenv PERL5LIB ${perl5lib}
endif
Although it's only a very slight improvement, at least it's something.
Technically this could be shortened to:
set perl5lib = "/some/other/path"
[ $?PERL5LIB ] && setenv PERL5LIB ${PERL5LIB}:${perl5lib} || setenv PERL5LIB ${perl5lib}
Not the most readable, but I guess that's about as good as I'm going to get.
Possibly more readable? ...I don't really know.
set perl5lib = "/some/other/path"
[ $?PERL5LIB ] \
&& setenv PERL5LIB ${PERL5LIB}:${perl5lib} \
|| setenv PERL5LIB ${perl5lib}
Upvotes: 0