# Weirdness with Perl's chop() and chomp()



## socillion (Jul 31, 2009)

For some reason, chop($x) and chomp($x) seriously mess up.

```
#!/usr/bin/perl

my $test = "a string";
print("starting string: '$test'\n");
$test = chomp($test);
print("chomp's output: '$test'\n");

print "\n";

my $test2 = "a newline string\n";
print("starting string: '$test2'\n");
$test2 = chomp($test);
print("chomp's output: '$test2'\n");

print "\n";

my $test3 = "abcdef";
print("starting string: '$test3'\n");
$test3 = chop($test3);
print("chop's output: '$test3'\n");
```

outputs


```
[color="Navy"]$ ./test.pl[/color]
starting string: 'a string'
chomp's output: '0'

starting string: 'a newline string
'
chomp's output: '0'

starting string: 'abcdef'
chop's output: 'f'
```



> $ perl -V
> Summary of my perl5 (revision 5 version 8 subversion 9) configuration:
> Platform:
> osname=freebsd, osvers=7.2-release, archname=i386-freebsd-thread-multi-64int
> ...



Anybody have a clue?


----------



## SirDice (Jul 31, 2009)

Yeah, you're using them wrong.

It's *chop $test3* and *chomp $test1* there's no need for the assignment. 

The assignment actually assigns the chopped off character.


```
$line="newline string\n";
chomp $line;

print "line is now '$line'\n";
```


----------



## socillion (Jul 31, 2009)

awesome, thanks. Been using functional programming languages for too long


----------



## SirDice (Jul 31, 2009)

The standard perl functions can all be found in the perlfunc man page.


----------

