Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > What am I missing?

Reply
Thread Tools

What am I missing?

 
 
ManieQ
Guest
Posts: n/a
 
      03-05-2004
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
 
Reply With Quote
 
 
 
 
ManieQ
Guest
Posts: n/a
 
      03-05-2004
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

 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      03-06-2004
In article <c2ah6v$8i8$(E-Mail Removed)>, ManieQ
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments