Re: Perl Coding Styles (Was: Fruitbasket and Server-Side Perl)

  • From: Veli-Pekka Tätilä <vtatila@xxxxxxxxxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sat, 13 Oct 2007 15:05:13 +0300

Hi Octavian,
a good point about SQL Lite. I use various styles as well. Speaking of
shortcuts, I always write foreach as for and let Perl figure out that
the parentheses imply a foreach loop in stead. Either

for(@array)

or 

for my $elem (@array)

I also use statement modifiers heavily, like you do, sometimes even the
low precedence and in stead of a proper loop an nested if. For instance,
Printing lines that are more than 80 chars in length, counting new
lines:

length > 80 and print while <>;

Additionally, I heavily use map or grep as substitutes for loops,
particularly in building up data structures. e.g. create an array of
lines of words (untested):

my @array = map [ split ], <>;

Or building up a hash of file names to their sizes printed biggest
first:

my %file2size = map { $_, -s } <*.mp3>;
print "$_: $file2size{$_}\n" for sort { $file2size{$b} <=>
$file2size{$a} } keys %file2size;

I do small command line apps for the most part, so gains in brevity
outweih readability here.

Notice the block form of map, since a comma is needed to separate list
items in the map expression, so the expression form could not be used. 

Knowing that in Perl 5.8 a foreach loop iterating a range is actually a
lazy one allows me to write up loops for going through array indeces,
with ease and efficiency, too: for instance 

print "$_. $array[$_]\n" for 0 .. $#array;

I've learned most of the good tricks in the Perl cook book and
intermediate Perl, both of which are books I recommend, in addition to
the Camel book, of course.

I have yet to find common, good uses for the range operator in scalar
context, it works like a flipflop then, much like it does in Sed and
Awk, I believe.

-- 
With kind regards Veli-Pekka Tätilä (vtatila@xxxxxxxxxxxxxxxxxxxx)
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila

Octavian Rasnita wrote:
> Yes I could use SQLite, but maybe some users will upload it to a Unix server
> using FTP, and they won't be able to install easy DBD::SQLite on that
> server.
> 
> I don't think I make the perl code look like the code made in other
> languages, because I don't know too many languages. For example, I don't
> know C at all.
> 
> I know just a little Java and C#, but I don't like their style of coding
> because it involves too much code for doing very few things.
> 
> However, there ar more ways to do it in perl, so I use all kind of styles.
> 
> Something I write:
> 
> foreach(@array) {
> function($_);
> }
> 
> but sometimes I write:
> 
> function($_) foreach @array;
> 
> I use to use not the most appropriate method always, but the one that comes
> to my mind earlier.
> 
> I think it is not very important if I use a method or another, and the speed
> difference between using for or foreach is practically 0.
> 
> Sometimes it is more clear if I use a block style but sometimes if too many
> blocks are involved, it is more clear to write some of them as a single line
> like in the example above and like in your second example.
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts:

  • » Re: Perl Coding Styles (Was: Fruitbasket and Server-Side Perl)