Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Wednesday February 11, 2009
12:25 PM

Clever

[ #38460 ]

Well, that was clever.

my $_combination_info = {};
sub combination_info {
    my $self = shift;
    @_ and $_combination_info->{ $self->csv_file_name } = $_[0];
    return $_combination_info->{ $self->csv_file_name };
}

Clever is bad. I mean TIMTOWTDI and all that, but as I'm reading through code, I hate seeing a "clever" construct that slows me down and makes me search for meaning and then bugs. It's a waste of my time.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Personally, I hate the style where you test something at the beginning of a statement and then do something if it's true by using and. I much prefer using if or unless at the end of a statement:

        my $_combination_info = {};
        sub combination_info {
            my $self = shift;
            $_combination_info->{ $self->csv_file_name } = shift if @_;
            return $_combination_info->{ $self->csv_file_name };
        }

  • I don’t see what’s wrong with that.

    You probably won’t like the way I’d write it either:

    my $_combination_info = {};
    sub combination_info {
        my $self = shift;
        my ( $info ) = @_;
        for ( $_combination_info->{ $self->csv_file_name } ) {
            $_ = $info if @_;
            return $_;
        }
    }

    • 5.10's given makes that a little less obscure to those unfamiliar with the idiom, or rather, since setting $_ is the only point of given, noone has to stop and think 'what's he looping over again?'
      • $ ~/perl/5.10.0/bin/perl -E '$x = 1; given ( $x ) { $_++ } say $x'
        1
        $ ~/perl/5.10.0/bin/perl -E '$x = 1; for ( $x ) { $_++ } say $x'
        2

        •     perl5.10.0 -E'say \my $x; given ($x) { say \$_ }'
              SCALAR(0x28368370)
              SCALAR(0x283683c0)

          It's a copy? Why on earth? Nothing in Perl copies its parameters. Doesn't that just make the whole feature hugely less useful? It's not as though given (my $tmp = $x) {} is that obscure, especially given its common use in conjunction with s///...

          Oh, and look:

              perl5.10.0 -E'say \$_ given my $x'
              syntax error at -e line 1, near "$_ given"
              Exec

          • I suppose it’s nice to be able to write given ( $foo ) in place of for ( my $copy = $foo ). But yeah, that threw me too. It’s a pity; a missed opportunity, even.

            The fact that you can’t use given as a statement modifier doesn’t bother me.