Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by davidfilmer (Sexton)
on Mar 07, 2021 at 08:24 UTC ( #11129248=perlquestion: print w/replies, xml ) Need Help??

davidfilmer has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Is there a better way to use the line number in a quoted string than __LINE__?
by haukex (Bishop) on Mar 07, 2021 at 08:43 UTC
    Line 4 is still a lot of fanning-about, but it doesn't even compile

    "@{[__LINE__}]" is a syntax error because you've swapped the } and ] - "@{[__LINE__]}" works fine.

    some convenient token such as $LINE (or $__LINE__ or whatever I can just embed in double-quotes)

    Nope, at least not built in, sorry. __LINE__ is like a function call, as of Perl 5.16 you can even write it as __LINE__().

    Personally, I'd suggest living with the "fanning-about" or writing a helper function to output debug info (e.g. using caller). (Update: TIMTOWTDI - AnomalousMonk's printf suggestion is nice too. Personally, if it was a case where I didn't feel like it's worth it to write a helper, I'd probably write print "Line ",__LINE__,"\n";)

    But since this is Perl, of course there's also a way to hack it:

    use warnings; use strict; { package LineNumber; sub TIESCALAR { bless {}, shift } sub FETCH { (caller)[2] } } tie my $LINE, 'LineNumber'; print "Line $LINE\n"; print "Line $LINE\n";
Re: Is there a better way to use the line number in a quoted string than __LINE__?
by AnomalousMonk (Bishop) on Mar 07, 2021 at 08:43 UTC
    ... a better way ...

    Maybe
        printf "Line %s looks very nice\n", __LINE__;
    or
        printf "Line %d looks very nice\n", __LINE__;
    which don't seem so bad to me.

    NB: This is hardly unfanny, but works:

    Win8 Strawberry 5.8.9.5 (32) Sun 03/07/2021 3:40:51 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings print "Line ${ \__LINE__ } is ok syntactically \n"; ^Z Line 1 is ok syntactically


    Give a man a fish:  <%-{-{-{-<

Re: Is there a better way to use the line number in a quoted string than __LINE__?
by LanX (Sage) on Mar 07, 2021 at 08:48 UTC
    Are you trying to reinvent warn ?

    >    #print "Line [ @{[__LINE__}] is a syntax error\n";

    Because you are closing the parens in the wrong order.

    Otherwise it works.

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

    Out of the box, no.

    But if you use this very often consider a subroutine which builds the string. Using caller inside that routine would give you the line number.

    Otherwise if it really needs to be a variable I'd suggest Tie::Hash to make "$caller{line}" return what you want. Alternatively "$LINE" with Tie::Scalar .

    Similarly for package and file

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    ) like shown by Haukex++ in the meantime.

Re: Is there a better way to use the line number in a quoted string than __LINE__?
by stevieb (Canon) on Mar 07, 2021 at 18:44 UTC

    Many great answers so far, but another option... printf()!

    printf("%d looks very nice\n", __LINE__);

    Easy to read, not a whole lot of crazy syntax, and very easy to update later if new variables are needed in the output (ie. don't have to trudge through making sure you're not breaking the concatenations).

Re: Is there a better way to use the line number in a quoted string than __LINE__?
by Fletch (Bishop) on Mar 08, 2021 at 04:22 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11129248]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-07-06 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?