perl and indent

Discussion in 'Perl Misc' started by Bill Cunningham, Oct 27, 2012.

  1. 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
     
    Bill Cunningham, Oct 27, 2012
    #1
    1. Advertising

  2. On 2012-10-27 19:39, Bill Cunningham <> wrote:
    > 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


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
     
    Peter J. Holzer, Oct 27, 2012
    #2
    1. Advertising

  3. "Bill Cunningham" <> wrote:
    > 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
     
    Jürgen Exner, Oct 28, 2012
    #3
  4. Henry Law wrote:
    > 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
     
    Bill Cunningham, Oct 29, 2012
    #4
  5. Henry Law <> wrote:
    >On 29/10/12 17:35, Bill Cunningham wrote:
    >
    >> 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, 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
     
    Jürgen Exner, Oct 30, 2012
    #5
  6. Bill Cunningham

    Dr.Ruud Guest

    On 2012-10-30 01:16, Jürgen Exner wrote:
    > Henry Law <> wrote:


    >> 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".

    --
    Ruud
     
    Dr.Ruud, Oct 30, 2012
    #6
  7. "Dr.Ruud" <> wrote:
    >On 2012-10-30 01:16, Jürgen Exner wrote:
    >> Henry Law <> wrote:

    >
    >>> 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
     
    Jürgen Exner, Oct 30, 2012
    #7
  8. Bill Cunningham

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    >> An even more perlish alternative;
    >> process($_) for @list;


    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
     
    Uri Guttman, Oct 31, 2012
    #8
  9. Bill Cunningham

    Uri Guttman Guest

    >>>>> "S(J)M" == Shmuel (Seymour J ) Metz <> writes:

    S(J)M> In <>, on 10/31/2012
    S(J)M> at 12:27 AM, Uri Guttman <> said:

    >> actually fewer lines mean fewer bugs


    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
     
    Uri Guttman, Oct 31, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,470
  2. Replies:
    7
    Views:
    7,375
    Pierre Barbier de Reuille
    Aug 14, 2006
  3. dt
    Replies:
    4
    Views:
    497
    CBFalconer
    Dec 31, 2006
  4. Replies:
    0
    Views:
    1,079
  5. Rustom Mody

    reifying indent and dedent into braces

    Rustom Mody, Oct 13, 2009, in forum: Python
    Replies:
    4
    Views:
    352
Loading...

Share This Page