470,636 Members | 1,507 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

What am I missing?

Hi

Probably it is very obvious, but at this moment I cannot solve it. could you
please help me?

(1).

$ perl -e '$a="123234"; $a=~tr/23/ab/; print "$a\n"'
1abab4

Well, it is clear.

(2).

$ perl -e '$a="123234"; $b="ab"; $a=~tr/23/$b/; print "$a\n"'
1$b$b4

I understand this too - no interpolation.

but:
(3).

$ perl -e '$a="123234"; $b="ab"; eval "$a=~tr/23/$b/"; print "$a\n"'
123234

Shouldn't I get same as (1)? So how to do this?

TIA

Mariusz
Jul 19 '05 #1
2 1884
Wiadomo¶ć z dnia pi± 5. marca 2004 19:31, której autorem jest ManieQ,
zawierała:
(3).

$ perl -e '$a="123234"; $b="ab"; eval "$a=~tr/23/$b/"; print "$a\n"'
123234


I know now:
eval "\$a=~tr/23/$b/";

I missed backslash :-(

Mariusz

Jul 19 '05 #2
In article <c2**********@nemesis.news.tpi.pl>, ManieQ
<ma************@idea.net.CIACHNIJpl> wrote:
Hi

Probably it is very obvious, but at this moment I cannot solve it. could you
please help me?

[examples 1 & 2 snipped]
but:
(3).

$ perl -e '$a="123234"; $b="ab"; eval "$a=~tr/23/$b/"; print "$a\n"'
123234

Shouldn't I get same as (1)? So how to do this?


No, you shouldn't. You are sending eval the double-quoted string
"$a=~tr/23/$b/", which, after interpolation, becomes
'123234=~tr/23/ab/', which is an error. Print out the contents of the
variable $@:

perl -e '$a="123234"; $b="ab"; eval "$a=~tr/23/$b/"; print "$a $@\n"'
123234 Can't modify constant item in transliteration (tr///) at (eval
1) line 2, at EOF

Try this, instead, with braces instead of double quotes:

perl -e '$a="123234"; $b="ab"; eval {$a=~tr/23/$b/}; print "$a $@\n"'
1$b$b4

Note: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by David MacQuigg | last post: by
5 posts views Thread by SimpSity | last post: by
1 post views Thread by bradleyc | last post: by
reply views Thread by kris | last post: by
17 posts views Thread by Justin Emlay | last post: by
10 posts views Thread by Protoman | last post: by
5 posts views Thread by le0 | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.