Code Style -- here-docs -- How do you make them look good?

Discussion in 'Perl Misc' started by MST, Jul 10, 2004.

  1. MST

    MST Guest

    Usually I use perl for system admin type things, but recently I've
    been writing a flurry of CGI scripts. In the process I find my self
    using (abusing?) here-docs to print chunks of HTML code. I find that
    no matter how hard I try I just can't find a style of writing a
    here-doc that pleases me I've tried things like this:

    if($test) {
    print <<HTML
    <p>Some html $Junk!</p>
    <p>$More junk</p>
    HTML
    }

    and that isn't so bad. What irks me is if the here-doc isn't at the
    end of a block I need to throw a ; on a line all by its lonesome. This
    leaves me trying to creatively position code chunks such that the
    here-doc is at the end of a block (which usually results in less
    readablility in the end). The readablity of my code is fairly
    important as eventually I will be passing it off to a much less
    seasoned perl programmer.

    What do you guys think? Should I forget it and switch to using
    multiple print qq()'s? Is there a better way to format the here-doc?
    Maybe I just have a stick up my butt; however I wanted to see what
    kind of feedback I'd get from the .misc crowd.
     
    MST, Jul 10, 2004
    #1
    1. Advertising

  2. MST

    gnari Guest

    "MST" <> wrote in message
    news:...
    >
    > if($test) {
    > print <<HTML
    > <p>Some html $Junk!</p>
    > <p>$More junk</p>
    > HTML
    > }
    >
    > and that isn't so bad. What irks me is if the here-doc isn't at the
    > end of a block I need to throw a ; on a line all by its lonesome.


    I think you suffer from a misconception. the ; belongs at the
    end of the print line. it just happens that you can leave it out
    in this case, but it is clearer if you don't.

    gnari
     
    gnari, Jul 10, 2004
    #2
    1. Advertising

  3. MST

    Paul Lalli Guest

    On Fri, 9 Jul 2004, MST wrote:

    > Usually I use perl for system admin type things, but recently I've
    > been writing a flurry of CGI scripts. In the process I find my self
    > using (abusing?) here-docs to print chunks of HTML code. I find that
    > no matter how hard I try I just can't find a style of writing a
    > here-doc that pleases me I've tried things like this:
    >
    > if($test) {
    > print <<HTML
    > <p>Some html $Junk!</p>
    > <p>$More junk</p>
    > HTML
    > }
    >
    > and that isn't so bad. What irks me is if the here-doc isn't at the
    > end of a block I need to throw a ; on a line all by its lonesome.


    No you don't. You need to put a semi-colon after the first heredoc
    marker:

    if ($test) {
    print <<HTML;
    <p>Some html here</p>
    HTML
    print "this prints!\n";
    }


    > This
    > leaves me trying to creatively position code chunks such that the
    > here-doc is at the end of a block (which usually results in less
    > readablility in the end). The readablity of my code is fairly
    > important as eventually I will be passing it off to a much less
    > seasoned perl programmer.
    >
    > What do you guys think? Should I forget it and switch to using
    > multiple print qq()'s?


    Why multiple?

    print qq
    (this is one line
    here's another.
    and a third\n);

    That will print the three lines as written, because qq obeys whitespace,
    including newlines.

    > Is there a better way to format the here-doc?
    > Maybe I just have a stick up my butt; however I wanted to see what
    > kind of feedback I'd get from the .misc crowd.


    The other thing you might want to consider is using one of the templating
    modules available on CPAN. I've never used any of them extensively, so I
    can't recommend one above the other. Others can, I'm sure.


    Paul Lalli
     
    Paul Lalli, Jul 10, 2004
    #3
  4. MST

    Ala Qumsieh Guest

    Paul Lalli wrote:
    > On Fri, 9 Jul 2004, MST wrote:
    >
    >>if($test) {
    >> print <<HTML
    >> <p>Some html $Junk!</p>
    >> <p>$More junk</p>
    >>HTML
    >>}
    >>
    >>and that isn't so bad. What irks me is if the here-doc isn't at the
    >>end of a block I need to throw a ; on a line all by its lonesome.

    >
    >
    > No you don't. You need to put a semi-colon after the first heredoc
    > marker:
    >
    > if ($test) {
    > print <<HTML;
    > <p>Some html here</p>
    > HTML
    > print "this prints!\n";
    > }


    That is of course correct. But, as an Emacs user, I know for a fact that
    heredocs confuse its cperl mode, and the auto indentation breaks after
    the end marker. Putting a lone semicolon on a line by itself after the
    end marker fixes the problem.

    To the OP, I don't see how this affects readability.

    --Ala
     
    Ala Qumsieh, Jul 10, 2004
    #4
  5. MST

    MST Guest

    Thanks for the advice, as I said I'm not usually treading in CGI land
    so my CGI programming style is style being worked out. I didn't
    realize that the quote like operators were ok with embedded newlines;
    I guess you learn something new every day :)
     
    MST, Jul 10, 2004
    #5
  6. MST

    Tintin Guest

    "MST" <> wrote in message
    news:...
    > Thanks for the advice, as I said I'm not usually treading in CGI land
    > so my CGI programming style is style being worked out. I didn't
    > realize that the quote like operators were ok with embedded newlines;
    > I guess you learn something new every day :)


    CGI has no "programming style" nor "quote like operators". Perhaps you are
    confusing CGI with Perl?
     
    Tintin, Jul 10, 2004
    #6
  7. Tintin wrote:
    > MST wrote:
    >> Thanks for the advice, as I said I'm not usually treading in CGI
    >> land so my CGI programming style is style being worked out. I
    >> didn't realize that the quote like operators were ok with
    >> embedded newlines; I guess you learn something new every day :)

    >
    > CGI has no "programming style" nor "quote like operators". Perhaps
    > you are confusing CGI with Perl?


    You should have read the whole thread before making such a comment. If
    you had, you had realized that the OP is well aware of the distinction
    between Perl and CGI. (And you wouldn't have posted.)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 10, 2004
    #7
  8. MST

    Vetle Roeim Guest

    On Sat, 10 Jul 2004 06:49:05 GMT, Ala Qumsieh <> wrote:

    [...]
    > That is of course correct. But, as an Emacs user, I know for a fact that
    > heredocs confuse its cperl mode, and the auto indentation breaks after
    > the end marker. Putting a lone semicolon on a line by itself after the
    > end marker fixes the problem.


    I believe you are mistaken. It is the normal perl-mode that is confused
    by the heredocs, and cperl-mode is not. At least that is my experience.


    [...]
    --
    It's not a bug, it's the future.
     
    Vetle Roeim, Jul 12, 2004
    #8
    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:
    370
  2. why the lucky stiff
    Replies:
    5
    Views:
    153
    why the lucky stiff
    Sep 22, 2004
  3. Replies:
    2
    Views:
    104
  4. Qu0ll
    Replies:
    2
    Views:
    142
    Qu0ll
    Apr 23, 2014
  5. Stefan Ram
    Replies:
    1
    Views:
    95
    Qu0ll
    Apr 23, 2014
Loading...

Share This Page