in reply to help with REGEXP to remove carriage return and caret from end

Another way of doing it is first doing a chop on the variable (this removes the last character of the string, whatever that character may be) and then doing a chomp on the string, which will remove the end-of-line character(s) (one or two characters depending on your system).

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re^2: help with REGEXP to remove carriage return and caret from end
by GrandFather (Saint) on Jul 25, 2006 at 21:37 UTC

    Not such a smart thing generally. Chop will remove the last character regardless of what it was. If it was a multiple character sequence then you have stuffed things for chomp - it won't remove the mutliated line end sequence. If it was a single character then the chomp is not required. In neither case is chomp going to do anything useful following chop.

    chomp first to remove the line end sequence (which may comprise many characters if $/ has been altered) and then chop if you really want to always remove the last character (doesn't happen often actually).


    DWIM is Perl's answer to Gödel
      I don't agree. From the OP's example it is clear his string is built as follows: 'some_name' + 'EOL-character(s)' + '^'.

      chop removes the '^' and then chomp can do its usual job of handling the 'EOL-character(s)'.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        The key word in my reply however was "generally" in the first sentence. Generally, as elaborated in my previous reply, chop is not such a useful function.

        Oh, I agree that it works for OP's problem as stated. But it is a fragile solution. Very likely that doesn't matter in OP's case. But in the interests of general education it is worth noting that chop is unfussy about what it removes and that chomp only removes trailing contents matching $/, and that $/ may contain anything.


        DWIM is Perl's answer to Gödel