What am I missing?

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

  1. ManieQ

    ManieQ Guest

    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
     
    ManieQ, Mar 5, 2004
    #1
    1. Advertising

  2. ManieQ

    ManieQ Guest

    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
    #2
    1. Advertising

  3. ManieQ

    Jim Gibson Guest

    In article <c2ah6v$8i8$>, ManieQ
    <> 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.
     
    Jim Gibson, Mar 6, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Fox
    Replies:
    0
    Views:
    752
    Paul Fox
    Mar 5, 2004
  2. Mark Newby
    Replies:
    1
    Views:
    1,713
  3. CodeMonkey
    Replies:
    0
    Views:
    418
    CodeMonkey
    Oct 11, 2005
  4. Acceed See
    Replies:
    1
    Views:
    531
    Carl W.
    Apr 22, 2005
  5. Gary Herron
    Replies:
    2
    Views:
    681
    Bruno Desthuilliers
    Jul 4, 2006
Loading...

Share This Page