Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dial your perldoc to perlsyn:

Here's how a C programmer might code up a particular algorithm in Perl: for (my $i = 0; $i < @ary1; $i++) { for (my $j = 0; $j < @ary2; $j++) { if ($ary1[$i] > $ary2[$j]) { last; # can't go to outer :-( } $ary1[$i] += $ary2[$j]; } # this is where that last takes me } Whereas here's how a Perl programmer more comfortable with the idiom might do it: OUTER: for my $wid (@ary1) { INNER: for my $jet (@ary2) { next OUTER if $wid > $jet; $wid += $jet; } }

When I scanned the above yesterday, I realized that there is a lot of C in my Perl. I, without a doubt, would have rolled the C loop to solve that problem. I don't want to focus too much on that example, though, it's just the catalyst that made me start thinking about this.

It's all over my code, I'll roll a C-style loop where a map would fit better, I'll go for the C solution rather than use better suited Perl idioms. Sometimes I'll notice this after the fact and write it better, sometimes I won't. But what really worries me is that I'm not really getting better at it over time. Sure, I know more about Perl and have a handle on more advanced features now, but I'm not really writing much better code than I did 5 years ago. Maybe it's because Perl isn't my main breadwinner, maybe I'm just stubborn.

Can you teach an old dog new tricks? Or rather, can you point me towards any kind of resource that would help? Or talk about your own experiences, or anything else you think would be useful.

For reference, the things that have made me "okay" at writing Perl so far are:

  • Perl Cookbook - Excellent book that stays beside me all the time now. Gives me great ideas involving tight and clean Perl.
  • Perl Best Practices - Besides being a good practices book, it keyed me into some traps I was creating for myself by not using Perl's tools properly.
  • PerlMonks - I've seen some great stuff on here, and the only problem is that the things I see are often ancillary to what I'm doing - they help out but not directly with problems I'm working on.

And that's that. Oh, also I put this in Meditations rather than Seekers because it seemed like the place for this kind of open-ended discussion-ish question, but I can axe it and move it if people want.

Edit: (Dec 14, 2007 at 15:40ish UTC): Thank you all for so much advice in such a short time. I think my plan going forward would be to:

  1. Learn a language that slaps me in the face with its functional nature, to expand my problem-solving toolbox.
  2. Try to spend more time with "Here's a problem - Here's a Perl solution" articles and such. The Cookbook has been good to me, so I might as well run with that kind of resource.
  3. Try to be less gunshy about asking questions here.

As to why I want to change, I currently write "okay" code with "okay" maintainability when working with Perl. I suppose I'm sort of annoyed that tool I'm happiest with is the one I produce the worst product with.


In reply to How do I train myself to write more Perl-ish Perl, rather than C-ish Perl? by amarquis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found