10

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
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
  • P.S i can seee edit revisions to see your last edit stole my answer! – Barkermn01 Jan 31 '18 at 17:21
  • The missing question mark was a typo. The code in the question doesn't work at all without the question mark, but I didn't realize I left it out until I went to write my own answer. – tjwrona1992 Jan 31 '18 at 17:24

2 Answers2

1

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
eternauta3k
  • 103
  • 6
  • So simple... Yet somehow I could not come up with this on my own haha. – tjwrona1992 Oct 01 '18 at 13:01
  • -1 this is wrong. if the variable was not defined, it will now be defined as `:/some/other/path` which includes two paths, the first of which is the empty path. this can be interpreted by some programs as `.` which can be very bad. – user3204459 Oct 19 '18 at 18:21
0

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.

EDIT:

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.

EDIT 2:

Possibly more readable? ...I don't really know.

set perl5lib = "/some/other/path"
[ $?PERL5LIB ] \
  && setenv PERL5LIB ${PERL5LIB}:${perl5lib} \
  || setenv PERL5LIB ${perl5lib}
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
  • I'm accepting this as the answer for lack of a better one... If you have a better answer please feel free to post it and I will change the accepted answer. – tjwrona1992 Jan 31 '18 at 16:23
  • you do know the use of curly brackets is unnecessary and adds complexity, `{` = Start in-line expansion and `}` = End in-line expansion since your not doing any expression you should not be using them and are wasting performance I think you don't know the difference between complexity and size in computers. – Barkermn01 Jan 31 '18 at 17:37
  • @MartinBarker This is more about code duplication than actual complexity and performance. I just don't want to write "/some/other/path" more than once because when you do that for many environment variables the chance of having a typo in one and not knowing about it becomes much greater. – tjwrona1992 Jan 31 '18 at 17:39
  • Also I personally think adding curly brackets improves readability which is more important than performance in this case. – tjwrona1992 Jan 31 '18 at 18:57