[Melbourne-pm] Perl chop behaviour

Becky Alcorn becky at unisolve.com.au
Thu Aug 11 22:18:55 PDT 2005


> I believe that chop forces string context on the things its given to chop.
> Thus the array reference is being stringified to the only thing that makes
sense
> in a string context and that is being chopped.  This allows chop to work
as
> expected on both numbers and strings.

That makes some sense.  My only problem with this of course is that before
the chop I had a functioning array and after the chop I don't.  I've
included an example at the bottom of this email to show what I mean.

> How'd you come by this?

I was impressed to see chop affect only the values (and not the keys) when
you pass it a hash.  I wanted to see what it'd get up to with more
complicated structures.  I really expected it to either complain about the
array ref or just ignore it.

Thanks for the info.

Becky

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @array = (
  'fred',
  'jill',
  ['aa', 'bb'],
);

print "1: $array[0]\n";
print "2: $array[1]\n";
print "3: $array[2]\n";
print "3a: $array[2][0]\n";
print "3b: $array[2][1]\n";

print Dumper(\@array) . "\n";

chop(@array);

print "1: $array[0]\n";
print "2: $array[1]\n";
print "3: $array[2]\n";
print "3a: $array[2][0]\n";
print "3b: $array[2][1]\n";

print Dumper(\@array) . "\n";



More information about the Melbourne-pm mailing list