Perl Tutorial : Perl data types
Perl Tutorial : Perl Operators
Perl Tutorial : Perl loops
Perl Tutorial : Perl Conditionals
Perl Tutorial : Perl Subroutines
Perl Tutorial : Perl regex
Perl Tutorial : Perl Files
Perl Tutorial : Perl & Databases
Perl Tutorial : Perl OOPs
Perl Tutorial : Perl signals
Perl Tutorial : Perl threads
Perl Tutorial : Perl Debugging

Introcution to Perl CGI

Introduction to modperl
Home >> perlfunctions >>

Perl function: chomp

Syntax:
chomp VARIABLE
chomp (LIST)
chomp
It's a safer version of chop. Removes any newline character from the end of the string. It actually removes any character that matches the current value of $/ where $/ means a newline.

Chomping a string:
When reading data from user using STDIN, there's a newline at the end of each line. chomp() is demonstrated here.
Example:


Chomping a hash:
When a hash is chomped, newlines from all the values of the hash are removed.
Example:

Chomping an array:
Chomping an array removes newline from the end of all the elements of the array.
Example:
#!/usr/bin/perl
use strict;
use warnings;

my @array = ("apple", "orange\n", "grape\n");
print "Array before chomping:\n";
print "@array\n";
chomp(@array);
print "Array after chomping:\n";
print "@array\n";
Output:
Array before chomping:
apple orange
grape
Array after chomping:
apple orange grape

Perl functions

scalars and strings

lc
ord

regex (regular expressions)

arrays

shift
split

More..

eval
Share |

List of all perl functions
Perl function : split
Perl function: abs
Perl function: chop
Perl function: chr
Perl function: crypt
Perl function: eval
Perl function: hex
Perl function: lc
Perl function: lcfirst
Perl function: length
Perl function: oct
Perl function: ord
Perl function: shift

Perl examples

  • How to use Data::Dumper?
  • How to parse config file?
  • Include external module in perl..
  • Download perl

    download stable version of perl
    Perl 5.10.1
    Perl interview Questions
    sed tutorial
    awk tutorial

    Join us on Google Groups

    Subscribe to perl
    Email:
    Visit this group
    Perl Jobs


    privacy | sitemap | disclaim | contact us
    © 2009 perlhome.com