Perl 5 version 6.0 documentation


  • chop VARIABLE
  • chop LIST
  • chop

    Chops off the last character of a string and returns the character chopped. It's used primarily to remove the newline from the end of an input record, but is much more efficient than s/\n// because it neither scans nor copies the string. If VARIABLE is omitted, chops $_ . Example:

    1. while (<>) {
    2. chop; # avoid \n on last field
    3. @array = split(/:/);
    4. #...
    5. }

    You can actually chop anything that's an lvalue, including an assignment:

    1. chop($cwd = `pwd`);
    2. chop($answer = <STDIN>);

    If you chop a list, each element is chopped. Only the value of the last chop is returned.

    Note that chop returns the last character. To return all but the last character, use substr($string, 0, -1) .