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

socillion

New Member


Messages: 7

For some reason, chop($x) and chomp($x) seriously mess up.
Code:
#!/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

Code:
[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
uname='freebsd dbox 7.2-release freebsd 7.2-release #0: fri may 1 08:49:13 utc 2009 root@walker.cse.buffalo.edu:usrobjusrsrcsysgeneric i386 '
config_args='-sde -Dprefix=/usr/local -Darchlib=/usr/local/lib/perl5/5.8.9/mach -Dprivlib=/usr/local/lib/perl5/5.8.9 -Dman3dir=/usr/local/lib/perl5/5.8.9/perl/man/man3 -Dman1dir=/usr/local/man/man1 -Dsitearch=/usr/local/lib/perl5/site_perl/5.8.9/mach -Dsitelib=/usr/local/lib/perl5/site_perl/5.8.9 -Dscriptdir=/usr/local/bin -Dsiteman3dir=/usr/local/lib/perl5/5.8.9/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Ui_malloc -Ui_iconv -Uinstallusrbinperl -Dcc=cc -Duseshrplib -Dinc_version_list=none -Dccflags=-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.9/BSDPAN" -Doptimize=-O2 -fno-strict-aliasing -pipe -Dd_dosuid=define -Ui_gdbm -Dusethreads=y -Dusemymalloc=n -Duse64bitint -Dusesitecustomize'
hint=recommended, useposix=true, d_sigaction=define
usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=define use64bitall=undef uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.9/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -I/usr/local/include',
optimize='-O2 -fno-strict-aliasing -pipe',
cppflags='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.9/BSDPAN" -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -I/usr/local/include'
ccversion='', gccversion='4.2.1 20070719 [FreeBSD]', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=4, prototype=define
Linker and Libraries:
ld='cc', ldflags ='-pthread -Wl,-E -L/usr/local/lib'
libpth=/usr/lib /usr/local/lib
libs=-lm -lcrypt -lutil
perllibs=-lm -lcrypt -lutil
libc=, so=so, useshrplib=true, libperl=libperl.so
gnulibc_version=''
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' -Wl,-R/usr/local/lib/perl5/5.8.9/mach/CORE'
cccdlflags='-DPIC -fPIC', lddlflags='-shared -L/usr/local/lib'


Characteristics of this binary (from libperl):
Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP
USE_64_BIT_INT USE_FAST_STDIO USE_ITHREADS
USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API
USE_SITECUSTOMIZE
Locally applied patches:
defined-or
Built under freebsd
Compiled at Jul 12 2009 18:34:35
@INC:
/usr/local/lib/perl5/5.8.9/BSDPAN
/usr/local/lib/perl5/site_perl/5.8.9/mach
/usr/local/lib/perl5/site_perl/5.8.9
/usr/local/lib/perl5/5.8.9/mach
/usr/local/lib/perl5/5.8.9
.
Anybody have a clue?
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 7,372
Messages: 29,914

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.

Code:
$line="newline string\n";
chomp $line;

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

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 7,372
Messages: 29,914

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