zsh-users
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: why is eval needed?
Date: Sat, 19 Nov 2022 16:48:52 +0000	[thread overview]
Message-ID: <20221119164852.hwujmufa6hn5lotr@chazelas.org> (raw)
In-Reply-To: <d0ef2035-e8e9-00c5-7f53-b16609d96262@eastlink.ca>

2022-11-19 06:34:30 -0800, Ray Andrews:
>  In a script:
> 
>    local level='-L 2'
>    tree $level
>    #eval tree $level
> 
> run it:
> 
>     2 /aWorking/Zsh/Source/Wk 0 $ . testing
>     tree: Missing argument to -L option.
> 
> If I use 'eval' it's fine.

We need eval when we need to evaluate some code stored as a
string dynamically. 

For instance, in perl, you could do:

$code_to_invoke_tree = 'system("tree", ';
$code_to_represent_arguments_for_tree = '"-L", "2"';
$code = $code_to_invoke_tree .
	$code_to_represent_arguments_for_tree . ');'
eval $code;

We have $code containing system("tree", "-L", "2");. That's perl
code which we evaluate with eval.

That looks very silly doesn't it?

Nobody's ever going to do that. Rather, you'd do:

$cmd = "tree";
@args_for_tree = ("-L", "2");
system($cmd, @args_for_tree);

It's exactly the same for shells except that the equivalent
syntax of perl's:

system("tree", "-L", "2");

to invoke tree with -L and 2 as arguments is:

tree -L 2

In shell, you separate arguments with whitespace instead of ",". You
don't need a system() function, because the shell's main job is
to execute commands that's the main thing it does, it would get
old quickly if had to use something like system() for everyone
of them. You don't need quotes around those arguments, because
everything is string in shells, since command arguments are
strings and that's the one thing shells deal with.

Now, you could also do:

shell_code_to_run_tree='tree '
shell_code_to_represent_arguments_for_tree='-L 2'
code=$shell_code_to_run_tree$shell_code_to_represent_arguments_for_tree
eval $code

but that would be equally silly. And like in perl you'd rather do:

cmd=tree
args_for_tree=(-L 2)
$cmd $args_for_tree

But you still need those -L and 2 to be two separate arguments,
hence the list variable.

If you do:

cmd=tree
arg='-L 2'
$cmd $arg

that's the same as:

$cmd = "tree";
$arg = '-L 2';
system($cmd, $arg);

And you're calling tree with only one "-L 2" argument while you
want to call it with 2 arguments: -L and 2.

-- 
Stephane




  parent reply	other threads:[~2022-11-19 16:49 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-19 14:34 Ray Andrews
2022-11-19 14:43 ` Roman Perepelitsa
2022-11-19 17:02   ` Ray Andrews
2022-11-19 17:10     ` Roman Perepelitsa
2022-11-19 18:02     ` Clinton Bunch
2022-11-19 18:18       ` Roman Perepelitsa
2022-11-19 16:48 ` Stephane Chazelas [this message]
2022-11-19 19:12   ` Ray Andrews
2022-11-19 19:50     ` Lawrence Velázquez
2022-11-19 22:21       ` Ray Andrews
2022-11-20  8:55         ` Stephane Chazelas
2022-11-20 13:47           ` Ray Andrews
2022-11-20 15:08             ` Stephane Chazelas
2022-11-20 16:27               ` Ray Andrews
2022-11-20 20:16                 ` Stephane Chazelas
2022-11-20 22:31                   ` Bart Schaefer
2022-11-21  2:13                     ` Ray Andrews
2022-11-20 22:47                   ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221119164852.hwujmufa6hn5lotr@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).