varunparihar has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to replace some string with "jdbc:oracle:thin:@jnpodb:1521:tiborcl" but as a result, am getting the output as "jdbc:oracle:thin::1521:tiborcl" after replacement. Can someone tell me how to deal with "@" ? Thanks in Advance!

Replies are listed 'Best First'.
Re: Perl replace
by Corion (Patriarch) on May 25, 2015 at 11:50 UTC

    First of all, add use strict; to the top of your script. This will enable Perl to warn you that it finds an array (@jnpodb).

    Then, read perlop (and perlre) on how to escape things in regular expressions and strings.

    You need to escape the @ by quoting it with a backslash:

    \@jnpodb
Re: Perl replace
by davido (Cardinal) on May 25, 2015 at 17:54 UTC

    @jnpodb is seen by Perl to be an array. And the righthand side of a substitution is subject to double-quote-like interpolation. Therefore, Perl interpolates the contents of @jnpodb into your replacement string. Since that variable exists nowhere else, it's empty, and consequently what gets interpolated is the empty list which is upgraded to an empty string.

    If use warnings were in use, you would get a warning about interpolating an undefined value. And if use strict were in use, you would get a fatal compiletime error stating that @jnpodb wasn't pre-declared in scope. More clues about errors is better than fewer clues, so always use strict and warnings.

    And as it goes with double-quotish interpolation, you just need to escape the sigil. Thus, this works:

    s/whatever/jbdc:oracle:thin:\@jnpodb:1521:tiborcl/

    Dave

Re: Perl replace
by Laurent_R (Canon) on May 25, 2015 at 19:07 UTC