[Melbourne-pm] Pls explain?

Jacinta Richardson jarich at perltraining.com.au
Tue Oct 23 17:34:50 PDT 2007


I don't quite have the right language to explain why in the guts this is
happening, but I'll try.

> #<<<<<<<<< CUT HERE >>>>>>>>>>>>
> #!/usr/bin/perl -w
> #
> use strict;
> use warnings;
> 
> sub get_name_1
> {
>     my ( $n, $backfill ) = @_;
> 
>     my $job_name = "backfill_" if $backfill;

This line above is like writing:

	my $a if $b;

So the lexical variable $a only springs into existence if $b is true.  Therefore
(since $backfill isn't true)

>     $job_name .= "migrate_$n";

this must be the global $job_name; hence the behaviour you see.  There is a
(lame) excuse for why you don't get strict errors and warnings, but I don't
remember what it is, and personally I think it's a bug.

You can fix get_name_1 by doing:

	my $job_name;
	$job_name = "backfill_" if $backfill;
	$job_name .= "migrate_$n";

All the best,

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  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