Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: quotes in Perl (opinionated note on coding style)

by dimar (Curate)
on Feb 09, 2005 at 18:04 UTC ( #429444=note: print w/replies, xml ) Need Help??

in reply to quotes in Perl

The use of here documents is particularly useful ... because it allows you to keep the markup relatively free of escape characters that would otherwise reduce ... readability

(opinionated note on coding style, your preference may vary)

The HERE doc syntax was once a favorite here, but now it has been completely abandoned because:

  • 1) There were cases of too many 'escape' chars strewn about, (as mentioned elsewhere in this thread) which motivated a 'zero-tolerance' bias to *absolutely ban all ugly escape sequences*.
  • 2) There were cases where *some* of the variables were intended to be interpolated, but not others (e.g., with code-generation scripts) so it was not clear whether to use "EOT" or 'EOT' (as mentioned elsewhere in this thread).
  • 3) Indentation hassles (as mentioned elsewhere in this thread).

Because of these issues, we made a stylistic decision not to use HERE doc syntax anymore, and instead use the very flexible quotelike operator with all variables 'concatenated in' instead.

Thus, your example would become:

use strict; my $foo = 123.45; my $bar = "Martha Stewedprune"; ### --------------------------- print q^ ===== This is an example of text taken literally except that variables are expanded where their variable names appear. foo: ^.$foo.q^ bar: ^.$bar.q^ (Our *uninterpolated* variable names are "$foo" and "$bar" ... and we didn't need escape chars to tell you that). ^; ### ---------------------------

All you have to worry about is to make sure your quotelike delim character (caret) is never used in the document. If it is, just change it to something else. Moreover, you can indent this however you want without external module dependencies. Here, we just use comments to make the document visually distinct from the rest of the script.

Replies are listed 'Best First'.
Re^2: quotes in Perl (opinionated note on coding style)
by hv (Parson) on Feb 10, 2005 at 14:56 UTC

    Hmm, so instead of ensuring some variables are not interpolated by replacing "$foo" with "\$foo", you instead ensure the other variables are interpolated by replacing "$bar" with "^.$bar.q^".

    That doesn't seem like such a good trade to me.


      It can be a good trade if most of your variables are uninterpolated. In that case you want the unusual form to be the "marked" form, which helps prevent mistakes. That's the justification for Perl 6's version of the same trick, only it's spelled \qq[$bar] there.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://429444]
[Corion]: Meh. SQL window functions would even make pagination easy/trivial (but not performant), as rank() over (partition by user order by timestamp) / 10 as page would give me a page number for each item, with 10 items per page.
[Corion]: Of course, the query performance for "all items on page 10" is likely worse than rank() between 100 and 109 , but if that means I can write 15 lines of SQL instead of needing to think about how to partition things and how to encode the page size...
[Corion]: ... that would be nice. But alas, I'm currently tied to SQLite as minimum implementation, and it doesn't implement window functions :-(
[Corion]: And I'm not aware of any other serverless SQL implementation that even reaches the capability of SQLite, not to mention surpasses it

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2018-03-22 12:17 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (274 votes). Check out past polls.