Drew Taylor wrote:
> 
> Does anyone have good evidence either way?

I don't see how C<sub { my $foo ; ...> could ever fail to undef $foo, modulo
bugs in perl.  A hell of a lot of code wouldn't work, then.

My practice is to never init lexicals to undef/(), and only to '' or 0 if
they might be used before being set to a defined value.  Can't see any
reason to = () in perl, where it's set to undef.  C's a different question,
where you often get randomish values instead of perl's nice clean undef.

Side note: stay away from the dreaded C<my $f = blah if blurgh> idiom
unless you really understand it and really need it.

Below are some useless benchmarks.  The first focuses on the performance of
C<my $foo=()>, the second is an attempt to put it in perspective.  Both
on perl 5.6.0.  It's a lot slower to initialize (66%).  My take is that
it's a minor hit compared to the other things you're usually doing, unless
you do it in a tight inner loop.

- Barrie

                     Rate  my $foo = 7 my $foo = () my $foo = undef      my $foo
my $foo = 7     1724138/s           --         -14%            -14%         -71%
my $foo = ()    2000000/s          16%           --              0%         -66%
my $foo = undef 2000000/s          16%           0%              --         -66%
my $foo         5882353/s         241%         194%            194%           --

##########################
#!/usr/local/bin/perl -w

use Benchmark qw( cmpthese timethese ) ;

cmpthese(
   timethese(
      1000000,
      {
         'my $foo = 7'         => sub { my $foo = 7        },
         'my $foo = undef'     => sub { my $foo = undef    },
         'my $foo = ()'        => sub { my $foo = ()       },
         'my $foo'             => sub { my $foo            },
      },
      'none'
   )
) ;

open ME, $0 ; print "\n##########################\n", <ME> ;

-----------------------------------------------------------------------
                     Rate my $foo = undef my $foo = ()  my $foo = 7      my $foo
my $foo = undef  934579/s              --          -2%          -7%         -36%
my $foo = ()     952381/s              2%           --          -6%         -34%
my $foo = 7     1010101/s              8%           6%           --         -30%
my $foo         1449275/s             55%          52%          43%           --

##########################
#!/usr/local/bin/perl -w

use Benchmark qw( cmpthese timethese ) ;

my $bar ;

cmpthese(
   timethese(
      1000000,
      {
         'my $foo = 7'         => sub { my $foo = 7       ; $bar = 12 },
         'my $foo = undef'     => sub { my $foo = undef   ; $bar = 12 },
         'my $foo = ()'        => sub { my $foo = ()      ; $bar = 12 },
         'my $foo'             => sub { my $foo           ; $bar = 12 },
      },
      'none'
   )
) ;

open ME, $0 ; print "\n##########################\n", <ME> ;

Reply via email to