Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Is a here-doc a good way to retrieve data file contents for testing?

by tybalt89 (Monsignor)
on Jan 17, 2021 at 01:58 UTC ( [id://11127016]=note: print w/replies, xml ) Need Help??


in reply to Is a here-doc a good way to retrieve data file contents for testing?

Maybe cleaner, or maybe not ...

#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; print "-------\n"; print json_q(); print "-------\n"; print json_here(); print "-------\n"; is(json_q(), json_here(), "should be the same"); sub json_q { q( "Type": 0, "Width": 504, "X": 18, "Y": 18 } ] }); } sub json_here { <<'END_JSON' =~ s/\n\z//r } "Type": 0, "Width": 504, "X": 18, "Y": 18 } ] } END_JSON

Replies are listed 'Best First'.
Re^2: Is a here-doc a good way to retrieve data file contents for testing?
by haukex (Archbishop) on Jan 17, 2021 at 08:42 UTC
    sub json_here { <<'END_JSON' =~ s/\n\z//r }

    Although I'd probably have done something similar, I just wanted point out that s///r requires Perl 5.14+, which may be a limitation if this is a module that is intended to be compatible with a broad range of Perl versions. sub json_here { chomp( my $json = <<'END_JSON'); $json } works fine too.

Re^2: Is a here-doc a good way to retrieve data file contents for testing? (updated)
by LanX (Saint) on Jan 17, 2021 at 02:04 UTC
    tybalt89, you are one of the most unorthodox Perl hackers I've seen so far and I mean it as a compliment. :)

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

    UPDATE

    WOW, this even allows interpolating variables from the function scope in the "outside" HERE-DOC ...

    use strict; use warnings; print "-------\n"; print json_here(42,666); print "-------\n"; sub chompit { my $str = shift; chomp $str; return $str; } sub json_here { my $x=shift; chompit << "__JSON__" } "Type": 0, "Width": 504, "X": $x, "Y": $_[0] } ] } __JSON__

    C:/Perl_524/bin\perl.exe -w d:/tmp/pm/chomp_here.pl ------- "Type": 0, "Width": 504, "X": 42, "Y": 666 } ] }-------

      Because it's a lexer thing. To the parser it's just a string *inside* the body of the sub.

        I know.

        But that's still not a guarantee, I doubt this was intended.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found