Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: hiccoughing here documents

by Anonymous Monk
on Aug 12, 2017 at 15:55 UTC ( [id://1197311]=note: print w/replies, xml ) Need Help??


in reply to Re: hiccoughing here documents
in thread hiccoughing here documents

Something like this works:

sub IFACE { my $pattern = shift @_; my $result; foreach $iface (@_) { $result .= (eval "\"$pattern\"") . "\n"; } chomp $result; return $result; } &utter( <<"EOF" ); iptables -t nat -N outbound-DMZ @{[ &IFACE( "iptables -t nat -A outbound-DMZ -s $lanCidr -o \$iface -j SNAT --to +-source $dmzIp", (keys %ipAddress)) ]} iptables -t nat -A outbound-DMZ -j RETURN EOF

I know it's unconventional but I've capitalised the function name to emphasise its relationship to the name of the variable being interpolated, I felt that trying to pass that name as a parameter would be pushing my luck. It's (obviously) very sensitive to the double-quotes being correct, both in the function and in the heredoc itself. The one thing I wasn't able to get working was ~ to enable indents (5.20.2 on Debian Jessie armhf), but that's a fairly detail since the nearer the heredoc is to the actual stuff going into the tables the better.

Many thanks to everybody, especially NetWallah.

Pax vobiscum, MarkMLl

Replies are listed 'Best First'.
Re^3: hiccoughing here documents
by AnomalousMonk (Archbishop) on Aug 14, 2017 at 18:48 UTC

    Here's a version that's not quite so funky (but still kinda ugly). This is a situation in which a prototype is actually useful. If the  hiccup() function is defined in the source file in which it's used | invoked, either the full definition of the function or a prototype declaration must appear before first invocation of the function. If the function is moved to a module, a use statement must appear before first invocation.

    File hiccup_here_3.pl:

    use warnings; use strict; use Hic; # sub hiccup (&@); my $lanCidr = 'WHATEVER'; my $dmzIp = 'SOMEWHERE'; my %ipAddress = qw(HERE we_are THERE it_goes EVERYWHERE at_once); my @array = qw(WIBBLE BOFF); my $s = << "EOF"; iptables -t nat -N outbound-DMZ @{[ hiccup { "iptables -t nat -s $lanCidr -o $_ -j SNAT --to-source $dmzIp" } keys %ipAddress ]} whatever else here @{[ hiccup { "flocculate -f $_ -j SNAT --to-source $dmzIp" } @array ]} iptables -t nat -A outbound-DMZ -j RETURN EOF # sub hiccup (&@) { return join "\n", map $_[0]->(), @_[ 1 .. $#_ ]; } print "[[$s]]";

    Module Hic.pm:

    package Hic; use warnings; use strict; use parent 'Exporter'; our $VERSION = '0.1.0'; our @EXPORT = qw(hiccup); # default exported symbol(s) our @EXPORT_OK = qw(); # optional exported symbol(s) (none) sub hiccup (&@) { return join "\n", map $_[0]->(), @_[ 1 .. $#_ ]; } 1;

    However it's invoked, the function produces the following output:

    c:\@Work\Perl\monks\Anonymous Monk\1197277>perl hiccup_here_3.pl [[iptables -t nat -N outbound-DMZ iptables -t nat -s WHATEVER -o EVERYWHERE -j SNAT --to-source SOMEWHER +E iptables -t nat -s WHATEVER -o THERE -j SNAT --to-source SOMEWHERE iptables -t nat -s WHATEVER -o HERE -j SNAT --to-source SOMEWHERE whatever else here flocculate -f WIBBLE -j SNAT --to-source SOMEWHERE flocculate -f BOFF -j SNAT --to-source SOMEWHERE iptables -t nat -A outbound-DMZ -j RETURN ]]

    Update: Screwed up initial post of hiccup_here_3.pl file. Fixed.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found