Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

using Perl::Tidy/perltidy()

by Aldebaran (Deacon)
on Apr 18, 2012 at 18:10 UTC ( #965767=perlquestion: print w/replies, xml ) Need Help??

Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm trying to make my posts more-readable and was advised to take a look at Perl::Tidy, which I downloaded from CPAN:

http://search.cpan.org/~shancock/Perl-Tidy-20101217/lib/Perl/Tidy.pm

The treatment there is a bit above my head, so I google for Perl::Tidy examples to learn that it is also a bash command:

http://perltidy.sourceforge.net/perltidy.html

So I get that too, and see that they talk about default behavior, which I test on the following script:

$ perltidy pt1.pl $ cat pt1.pl #!/usr/bin/perl -w use strict; use WWW::Mechanize; use LWP::Simple; use feature ':5.10'; my $domain = 'http://www.yahoo.com'; my $m = WWW::Mechanize->new( qw/ autodie 1 /); $m->get( $domain); my @list = $m->images(); my $counter = 0; my $dir = &DirPP; for my $img (@list) { my $url = $img->url_abs(); $counter++; my $filename = "$dir". "/image_". "$counter"; getstore($url,$filename) or die "Can't download '$url': $@\n"; } sub DirPP { use Errno qw/ EACCES /; # permission denied state $counter2 = 1; my $word = "site"; my $name ; my $made = 0; while(1){ $name = sprintf '%s%3d', $word, $counter2; last if not $made = mkdir $name, 0755 ; die $! if $! == EACCES; } return $name if $made; return; } $

So, so far, I've got nothing. It occurs to me, if I get this feature squared away, then the Virtue of Laziness will be doing my formatting indefinitely.

q1) How do I proceed? Do you have a perltidy install that works for you?

q2) Can someone discuss the difference between Perl::Tidy and perltidy()? Are they related under the hood, for example?

TIA.

Replies are listed 'Best First'.
Re: using Perl::Tidy/perltidy()
by toolic (Bishop) on Apr 18, 2012 at 18:37 UTC
    Do you have a perltidy install that works for you?
    Yes, I do.
    So, so far, I've got nothing.
    When I run perltidy on your code, I get a new file named pt1.pl.tdy, as described by:
    $ perldoc perltidy
    When I run diff on the 2 files, I see many whitespace differences, as expected:
    $ diff pt1.pl pt1.pl.tdy 7,9c7,9 < my $m = WWW::Mechanize->new( qw/ autodie 1 /); < $m->get( $domain); < my @list = $m->images(); --- > my $m = WWW::Mechanize->new(qw/ autodie 1 /); > $m->get($domain); > my @list = $m->images(); 11c11 < my $dir = &DirPP; --- > my $dir = &DirPP; 15,16c15,16 < my $filename = "$dir". "/image_". "$counter"; < getstore($url,$filename) or die "Can't download '$url': $@\n"; --- > my $filename = "$dir" . "/image_" . "$counter"; > getstore( $url, $filename ) or die "Can't download '$url': $@\n" +; 19c19 < sub DirPP { --- > sub DirPP { 21,32c21,29 < use Errno qw/ EACCES /; # permission denied < state $counter2 = 1; < my $word = "site"; < my $name ; < my $made = 0; < while(1){ < $name = sprintf '%s%3d', $word, $counter2; < last if not $made = mkdir $name, 0755 ; < die $! if $! == EACCES; < } < return $name if $made; < return; --- > use Errno qw/ EACCES /; # permission denied > state $counter2 = 1; > my $word = "site"; > my $name; > my $made = 0; > while (1) { > $name = sprintf '%s%3d', $word, $counter2; > last if not $made = mkdir $name, 0755; > die $! if $! == EACCES; 33a31,33 > return $name if $made; > return; > } 35,37c35 < < < $ --- > $
    q2) Can someone discuss the difference between Perl::Tidy and perltidy()? Are they related under the hood, for example?
    Perl::Tidy is used by perltidy. perltidy is just a wrapper script:
    $ perldoc -m perltidy

    If you want to edit the file in place, leaving a backup file, use perltidy -b

      Thx toolic, I was reading the example page wrong and missed that my file was there with a tdy suffix. Looks like it's all straightened out now:

      $ perltidy -b tg6.pl $ perl tg6.pl downloaded 2 images from http://www.perlmonks.org to folder site_5 $ echo "wow, that's minimal." wow, that's minimal. $ cat tg6.pl #!/usr/bin/perl -w # creates a new directory and downloads images from url to it # perlmonks node 965537; thx aaron and A.M. use strict; use feature ':5.10'; use WWW::Mechanize; use LWP::Simple; use Errno qw[ EEXIST ]; # get information about images my $domain = 'http://www.perlmonks.org'; my $m = WWW::Mechanize->new(); $m->get($domain); my @list = $m->images(); # create new folder and download images to it. my $counter = 0; my $dir = &mk_new_dir; for my $img (@list) { my $url = $img->url_abs(); $counter++; my $filename = $dir . "/image_" . $counter; getstore( $url, $filename ) or die "Can't download '$url': $@\n"; } # output print "downloaded ", $counter, " images from ", $domain, "\n"; print "to folder ", $dir, "\n"; sub mk_new_dir { my $counter2 = 1; while (1) { my $word = "site"; my $name = $word . '_' . $counter2++; if ( mkdir $name, 0755 ) { return $name; # success, return new dir name } else { next if $!{EEXIST}; # mkdir failed because file exists die sprintf "(%d) %s", $!, $!; # other failure; bail ou +t! } } } $

      I think, if I were going to change anything, it might be the number of spaces (or tab-length, whichever it is) after a statement and before a right comment and maybe go from 4 to 3 spaces for a general indenting. You can see that I have a bit of fold-over in the display (not real fold-over with a newline tho)

      Let me ask this. Are there specific choices that matter a whole bunch in the negative sense here. For example, if I went with -i3 indenting, are there people that matter out there who would say, "gosh, the perl here is one thing, but this 3-space indenting is giving me a headache and crossing my eyes. What moron does that when he could have done 2 or 4: nice even numbers?"?

        A 4 space indent is fairly conventional in the Perl world, but really is not critical in any sense. In a C++ context I use a 3 space indent because 2 is too few for me to be able to see correctly a lot of the time and 4 leads to stuff disappearing off the right edge too fast. I use a 4 space indent for Perl because that is most common. Sometimes it is just best to go with the flow!

        Personally I really dislike white space on the inside of parenthesis ( like this ), but if you are going to do it at least be consistent (why should the rule be different for while than for for?).

        Where there is no other compelling reason to choose one formatting option over another I choose the one that results in code looking the most like conventionally formatted English prose. That is what I parse most and I figure the more my code parser is like my prose parser the fewer parsing errors I'll get.

        True laziness is hard work
Re: using Perl::Tidy/perltidy()
by GrandFather (Saint) on Apr 18, 2012 at 23:52 UTC

    I use an editor (Komodo) which supports external pretty printers for various file types and comes with perltidy already hooked up. I have tuned perltidy to match the way I tend to write Perl code so most of the time I run it it doesn't change a lot, but if I am refactoring code perltidy is invaluable for quickly fixing indentation and wrapping lines nicely.

    If you use a configurable editor and let us know what you are using. It may be that someone may be able to help you hook up perltidy to your editor. For Komodo see the 'Formatters' section of the Preferences dialog (Edit|Preferences menu entry).

    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2021-03-04 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (107 votes). Check out past polls.

    Notices?