Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Mr. BlindNewB question regarding spacing

by mhinkle (Initiate)
on Oct 01, 2012 at 17:36 UTC ( [id://996700]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have just started to learn Perl. I am not a programmer (yet).

All of the examples I find have no spaces between lines and I am getting older so its harder for me to see and thus understand, :( so I’d like to add spaces (newlines, whitespace) in my programs—is this acceptable?

I am including an example below of a program example I found and the way I have modified it to make it easier for me to understand and see.

Can someone tell me if these two scripts will produce the EXACT same results? Or if my modifications change the functionality of it?

Original:

#!/usr/bin/perl

@lines = `perldoc -u -f atan2;`;

foreach (@lines) {

s/\w<(^>+)>/\U$1/g;

print;

}

Modified:

#!/usr/bin/perl

@lines = `perldoc -u -f atan2;`;

foreach (@lines)

{

s/\w<(^>+)>/\U$1/g;

print;

}

  • Comment on Mr. BlindNewB question regarding spacing

Replies are listed 'Best First'.
Re: Mr. BlindNewB question regarding spacing
by toolic (Bishop) on Oct 01, 2012 at 17:42 UTC
    Can someone tell me if these two scripts will produce the EXACT same results?
    You can determine if the output is identical yourself by using diff:
    orig.pl > out1 mod.pl > out2 diff out1 out2

    See also:

    UPDATE: from perlsyn:

    Perl is a free-form language: you can format and indent it however you like. Whitespace serves mostly to separate tokens, unlike languages like Python where it is an important part of the syntax, or Fortran where it is immaterial.
Re: Mr. BlindNewB question regarding spacing
by davido (Cardinal) on Oct 01, 2012 at 17:49 UTC

    Hello, I have just started to learn Perl.

    Great. This is a good place to start, and you've picked a fun language to learn with.

    Since you're just getting started, go to http://perldoc.perl.org and read perlintro. The question you're asking is addressed in the section titled, "Basic Syntax Overview". There it explains the rules regarding whitespace. But do read through that full document. It will hasten your learning experience, and takes advantage of the fact that many of the questions you're likely to come up with in the first few days have already been answered in one concise document.


    Dave

Re: Mr. BlindNewB question regarding spacing
by Fletch (Bishop) on Oct 01, 2012 at 17:43 UTC

    Please say you didn't, you know, actually do something foolish like actually trying it yourself? That'd have been sheer madness threatening the very fabric of space/time itself.

    (Not that one can see any differences since you neglected to use <code></code> tags around your samples.)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Mr. BlindNewB question regarding spacing
by Anonymous Monk on Oct 01, 2012 at 20:07 UTC

    While I cannot see the changes you made in your example (a problem with node formatting), the answer is yes, you can use whitespace. In fact, please add (a reasonable amount of) whitespace. Separate any logical blocks of code you have with an extra linebreak. I generally sprinkle one after (a group of) variable declarations, too. (But I hate "double-spaced" code.)

    Also, use tabs to indent your code. Configure your editor to show them as n spaces, where n is a comfortable value (for you). I generally use a value of two spaces, but whenever I'm reading code I'm unfamiliar with, I'll increase it temporarily to four. It helps me understand the flow better.

    You might also like the /x modifier if you're working with regular expressions. And if you don't have a syntax-colouring editor yet, get one immediately! It will help tremendously.

    Here's what my code looks like, to give you a rough idea on what I consider OK spacing:

    sub lint { my ($fn) = $_; $fn = decode_utf8($fn); if ($fn ne (my $new_fn = compose($fn))) { print STDERR "Decomposed unicode: $new_fn\n"; if (!DRY_RUN) { die if -e $new_fn; rename($fn, $new_fn) or warn "Failed rename for file $fn"; } } if (-f $fn) {
Re: Mr. BlindNewB question regarding spacing
by Anonymous Monk on Oct 01, 2012 at 18:12 UTC

    Some editors have “Perl Tidy” tools that will automatically reformat source-code without changing it, in order to improve visual consistency within the code.   Yes, such changes can be a big improvement, because we are extremely visual creatures.

    One thing can be a problem, though ... if you reformat an entire module, a source code control system will perceive that the entire module has been changed.   Therefore, what I often prefer to do is to highlight a particular block of code, reformat it automatically, then check-in the file with an appropriate comment.   Then, separately, make the changes that I am contemplating, and check those in again.   This creates a much clearer history of what was changed and why.   Gradually, over the course of time, but only in the areas currently receiving attention for other reasons, the visual quality of the code will improve.

Re: Mr. BlindNewB question regarding spacing
by GrandFather (Saint) on Oct 02, 2012 at 20:21 UTC

    Instead of adding what would generally be considered excess white space, try setting your editor to use a larger font.

    Generally more important than white space is that you always use strictures (use strict; use warnings;) to tell you early about silly errors such as typos.

    It's much harder to see and understand stuff that isn't there. Avoid using the default variable $_ where you can use a meaningfully named variable instead. Consider:

    #!/usr/bin/perl use strict; use warnings; my @lines = `perldoc -u -f atan2;`; for my $line (@lines) { $line =~ s/\w<([^>]+)>/\U$1/g; print $line; }
    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found