On Sun, Aug 29, 2010 at 07:15, Kaushal Shriyan <kaushalshri...@gmail.com> wrote:
> Hi
>
> Can someone please explain me with an example of the usage chomp ()
> builtin function in perl.
snip

The chomp function removes whatever is in the $/ variable from the
argument passed in (or $_ if no argument is passed in).  The default
value of $/ is "\n".  It is often used after reading a line to remove
the newline ("\n"):

open my $fh, "<", "/etc/passwd"
    or die "could not open passwd: $!";

while (my $line = <$fh>) {
    chomp $line;
    print "I read [$line]\n";
}

Note, it will only remove the last string that is equal to $/, so this

{
    local $/ = "abc"
    my $string = "fooabcbarabcbazabc";
    chomp $string;
    print "$string\n";
}

will print "fooabcbarabcbaz\n";

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to