[Melbourne-pm] Perl chop behaviour

Jacinta Richardson jarich at perltraining.com.au
Thu Aug 11 22:07:48 PDT 2005


Becky Alcorn wrote:

> Example code is:
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
> 
> my @array = (
>   'fred',
>   'bob',
>   ['aa', 'bb'],
> );
> 
> my $chr = chop(@array);
> 
> print Dumper(\@array) . "\n";
> print "Char: $chr\n";
> 
> And the output from that is:
> 
> $VAR1 = [
>           'fre',
>           'bo',
>           'ARRAY(0x812bfc0'
>         ];
> 
> Char: )
> 
> I can see that it is taking the ')' from the array reference.  Is it a bug
> in my version of Perl (5.8.3), or does that actually make sense at some
> level?  I understand that it is a little bit difficult for Perl to decide
> what to do in this case, but I didn't expect it to do that!

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.

What would you like chop to do when given a reference?  Would that be different
for hash references?  Subroutine references?  Regular expression references?
References to objects, globs, filehandles?  I suspect stringifying them and
chopping that is about as sensible as any other easily implemented option.

How'd you come by this?

	J


-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Melbourne-pm mailing list