sarathi.mh has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I am a student, learning perl. I came across CHOMP operator.
In many references I saw this statement. " This command returns the total number of characters removed from all its arguments"
I am confused with this statement.
1. Is the "value" referred here is the total number of new lines or white spaces which follow that variable
2. If so in which location does it stores this value.
3. Can we access this "value" by using any command.
please correct me if I am wrong.

Replies are listed 'Best First'.
Re: Chomp Command
by toolic (Bishop) on Sep 17, 2010 at 18:39 UTC
    By default, chomp returns the number of newline characters it removed. To view the number returned, store it into a scalar variable, as you would any other function in Perl. You can prove this to yourself by writing some code, such as:
    use strict; use warnings; my $x = "foo\n"; my $count = chomp $x; print "$count\n"; my @arr = ("bar\n", "boo\n"); $count = chomp @arr; print "$count\n"; __END__ Prints out: 1 2

    Before you ask any more questions, write some code, run the code, and see if you understand the results.

      Thanks a lot

Re: Chomp Command
by kennethk (Abbot) on Sep 17, 2010 at 18:43 UTC
    You can find full documentation on chomp in the Perl documentation. But to answer your specific questions:
    1. The value is the total number of characters removed over all list elements passed. If you chomp a scalar, the return value will generally be 0 or 1.
    2. It returns this value. You can see this from the code:

      use strict; use warnings; my @input = <DATA>; # Take in the entire file my $count = chomp @input; print "Characters removed: $count\n"; __DATA__ This is some text Each line has a newline That makes 3 total

      In general, questions like this can be answered quickly by just creating a mock-up script and playing around.

    3. See point 2.

    As side notes, since Perl is case sensitive, CHOMP means something different than chomp, though your intent is obvious enough. As well, note chomp is a function (perlfunc), not an operator (perlop). In particular, chomp is a function with side-effects - it changes the variables you pass it. The difference may explain why you were searching for some special Perl variable (perlvar).

Re: Chomp Command
by TomDLux (Vicar) on Sep 17, 2010 at 20:11 UTC

    If you're trying to figure out a command, write a tiny script. Run it in the debugger: perl -d myscript.pl; You can also run the debugger without any script, to experiment with a command. I use perl -demo because it's memorable, though the 'mo' don't mean anything useful.

    chomp() will remove a trailing newline, but not other characters. If passed an array, it will process each element of the array. So if you care, you can use it to determine whether there used to be a newline in the variable. I don't remember ever seeing anyone care, but the potential is there.

    DB<1> $a = "a \n" DB<2> x $a 0 'a ' DB<3> print chomp $a 1 DB<4> x $a 0 'a ' DB<5> print chomp $a 0 DB<6> x $a 0 'a ' DB<7> @a = ( "abc\n", "def\n", "ghi\n" ) DB<8> x \@a 0 ARRAY(0x8406258) 0 'abc ' 1 'def ' 2 'ghi ' DB<9> print chomp @a 3 DB<10> x \@a 0 ARRAY(0x8406258) 0 'abc' 1 'def' 2 'ghi'

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Chomp Command
by jwkrahn (Abbot) on Sep 17, 2010 at 18:47 UTC
    1. Is the "value" referred here is the total number of new lines or white spaces which follow that variable

    The "value" is the total number of strings removed which are defined by the $/ variable.

    2. If so in which location does it stores this value.

    It does not store a value but it does return a value which can be assigned to a variable.

    3. Can we access this "value" by using any command.

    You can assign it to a variable using the assignment operator.