Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Perl looks ugly to novices?

Name: Anonymous 2012-08-25 21:58

I thought the whole point of Perl was that there were so many ways to code something, there's always a way to make the code look elegant.
From Larry Wall:
Perl is designed to give you several ways to do anything, so consider picking the most readable one.
This contrasts languages like Ruby which basically encourage/force you  to do things a specific way.

Just read this quote
I have a pretty major problem with a language where one of the most common variables has the name $_
Except you never have to use $_ in the first place. It's always a choice

In conclusion, people who don't like Perl don't know how to use it.

Name: Anonymous 2012-08-26 5:07

>>2
What about this? Takes a normally delimited data file and adds a column which is just the sum of the other columns

#!/usr/local/bin/perl
use strict;
use warnings;

sub f(@); #function declaration taking an array as an argument
my $line;
my ($field, @fields);
my $file_name = $ARGV[0];

open(my $source_file, $file_name) || die $!;
open(my $destination_file, ">$file_name.tmp") || die $!;

while($line = <$source_file>) { #read one line at a time, store in $line
  @fields = split(/\s+|,/, $line); #split line into an array with whitespace or comma as delimiter
  foreach $field(@fields) {
    print $destination_file "$field\t";
  }
  print $destination_file f(@fields);
  print $destination_file "\n";
}

close($source_file);
close($destination_file);

unlink($file_name) || die $!;
rename("$file_name.tmp", $file_name) || die $!;

sub f(@) {
  my @array = @_;
  my $value = 0;
  my $item;

  foreach $item(@array) {
    $value += $item;
  }
  return($value);
}


It can still be cleaned up, but claiming that that can't be read by a a programmer who doesn't read know perl is silly. Any decent programmer can read that.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List