Interpolation problem

Discussion in 'Perl Misc' started by Pal Hulden, Oct 3, 2003.

  1. Pal Hulden

    Pal Hulden Guest

    Say I have this case:

    ----
    #!/usr/bin/perl -w
    $a = "(I leave now";
    $_ = "Goodbye (I leave now)";
    s/$a//;
    print "$_\n";
    ----

    I would like to expect

    Goodbye )

    as an answer. This does obviously not work (get unmatching parenthesis
    error) due to more interpolation that I wanted. I have and have read
    Wall/Christiansen/Schwarz' book 'Programming Perl' but I cannot find
    the solution to the problem in there.

    What is the correct syntax?

    Please help.

    Sincerely
    Pål Huldén
    Gothenburg
    Sweden
    Pal Hulden, Oct 3, 2003
    #1
    1. Advertising

  2. Pal Hulden

    Kien Ha Guest

    Pal Hulden wrote:
    > Say I have this case:
    >
    > ----
    > #!/usr/bin/perl -w
    > $a = "(I leave now";
    > $_ = "Goodbye (I leave now)";
    > s/$a//;
    > print "$_\n";
    > ----
    >
    > I would like to expect
    >
    > Goodbye )
    >
    > as an answer. This does obviously not work (get unmatching parenthesis
    > error) due to more interpolation that I wanted. I have and have read
    > Wall/Christiansen/Schwarz' book 'Programming Perl' but I cannot find
    > the solution to the problem in there.
    >
    > What is the correct syntax?
    >


    perldoc -f quotemeta

    $a = quotemeta $a;
    s/$a//;

    or
    s/\Q$a//;
    Kien Ha, Oct 3, 2003
    #2
    1. Advertising

  3. Pal Hulden wrote:
    > Say I have this case:
    >
    > ----
    > #!/usr/bin/perl -w
    > $a = "(I leave now";
    > $_ = "Goodbye (I leave now)";
    > s/$a//;
    > print "$_\n";
    > ----
    >
    > I would like to expect
    >
    > Goodbye )
    >
    > as an answer. This does obviously not work (get unmatching
    > parenthesis error) due to more interpolation that I wanted. I have
    > and have read Wall/Christiansen/Schwarz' book 'Programming Perl'
    > but I cannot find the solution to the problem in there
    >
    > What is the correct syntax?


    s/\Q$a//;
    ------^^

    Check out perldoc perlre.

    > Gothenburg Sweden


    Really? Me too. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 3, 2003
    #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. Replies:
    8
    Views:
    5,971
    Researcher_123
    Oct 7, 2011
  2. Raj
    Replies:
    2
    Views:
    1,882
  3. Lord0
    Replies:
    1
    Views:
    610
    Lord0
    May 16, 2005
  4. Kevin
    Replies:
    2
    Views:
    99
    Jürgen Exner
    Oct 11, 2004
  5. Justin C
    Replies:
    3
    Views:
    153
    Paul Lalli
    Jul 7, 2007
Loading...

Share This Page