Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

using local::lib requires you to add a line to your ~/.shellrc

eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)

I don't understand what the point of using eval, and encasing the statement in $() is. I also notices that csh doesn't require you to use those. So I'm wondering what the difference is, and whether or not I should use this for generic bourne shell, or zsh.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

perl -I$HOME/perl5/lib/perl5 -Mlocal::lib prints out some shell code. The point of eval $(…) is to execute that code in the context of the current shell. This is typically used to set environment variables. You can't use a subprocess for this as this would only affect the subprocess's environment. You can source a snippet:

. /path/to/snippet-containing-variable-definitions

but that only works if the code that generates the variable values is written in shell. Here that code is written in Perl, so the Perl code generates shell code.

dircolors uses the same technique, as do many other programs.

The shell snippets are generally kept very simple, just variable assignments (with plain strings for values) and export statements, so they are compatible with all Bourne-style shells (including any POSIX shell) and zsh. local::lib is gratuitously incompatible with some decade-old systems as it combines export with assignment (which is permitted by POSIX but not by the original Bourne shell). Csh requires a different syntax; local::lib emits csh syntax if $SHELL contains csh as a substring. Under Windows, local::lib generates the equivalent cmd syntax; because cmd has no equivalent .

share|improve this answer
    
It's probably simplest to understand if you just run the command inside the $() and look at what it prints. That's what the shell will eval. Just to be clear, $() is the new-fangled way of writing backticks (command interpolation)... it's nice because it's easily nestable. Otherwise there's not much difference. –  MadScientist Feb 22 '13 at 5:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.