in reply to Re: Style Question: Throwaway Objects
in thread Style Question: Throwaway Objects

Just to nitpick: you're leaving a lexical $parser around that the condensed form doesn't, so it is actually equivalent to
my $book; { my $parser = Spreadsheet::ParseExcel->new() $book = $parser->Parse($filename); }
or, as I'd prefer,
my $book = do { my $parser = Spreadsheet::ParseExcel->new() $parser->Parse($filename); };
logically followed by
my $book = do { my $parser = Spreadsheet::ParseExcel->new() $parser }->Parse($filename);
which is (and here's the important step) obviously equivalent to my $book = do { Spreadsheet::ParseExcel->new() }->Parse($filename); which is trivially equivalent to my $book = Spreadsheet::ParseExcel->new()->Parse($filename); Tightly scoping with a block instead is generally better practice. The garbage collector will clean up after you without any extra effort on your side anyway.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: Style Question: Throwaway Objects
by dws (Chancellor) on Dec 09, 2002 at 02:44 UTC
    Just to nitpick: you're leaving a lexical $parser around that the condensed form doesn't

    Well, yes. But compared to turning a very readable

    my $parser = Spreadsheet::ParseExcel->new() my $book = $parser->Parse($filename);
    into a double-take inducing   my $book = do { Spreadsheet::ParseExcel->new() }->Parse($filename); leaving a lexical around is a minor sin. When I have to choose between various sins, I'll go with the one that's readable.

      That was just the last in a series of transformations showing the equivalence of the samples - I'd probably pick the first one:
      my $book; { my $parser = Spreadsheet::ParseExcel->new(); $book = $parser->Parse($filename); }

      Makeshifts last the longest.