What am I missing?

M

ManieQ

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
 
M

ManieQ

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
 
J

Jim Gibson

ManieQ said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top