• Is there a better way to insert the line number into a text string than

    From David Filmer@21:1/5 to All on Sun Mar 7 00:00:33 2021
    Hello everyone. 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.

    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 5 is still a lot of fanning-about, but it doesn't even compile.

    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.

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

    Thanks for reading.

    - David

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to David Filmer on Sun Mar 7 11:10:06 2021
    David Filmer <davidfilmer@gmail.com> writes:

    Hello everyone. 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.

    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__ is not a Perl variable. It's just a special token replaced at
    compile time by the current source line number.

    Line 5 is still a lot of fanning-about, but it doesn't even compile.
    s/5/3/

    That line has mismatched brackets, but it wouldn't work anyway because
    __LINE__ is not a variable.

    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.

    Is ", and he then ," really a lot of fanning about? Anyway, I don't
    know of any alternative, so you'll have to decide it it's worth it.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Athanasius@21:1/5 to David Filmer on Sun Mar 7 21:33:19 2021
    On 7/03/2021 6:00 pm, David Filmer wrote:
    Hello everyone. 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.

    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 5 is still a lot of fanning-about, but it doesn't even compile.

    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.

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

    Thanks for reading.

    - David


    FWIW, I would use printf:

    21:29 >perl -wE "printf qq{Line [ %s ] looks nice too\n}, __LINE__;"
    Line [ 1 ] looks nice too

    21:30 >

    Hope that helps,

    --
    Athanasius <°(((><

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)