in reply to
Re: Loop Control
in thread Loop Control
Here are some pointers to help you assimilate to a more Perlish way of thinking (RESISTANCE IS FUTILE)
- I can see the point of the newl() subroutine if you want to be able to globally change what is printed after each thing of interest, but here are some ways you can make it look nicer:
- The most obvious is combine the two prints into print "\n\n";
- Since you seem to always use it immediately after a print statement, why not just declare it as a variable? $NEWL="\n\n"; print "Pass it after the string like so", $NEWL; print "Or you can just interpolate$NEWL"
- If you read through perlvar, you will find a section about $\, the output record separator. This is a special variable that is printed after every print statement. Instead of making a call to newl every time, just let Perl do all the work. local $\ = "\n\n"; print "No more newls!" You can always unset it with undef $\;
- my (and similar operators like our and local) can be applied to any number of variables at once: my (@a, @b, @c, @d, @e);. You can even assign things this way: my ($z1, $z2) = ($a[$i],$c1[$w]);
- assignment returns the lvalue (not the rvalue!), which means chomp(my $a = $b); is equivalent to my $a = $b;chomp $a;
- the range operator can use any integer (or even string) values, providing the left one is smaller than the right. (0, 1..$n) can be written as (0..$n) with no ill effects.
- qw// is often preferred when making lists of strings that do not contain spaces. Like most operators in the quote-like family, you can use any delimiters you want: @a = qw(/ /home /var /tmp /var/tmp)
- Off the top of my head, I can't think of an instance where $a = $b would give a different result than $a = "$b". I'm sure someone out there knows enough voodoo to provide some rare case where the latter would be preferred (perhaps if you were overloading stringification?), but in general Perl doesn't care if the scalar you give it is a number of a string because it will convert freely between the two. Maybe if you wanted to clean up a number stored as a string you could do something like $a = "05.230000"; $b = $a+0; # $b is 5.23 (number), but interpolating variables by themselves is a little redundant.
There are other things that could be greatly simplified as well, but those are just some of the more critical stylistic faux pas that jumped out at me. Honestly, if your code works exactly as expected, then there's not really anything wrong with it :) And if you can understand why and how it works, then it's Good Enough(tm). When I was just starting out with Perl, map confused the hell out of me, so I never used it and I always avoided solutions that people gave me that used it. Then when it clicked I thought "Holy llama balls, this shit is The Shit" and tried using it for everything. Neither ways are necessarily the best approach. In the end, all that matters is that you're constantly learning how and when to use the unique features of Perl that make it so slackfully elegant. Just lurk around and look at code examples passed around and you'll start to get it.
But seriously, do some experimenting with map, because it will greatly simplify any operation where you have to turn one set of values into another.
$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q. /_/__.,q..
Happy, sober, smart: pick two.