Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: using Perl::Tidy/perltidy()

by toolic (Bishop)
on Apr 18, 2012 at 18:37 UTC ( #965776=note: print w/replies, xml ) Need Help??

in reply to using Perl::Tidy/perltidy()

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, as described by:
$ perldoc perltidy
When I run diff on the 2 files, I see many whitespace differences, as expected:
$ diff 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

Replies are listed 'Best First'.
Re^2: using Perl::Tidy/perltidy()
by Aldebaran (Deacon) on Apr 19, 2012 at 19:10 UTC

    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 $ perl downloaded 2 images from to folder site_5 $ echo "wow, that's minimal." wow, that's minimal. $ cat #!/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 = ''; 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

        alright then, 4 it is. When you think about it, that's fine because you're not dealing with

        int main(){ c everything's indented in the main body return 0; }

        nor does perl have the nested looping that you see so much with explicit indices. In reading the camel and llama book, I don't think I've seen nesting beyond 2 levels.

        If anyone has what they think is an improvement over the default values that might be appropriate for someone coming up on acolyte status, I am--monkishly--all ears.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2021-03-01 10:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found