perl and indent

B

Bill Cunningham

I use indent when I'm writing C code but I just started looking at perl.
I am in the perlintro document. Is there a way to use indent with perl?

Bill
 
P

Peter J. Holzer

I use indent when I'm writing C code but I just started looking at perl.
I am in the perlintro document. Is there a way to use indent with perl?

Use perltidy.

hp
 
J

Jürgen Exner

Bill Cunningham said:
I use indent when I'm writing C code but I just started looking at perl.
I am in the perlintro document. Is there a way to use indent with perl?

Any editor used for programming should automatically indent program code
as it fits for the particular programming language. If it doesn't then
probably it misses many other useful features, too, and you may want to
consider using a different editor.

jue
 
B

Bill Cunningham

Henry said:
I suspect there may be a precise technical meaning of "I use indent";
if so then I'm about to sound like an idiot ...

My experience of using indentation for Perl programming prompts these
musings, which may be of help to you:

* Your editor, if it's any good, probably has a mode for Perl. I use
Emacs, which definitely does, and also Eclipse via the
specifically-Perl "Epic" plug-in. The editor should handle
indentation within braces, and aligning closing braces properly.

* I find two characters of indentation plenty; three at most. You may
need to customise your editor to get that.

* I advocate using "soft tabs" (spaces) rather than hard tabs.
Occasionally I'll "cat" (Linux) or "type" (Windows) a Perl file just
to have a look at it and the built-in tab is usually far too big
(like 8).
[snip]

I use nano. I'll check more into it. I'm new to Perl. I'm just now looking
at it as a second language along with C. So far it looks C like.

Bill
 
J

Jürgen Exner

Henry Law said:
Bill, nice to have you aboard. Yes, it's C-like, and many of the
differences are (IMO) improvements. But there are people who say "Don't
write C in Perl; learn to write Perl" and if you read people's code
you'll see -- at least partly -- what they mean.

For example (and I'm only an amateur at this), the C structure for an
"if" statement translates to Perl as this:

A far better example would be something, where there is no analog
construct in C, e.g. a foreach loop.
foreach my $elem (@list) {
process($elem);
}

Or filtering a list:
@foo = grep {!/^#/} @bar; # weed out lines that begin with #
Try writing this as concise in C.

jue
 
D

Dr.Ruud

For example (and I'm only an amateur at this), the C structure for an
"if" statement translates to Perl as this:
[... The code was removed, but why? ...]

A far better example would be something, where there is no analog
construct in C, e.g. a foreach loop.
foreach my $elem (@list) {
process($elem);
}

I don't see what is 'far better' here. A while on an iterator would be
very similar in C.


An even more perlish alternative;

process($_) for @list;

Or filtering a list:
@foo = grep {!/^#/} @bar; # weed out lines that begin with #

That comment is more 'what' than 'why',
and that is already clear from the code.
Consider: "ignore comment lines".
 
J

Jürgen Exner

Dr.Ruud said:
For example (and I'm only an amateur at this), the C structure for an
"if" statement translates to Perl as this:
[... The code was removed, but why? ...]

A far better example would be something, where there is no analog
construct in C, e.g. a foreach loop.
foreach my $elem (@list) {
process($elem);
}

I don't see what is 'far better' here. A while on an iterator would be
very similar in C.

I respectfully disagree. The point is that in C you cannot access the
elements of an array or list without using an explicit index which means
you have to iterate (i.e. explicitely initialize, increment, and
terminate) over that index.

Therefore the difference is between
"with each element of the list do foobar()"
and
"initialize $i to start index;
as long as $i is smaller than the end index
{do foobar() with element array[$i] and increment $i}

To me it is a very major difference if I have to invent and maintain an
auxiliary index variable or not.
An even more perlish alternative;
process($_) for @list;

This is where _I_ would ask what is the difference to
foreach (@list) {process ($_)};

jue
 
U

Uri Guttman

JE> This is where _I_ would ask what is the difference to
JE> foreach (@list) {process ($_)};

no block entry on each loop iteration which is overhead. also i would
almost never format a foreach loop on one line so the for modifier save
lines, pixels, {} chars and thousand of lost souls. actually fewer lines
mean fewer bugs given the old saw of 1 bug per 100 lines in any language
average.

i like foreach modifier a great deal and use it when i can.

uri
 
U

Uri Guttman

S(J)M> I've dealt with code that had no white space not required by the
S(J)M> language; it wasn't pretty. I'll take my code prettyprinted, TYVM.

i never said no white space. i said fewer lines when you can. foreach
modifier is one of those times. there is a balance to be found. foreach
modifier is a win for that and other reasons.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top