Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: if-elsif-else (?)

by GrandFather (Saint)
on Jul 02, 2011 at 22:59 UTC ( [id://912506]=note: print w/replies, xml ) Need Help??


in reply to if-elsif-else (?)

Absolutely omit an "unused or useless" else clause. Including it adds clutter to your code making it harder to find the code that does real work and making it harder to see the decision tree that gets you to any particular outcome.

With that in mind there is no need to add extra comments to say "Oh, and I really mean not to have some code here". Do you put a comment at the end of each line to say "This line is not conditional on anything"? The code structure itself should reflect thee intended behaviour. Relying on comments to make that clear will simply lead to trouble when the implementation and comment drift apart - perl doesn't read your comments.

That said, it may be that a better implementation for your code could be something like:

#!/usr/bin/perl use strict; use warnings; use 5.010; my @tracks = map {open my $track, ">", $_ or die "Can't open $_: $!"; + $track} 'track1.txt', 'track2.txt', 'track3.txt'; for my $track (@tracks){ print <$track>; }

or if the "line" numbers are important:

#!/usr/bin/perl use strict; use warnings; use 5.010; my @tracks = (['track1.txt', 1000], ['track2.txt', 5000], ['track3.txt', 20000] +); for my $track (@tracks) { open my $trackFile, ">", $track->[0] or die "Can't open $track->[0 +]: $!"; $track = [$trackFile, $track->[1]]; } for my $track (@tracks) { my ($trackFile, $lines) = @$track; print <$trackFile>; }

Whenever you catch yourself doing xx1 = ...; xx2 = ...; ... think instead about arrays and loops. It makes the code clearer and much much easier to maintain.

True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://912506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found