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

How do I remove a "$" from an input string? I've tried s*\$**g (and various permutations) but Perl is interpreting the $ it finds. I'd just as soon replace it with a space or get rid of it.

Here's a commmon example of the input string:
db1$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_ClientID.sql

Replies are listed 'Best First'.
Re: Remove $
by FunkyMonk (Chancellor) on Aug 27, 2007 at 15:05 UTC
    What makes you think it doesn't work?
    $_ = 'db1$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_ClientID.sql' +; s*\$**g; print; #Output: #db1/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_ClientID.sql

    You're not using $ inside a double-quoted string are you? $/ is a valid Perl identifier (the input record separator).

    update: double quotes stuff
    update^2: gah. $/ --> input record separator

      I guess I am inside a double quoted string. I'm receiving the string from an Excel spreadsheet like this:

      my $cellval = $Sheet->Cells($rownum,"B")->{'Value'};
      chomp ($cellval);

      As noted before it comes out like this:
      db1$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_ClientID.sql

      So how do I then convert that string to literal, remove the $, and flip back to interpreted? (that doesn't read right but I think you get the drift)
        I think there's something else going on. What output do you get if you place a print either side of the substitute? ie

        my $cellval = $Sheet->Cells($rownum,"B")->{'Value'}; print "$cellval\n"; $cellval =~ s*\$**g; print "$cellval\n";

Re: Remove $
by SFLEX (Chaplain) on Aug 27, 2007 at 15:23 UTC
    my $text2 = 'db1$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_Client +ID.sql'; my $hmm = chr(36); $text2 =~ s{\Q$hmm}{}gso;

    That should do it...

    Good Luck ^^
      That looks fabulous but I have no idea what it's actually doing. Can you explain it? Please type slowly. ;-)
        #!/usr/bin/perl my $text2 = 'db1$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_Client +ID.sql'; # chr(36) = $ # http://perldoc.perl.org/functions/chr.html # It's another way to represent $ # my $hmm = chr(36); my $hmm = '$'; # \Q Disable pattern metacharacters until \E # http://perldoc.perl.org/perlreref.html#ESCAPE-SEQUENCES # \Q makes it work =D $text2 =~ s{\Q$hmm}{}g; print "Content-type: text/html\n\n"; print "<html><h1>$text2</h1></html>\n";
        same thing as
        #!/usr/bin/perl my $text2 = 'db1$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_Client +ID.sql'; $text2 =~ s{\Q$}{}g; print "Content-type: text/html\n\n"; print "<html><h1>$text2</h1></html>\n";
        Is that good enough?

        *Edited
Re: Remove $
by dogz007 (Scribe) on Aug 27, 2007 at 15:18 UTC
    If there is no way to stop the interpolation of the $/ in your input, then perhaps you could localize it just within that context. For instance:

    { local $/ = '/'; $text = "db1$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_ClientID +.sql"; print "$text\n"; }
    Prints:

    db1/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_ClientID.sql
    Make sure that this exception doesn't flow over into the rest of your code. You probably don't want a screwed-up new-line character wandering around your script.
      But there is a way... backslashing the $:
      $text = "db1\$/SQL/GEMINI/StoredProcs/pboValidateClientRefNo_ClientID

      If he can change the code to add { local $/ = '/'; ... }, he can change the code to add $.

      I consider your advice very dangerous.

        I understand the danger. That's why I gave the warning at the end. However, notice the context of the string in which he's interested. It's a filepath, which means that it might not have been input by hand, but by some directory walker. In that case he may not be able to insert a '\' or catch the '$' before it interpolates. In that extreme case, he may choose to change the new-line character, but should only do so temporarily. Of course it's dangerous, but it's the bad-boy solutions that perl allows that make it such a wonderful language to use. Document it well, and be smart about it, and it should work fine.