Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: hiccoughing here documents

by AnomalousMonk (Archbishop)
on Aug 14, 2017 at 18:48 UTC ( [id://1197375]=note: print w/replies, xml ) Need Help??


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

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://1197375]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found