Greetings, Masters. I am often annoyed that this code:

#!/usr/bin/perl print "Line [ ", __LINE__, " ] looks very nice\n"; print "Line [ __LINE__ ] is taken as a string-literal\n"; #print "Line [ @{[__LINE__}] is a syntax error\n";

produces this result:

Line [ 2 ] looks very nice. Line [ __LINE__ ] is taken as a string-literal.

The syntax of Line 2 works OK, but it is a lot of fanning-about that often makes me reluctant to include this useful bit of debugging information.

I would much prefer the syntax of Line 3, or some convenient token such as $LINE (or $__LINE__ or whatever I can just embed in double-quotes).

Line 4 is still a lot of fanning-about, but it doesn't even compile, even though this screwball syntax:

     print "The Time Is: [ @{[scalar(localtime)]} ] right now\n\n";

works just fine, without dropping out of the double-quotes:

     The Time Is: [ Sun Mar  7 00:14:15 2021 ] right now

Is there a better way to include the __LINE__ information without all this fanning-about?

Thanks for reading. I appreciate any help to beautify my ugly code.

- David

In reply to Is there a better way to use the line number in a quoted string than __LINE__? by davidfilmer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.