# What am I missing?

Discussion in 'Perl' started by ManieQ, Mar 5, 2004.

1. ### ManieQGuest

Hi

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

(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

ManieQ, Mar 5, 2004

2. ### ManieQGuest

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

ManieQ, Mar 5, 2004

3. ### Jim GibsonGuest

In article <c2ah6v\$8i8\$>, ManieQ
<> wrote:

> Hi
>
> Probably it is very obvious, but at this moment I cannot solve it. could you
>

[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