Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: RFC: Creating unicursal stars

by huxtonr (Initiate)
on Nov 06, 2009 at 16:27 UTC ( [id://805517]=note: print w/replies, xml ) Need Help??


in reply to RFC: Creating unicursal stars

The main problem isn't finding $mod, it's getting it to work, and you've done that. Congratulations!

However, there are a couple of things you might find it useful to change.

Firstly, "use strict" at the top of your script. Always. Until you know enough to ignore that advice.

Secondly, try having a short main body of code calling a couple of top-level functions:

#!/usr/bin/perl ... my @polygon = build_polygon($num_points, $radius); print_svg_polygon(@polygon); exit; sub build_polygon { ...

It makes it easy to see what the script does at a glance, and lets you re-use bits more easily.

Thirdly, make sure you scope your variables. Use "my" to restrict them to their defining block. Otherwise, the $n in calcMagic for example is the same as the $n at the top of your script. If you changed it inside the sub it would change in the main script too.

sub calcMagic { my ($n) = @_; my $m = -1; my $mod = 1; for(1..$n) { $mod += $m; $m = nextMagic($m); } return $mod; }

Finally, read up on Getopt::Long (http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm). That will let you call add named options to your script so you can do something like:

draw_star.pl --points=5 --radius=200

It's a well-known and well tested module, and has good documentation.

You could also replace the multiple "if" statements with a lookup into an array, but I'm not sure it would make the code any clearer really.

Hope that's helpful.

Replies are listed 'Best First'.
Re^2: RFC: Creating unicursal stars
by Fox (Pilgrim) on Nov 07, 2009 at 16:09 UTC
    thanks for the tips, will try to put it in practice ;)
Re^2: RFC: Creating unicursal stars
by Anonymous Monk on Nov 06, 2009 at 18:14 UTC

      That template is from 2000 (we're three versions of Perl past that time), was probably mostly developed even earlier, and while tye's advice is generally to be preferred over mine, I would call camelCase bad—or at the most defensible least, uncommon—Perl style.

        To be fair, Your Mother is from before 2000 (though I don't recall using Perl v2 that year) and was probably conceived even before that. That is clearly enough reason to discount something with no further justificaiton. Oh, wait, I forgot to make a dubious style argument. Unfortunately, most have not seen how he dresses, so I'll omit that. (:

        was probably mostly developed even earlier

        Perhaps you are unfamiliar with the meaning of the words "updated" and "update"?

        Actually, much (I'm not sure "most") originated before 2000. And I've updated it quite recently (and several times before that) and so anything from before 2000 must be some of the best of that style since it has survived unchanged for so long.

        So, did you actually have any real critique to offer (other than the one style point)? I'm sure people would love to learn what can be improved and how (and why).

        As to CamelCase, I'll disagree that it is bad and also that it is uncommon in Perl. I seriously doubt you are using the modules file_handle, ext_utils::make_maker, dyna_loader, auto_loader, math::big_int, or time::hi_res to name just a tiny fraction. And the motivations for using CamelCase in module names at least partly matches my motivations for using it in subroutine names.

        Compare the output of perl -we "print this" vs. perl -we "print This" and you'll see that the second avoids:

        Unquoted string "this" may clash with future reserved word

        Unfortunately, it seems that most Perl guides pretty much ignore the problems with the following code:

        sub log { warn timestamp(), "@_"; } sub dump { log( Dumper( \@_ ) ); }

        I expect a significant fraction of serious Perl coders to notice the problem with line 1 (I'm not sure if it would be more or less than half of them). I expect only a small fraction to notice the problem with line 5. I expect many to go so far as to deny the problem with line 2.

        Personally, I much prefer a style that simply prevents all of those problems. Which means I updated my standard template to not use "sub main". The two routes I considered were CamelCase and under_scored. under_scored would have meant either "sub _main" or "sub main_" and the former of those already is well-established style for "private" and so would be quite confusing while the latter I consider rather silly looking and much, much more unusual than "sub Main", which is what I standardized on. (And going with "sub Main" with the intent of keeping underscores would lead to the Under_Scored_Capitalized style which I find rather "the worst of both worlds".)

        I don't personally grumble when I run into under_scored-style identifiers in Perl code (much less go publicly proclaiming such to be "bad"). But I do dislike the relative difficulty of typing _, that fact that documentation about such identifiers usually ends up with the _ being wider than whitespace and thus the spacing conveys rather the opposite of what _ is meant to convey, and that things like HTML links often make such style very hard_to read with_confidence. While I almost never run into identifiers that get to the point of being difficult to read whenWrittenInCamelCase (and can't think of any other down sides to camelCase). So I personally consider CamelCase to be slightly better than under_scored in several ways and worse than under_scored in no practical ways. But I end up using both under_scored and CamelCase quite a bit in Perl code.

        But, of coure, the point of that template has extremely little to do with what style one uses for their variable names. I really don't want anybody to declare a lexical with the name $putMainVarsHere.

        As for some more "modern" alternatives, I actually prefer -w over warnings.pm for this specific case (and prefer neither for writing modules), though I do value and use warnings.pm in much more limited scenarios (but I won't go into the "why"s in this node -- though I have discussed at least much of that elsewhere around here).

        I often prefer use vars over our, especially in modules. But I do use our when it is worth it. I'm probably even more likely to replace use vars qw( $Globals ); in that template with my $Global= ...;. But the distinction between any of those three choices isn't the point of that terse example, either.

        Then there are state variables. A great many of the Perl installs that I use right now don't support such. It certainly isn't time for me to consider standardizing on using them. Nor do I believe that I'd always replace my current pattern even when state $x is an option. Actually, the next module I'll be putting on CPAN (perhaps this week) deals with this type of stuff.

        - tye        

        I've used that template about 10 times in the past month and it helped me avoid the pitfalls tye lists as justifications at least 5 times. To use your words Your Mother, And we were just discussing Dunning–Kruger the other day...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found