Slash Boxes
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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • It's in a very functional style, that's all.

    True, most Perl programmers would have trouble maintaining it, and this is a valid concern.

    Maybe you should comment it heavily and stare at it until you understand it forward and backwards. There's a lot to be gained by functional programming. Generally, the fewer the intermediate variables and the less the code, the _better_ the maintenance in the long run.

    Obnoxious comment. Could you make it even more functionally gnarly with something like this (untested)

    • by Ovid (2709) on 2003.08.02 14:58 (#22700) Homepage Journal

      Aack! I'll pretend you didn't write that :)

      One of the reasons why I break things into smaller steps like I did is that it's easier to insert debugging statements. As for the above, I wound up using an entirely different strategy suggested by Gav.

      sub _set_up_names {
          my ($self,$products) = @_;
          my $length = 0;
          my @names =
              map {
                  local $_ = [$_->id, [split ' ', $_->name]];
                  $length = @{$_->[1]} if @{$_->[1]} > $length;
          $length -= 2;
          my $count = @names;

          # assume length > 0 -- must test
          my ($index,$name) = (0,'');
          FIND_COMMON_NAME: {
              $name = '';
              foreach $index ( reverse 0 .. $length ) {
                  $name = join ' ', @{$names[0][1]}[0 .. $index];
                  if ($count == grep { $name eq join ' ', @{$_->[1]}[0..$index] } @names) {
                      @names = map { $_->[0], join ' ', splice @{$_->[1]}, $index + 1 } @names;
                      last FIND_COMMON_NAME;
          my %attributes = @names;
          return ( $name, \%attributes );

      It's still pretty wonky (and the above is just a first pass), but it solves my problem. It also much smaller than my original solution of which my earlier snippet was only a small subset.

      • I've heard this argument about inserting debug statements, but I think it's oversold.

        If I have something in an extreme functional style and I need to debug it, I just replace one of the function calls with a call to a function_debug wrapper. In my above extremely functional style, you could add map_debugs or mapcaru_debugs which would print arguments and then apply the map or mapcaru function.

        One added advantage of this is that you can get all your maps or mapcarus to confess at once, often yielding une