Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Automate perltidy for PM code?

by Moron (Curate)
on Jul 09, 2007 at 14:14 UTC ( #625613=monkdiscuss: print w/ replies, xml ) Need Help??

(Update: scratch the idea of changing the behaviour of code tags - see jdporter's reply and yes Perl::Tidy would make more sense than from scratch, see Derby's reply).

Re-reading the various nodes relating to editing and moderation, I couldn't find a clear statement as to whether editing should go as far as cleaning up untidy code, except that it is standard enough to request the addition of code tags via the moderation tool.

Every now and again someone complains that a post has untidy code in it, especially, inconsistent indentation that makes it hard for the reader to match block and other boundaries.

It appears reasonable to nudge the author into cleaning up his own code. On the other hand, it occurred to me that not everyione is aware of perltidy which does it automatically in a flash. And the whole point of requesting editing via moderation is to allow inexperienced, potentially novice, posters to make themselves heard without having to run a technical/site-"in"-group gauntlet first.

I did a quick straw poll through the SOPW and nearly half of the posts look up to perltidy standards. The other half and a bit apparently weren't, although with widely varying degrees of deviancy from the very trivial to the profoundly perverted (...UK readers can calm down now; I am only talking about coding standards ;) ).

Now, I have no idea what precise facilities janitors have at their disposal, nor any insight into how PM is devved, but it occurred to me that it might not be that hard to automate perltidy for select and press-button use by janitors or even to have specific perltidy tags available or another option might be to enhance the code tag to automatically feed the code through perltidy and replace the code with the results, (e.g. something like the code below).

The actual question I am raising is - should perltidy be automated here for these purposes, and if so, should it be automatically for every code-tagged block (update: to clarify: I meant to imply the alternative option to that as being a separate tidying tag for optional use only) or something requested by moderators and performed by janitors? Or the status quo is "advise but not enforce perltidy". I'll say this much about the status quo option: if the more fastidious monk wanted to goad OPers into tidying code themselves, they would have to engage in it on a more than full-time basis, given the steady flow of "unperltidy" postings!

sub Tidy { my ( $text, $cmdopt ) = @_; my $tmpfile = "/tmp/Tidy.$$." . join( '', localtime()) ."tmp"; open my $th, ">$tmpfile" or die; print $th $text or die; close $th or die; my $pid = open my $ph, "perltidy -st $cmdopt $tmpfile |" or die; my $tidied = join '', <$ph>; close $ph or die; waitpid $pid, 0; unlink $tmpfile; return $tidied; }
__________________________________________________________________________________

^M Free your mind!

Comment on Automate perltidy for PM code?
Download Code
Re: Automate perltidy for PM code?
by derby (Abbot) on Jul 09, 2007 at 14:28 UTC

    No need for piping through perltidy, Perl::Tidy with the right config can do the trick.

    -derby

    Update:

    #!/usr/bin/perl use strict; use warnings; use Perl::Tidy; my $input = <<'_EOT'; sub Tidy { my ( $text, $cmdopt ) = @_; my $tmpfile = "/tmp/Tidy.$$." . join( '', localtime()) ."tmp"; open my $th, ">$tmpfile" or die; print $th $text or die; close $th or die; my $pid = open my $ph, "perltidy -st $cmdopt $tmpfile |" or die; my $tidied = join '', <$ph>; close $ph or die; waitpid $pid, 0; unlink $tmpfile; return $tidied; } _EOT my $output; Perl::Tidy::perltidy( source => \$input, destination => \$output, ); print $output;

      Yes, thanks for that CPAN link. One thing I notice with a wry smile is that when you cut and pasted my hand-written effort you lost the original tabbing on the repaste, this being a perfect example of the kind of problem I am talking about and one that happened to be in my mind when I wrote the OP! That's the second time within a week I notice that replies to the OP serve themselves as entirely accidental examples of what the OP is talking about - - spooky! Even more spooky when you consider that both cases were your replies to my OPs - hmmm what does that mean?
      __________________________________________________________________________________

      ^M Free your mind!

        It means I got my eye on you Moron! Just kidding. For this thread, I did purposefully delete your leading spaces just to show that Perl::Tidy does the job - if I left the code alone, I believe the output from perltidy would have been identical to your original code. As for Forthcoming attraction - "Positive fast, negative slow"? ... well lets just say my attempts at sarcasm and humor did not go over well - I knew I should've said "I can't get past the first paragraph ... so I downvoted."

        -derby
Re: Automate perltidy for PM code?
by jdporter (Canon) on Jul 09, 2007 at 14:28 UTC
    should perltidy be automated here for these purposes, and if so, should it be automatically for every code-tagged block or something requested by moderators and performed by janitors?

    No, no, no. Posters should be able to post code to look the way they want it to look, full stop.

    Or the status quo is "advise but not enforce perltidy".

    Personally, I don't believe PerlMonks should officially advise perltidy. Fine for individuals to do, but PerlMonks should be neutral on the issue.

    (I point out, though it should be rather obvious, that in many cases the way code is formatted when posted must be preserved, as in the obfu section.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight

      While I would agree with on not automagically using perltidy, why should PM remain neutral about advising it? While layout style discussions waste a lot of time, I think we could all agree that following any standard is best practices and if you're having trouble deciding what that standard should be than maybe the default settings of perltidy could fill the gap.

      -derby

        And PerlMonks should recommend Parse::RecDescent for all your parsing sorro^Wneeds, too.

        A word spoken in Mind will reach its own level, in the objective world, by its own weight

        For one thing, because the quality of the code (and thus the stage of the querent's Perl journey) is often reflected in less tangible elements like the use of white space and identifiers. A lot of information could be lost by running everything through PerlTidy - including the entire point of some questions, especially those involving style considerations.


        DWIM is Perl's answer to Gödel
      Yes I can see ur right - it can't be automated for every code tag for poetic/artistic reasons - I'll scratch that idea - but I also tried to offer that there are other options available of varying flexibility and as to who-can hypothetically use it. For example, the option of a perltidy tag wouldn't enforce any policy.
      __________________________________________________________________________________

      ^M Free your mind!

Re: Automate perltidy for PM code?
by Tux (Monsignor) on Jul 09, 2007 at 14:53 UTC

    As soon as something like this will be done on PM, I will immediately stop posting code. I see this as an infringement on my personal artistic expressions layed out in the code I write. You can interpret that as a signature. If someone changes the code I wrote to something else without my permission, I see that as theft.

    If you think layout and style are bad, just use your vote to express that.


    Enjoy, Have FUN! H.Merijn
      Again, I think you are reacting to only one of the options (re code tags) which I agree with further thought would be no good and the code tags should be left alone for just the reasons you say. But I did also suggest that there are other options to choose from - it for example not being intrusive to have perltidy tags at the disposal of authors to use or not as they please.
      __________________________________________________________________________________

      ^M Free your mind!

      If someone changes the code I wrote to something else without my permission, I see that as theft.

      I'm not sure the site administrators would agree that it's theft. I could argue that it's rude and something to discourage, but not theft.

        I'm not sure the site administrators would agree that it's theft. I could argue that it's rude and something to discourage, but not theft.

        "PITA" is the buzzword. Like Google Groups habit of "massaging" both subjects and text bodies of posts, which makes for great discomfort to those used to a real newsreader...

Re: Automate perltidy for PM code?
by bluto (Curate) on Jul 09, 2007 at 15:47 UTC
    I wouldn't mind seeing a "tidy this" link right next to the code's download link which tidied the display for me alone. I'd probably use it once in a great while. Anything more than that seems like overkill (esp. on server resources).
      Putting something next to the download doesn't do much given that you could set up your own launcher to do the same thing after download. I wouldn't expect the impact on server resources of a tidy button to be that much if anything - the server appears to spend most of it's time on database interlocking, suggesting there is cpu idle time left over. The impact of saving a write or update, causing the interlocking is the same irrespective of whether a tidy was done on the post.
      __________________________________________________________________________________

      ^M Free your mind!

        To clarify, by "tidied the display" I meant on the screen. You are right that it makes no sense to provide this on download only.
Re: Automate perltidy for PM code?
by swampyankee (Parson) on Jul 09, 2007 at 17:51 UTC

    I think moron's OP is reasonably argued and well written, so I think it deserves up votes.

    On the other hand, I disagree with the necessity or even desirability of enforcing perltidy's standards. Part of this is that I don't like perltidy's defaults; part of it is that the code's physical layout can give clues about the poster's thinking.

    -->

    emc

    Any New York City or Connecticut area jobs? I'm currently unemployed.

    There are some enterprises in which a careful disorderliness is the true method.

    —Herman Melville

      I'm in this camp as well (well, I've got my own perldityrc that makes things like I liked them (which of course is contrary to the official settings at $work so I've gotta have two different copies so I can actually look at things sanely then broked them before checkin . . . :)). Very good point about the state of the code revealing state of mind; inconsistent indentation or ZOMGWTFBBQ complete lack thereof can point out stuff ("This entire block is outdented . . . maybe it's a pasteo").

      That being said though, I can agree that it'd be a nice-to-have if there were (say) a "Download Tidy'd" link which would save me the trouble of cutting and pasting into a new document and then having the editor run-shell-perltidy over it manually. Less steps == less effort to look into problems == easier to help == more likely to bother actually replying.

      Maybe some enterprising Greasemonkey guru could crank out something that would do something along these lines (not that'd do me any good since I primarily use Safari . . . ).

Re: Automate perltidy for PM code?
by DrHyde (Prior) on Jul 11, 2007 at 09:42 UTC
    I'm pleased that Moron has realised that doing this automatically is a bad idea. However, a point which no-one else raised and which is worth mentioning is that not all the code posted here is perl. I occasionally post shell scripts. Others post C or python or other languages.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://625613]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-09-17 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls