in reply to Re: winter games, perl, ice and bling
in thread winter games, perl, ice and bling

I have a style note. I'd like to see a consistent placement of the opening curly bracket for an anonymous block as an argument to a function when the block is going to span multiple lines. You place the bracket for map on the next line, but on the same line for sort. Either would make sense despite personal tastes, but they should probably be the same.

Not something wrong with your code, but perhaps worth mentioning is the magic <>. It is Perlish enough in a short code sample like this, but I did mark the OP down for not checking the return on open. Since Perl automatically emits an error message when it can't open a specified file using this idiom, the only trade-off for the extra power is not being able to emit your own custom error message. That's usually not worth much compared to processing all of your command-line arguments in order, but in the case of internationalization it might pose an issue.

Replies are listed 'Best First'.
Re^3: winter games, perl, ice and bling
by jdporter (Paladin) on Mar 27, 2008 at 15:43 UTC

    Thank you for your comments.

    they should probably be the same.

    I am of the "do what looks right in each individual situation" school of thought, which I believe is very consistent with the Perl philosophy.

    perhaps worth mentioning is the magic <>.

    Um... I think not. It's Perl 101.

    in the case of internationalization it might pose an issue.

    Really? Is perl's default error message library not sufficiently internationalized?

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
      I thought the magic <> was worth mentioning specifically because of the error message issue, not because I thought people might not be able to figure it out.

      When using the locale module and POSIX::setlocale, the error message does not seem to change. I get the same when setting LANG, LC_CTYPE, and LC_MESSAGES in my environment then using the locale module.

      perllocale says this:

      The remaining locale category, "LC_MESSAGES" (possibly supplemented by others in particular implementations) is not currently used by Perl--except possibly to affect the behavior of library functions called by extensions outside the standard Perl distribution and by the operating system and its utilities. Note especially that the string value of $! and the error messages given by external utilities may be changed by "LC_MESSAGES". If you want to have portable error codes, use "%!". See Errno.

      There are options, though. You can use the Errno module to provide an error message in addition to the one Perl's going to emit, you can trap the warning and change the text that's emitted, or you can wrap the hash assignment/map/read in parentheses and use a logical or to emit an additional message.

      use Errno; ... my %skater_avg = map { chomp; my( $skater, @scores ) = split /,/; { $skater => ( sum(@scores)-min(@scores)-max(@scores) ) / (@scores +-2) } } <>; warn "custom message!\n" if $!{ENOENT};
      use Errno; $SIG{__WARN__} = sub { if ( $!{ENOENT} ) { warn "custom message!\n"; } };
      ( my %skater_avg = map { chomp; my( $skater, @scores ) = split /,/; { $skater => ( sum(@scores)-min(@scores)-max(@scores) ) / (@scores +-2) } } <> ) || warn "custom message!\n";

      Update: made a doc link to Errno. Update 2:s/though/thought/ in that first sentence.