[Melbourne-pm] Pls explain?

Brendon Oliver brendon.oliver at gmail.com
Tue Oct 23 17:20:25 PDT 2007

Hi all,

I just got bit by some behaviour that I wasn't expecting.  Consider the 
following code (sort of cribbed from a larger module, but illustrates the 

#<<<<<<<<< CUT HERE >>>>>>>>>>>>
#!/usr/bin/perl -w
use strict;
use warnings;

sub get_name_1
    my ( $n, $backfill ) = @_;

    my $job_name = "backfill_" if $backfill;

    $job_name .= "migrate_$n";

    print "Job name: $job_name\n";

sub get_name_2
    my ( $n, $backfill ) = @_;

    my $job_name = "migrate_$n";

    $job_name = "backfill_$job_name" if $backfill;

    print "Job name: $job_name\n";

print "Using get_name1():\n";
for ( qw( foo bar baz tom joe fred ) ) {

print "\nUsing get_name2():\n";
for ( qw( foo bar baz tom joe fred ) ) {
#<<<<<<<<< END CUT >>>>>>>>>>>>

Which produces the following output:
Using get_name1():
Job name: migrate_foo
Job name: migrate_foomigrate_bar
Job name: migrate_foomigrate_barmigrate_baz
Job name: migrate_foomigrate_barmigrate_bazmigrate_tom
Job name: migrate_foomigrate_barmigrate_bazmigrate_tommigrate_joe
Job name: migrate_foomigrate_barmigrate_bazmigrate_tommigrate_joemigrate_fred

Using get_name2():
Job name: migrate_foo
Job name: migrate_bar
Job name: migrate_baz
Job name: migrate_tom
Job name: migrate_joe
Job name: migrate_fred

My puzzlement is:
1) Why in get_name_1() is there no "use of uninitialised variable in 
concatenation" error at the statement:
	$job_name .= "migrate_$n";
since $job_name would be uninitialised when $backfill is false.
2) Why does get_name_1() keep concatenating onto $job_name as if it's a global 
with each iteration, when it is clearly localised 'my $job_name' within the 

I consider myself to be a fairly seasoned user of perl, but I can't explain 
the behaviour of get_name_1() (it's original intent was to produce the output 
shown in get_name_2().  I've shown it to a couple of other like-minded bodies 
here who are also at a loss to explain what's happening.

For the record, /usr/bin/perl -v:
This is perl, v5.8.8 built for i386-linux
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2006, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

(FWIW, tried on both FreeBSD & Mandriva Linux - same perl version).

Ideas anyone?  It's probably something stupidly basic that I've simply 
forgotten... but... someone I showed it to suggested that (horror of 
horrors!) it could be a bug in perl, so on the very remote off-chance that it 
might be, bragging rights reserved :-)


- Brendon.

Our houseplants have a good sense of humous.

 10:01:39 up 8 days, 21:27,  3 users,  load average: 0.02, 0.11, 0.14

More information about the Melbourne-pm mailing list