Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. # Copyright 2004, Hetzner Africa. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS EMPLOYEES
  13. # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16. # HOLDER OR EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  18. # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. #
  24. # $Id: Interpolator.pm,v 1.1 2004/01/30 09:34:57 ianf Exp $
  25.  
  26. package HETZNER::Interpolator;
  27.  
  28. $HETZNER::Interpolator::VERSION = '1.0';
  29.  
  30. use Class::MethodMaker
  31. new_with_init => 'new',
  32. new_hash_init => '_init_args',
  33. get_set => [ qw( filename delim_re depth inc_re contents) ];
  34.  
  35. sub HETZNER::Interpolator::init {
  36. my ($self, $args, $hash_ref) = @_;
  37. $self->_init_args(%{$args});
  38.  
  39. $self->{delim_re} = defined($self->{delim_re}) ?
  40. $self->{delim_re} : q{\$([a-z_][a-z0-9_]*)};
  41.  
  42. if ($self->{delim_re} !~ m/\(.*\)/) {
  43. die "delim_re does not contain '()'";
  44. }
  45.  
  46. $self->{depth} = defined($self->{depth}) ? ++$self->{depth} : 0;
  47.  
  48. $self->{inc_re} = defined($self->{inc_re}) ?
  49. $self->{inc_re} : q{^\s?#include '(.*)'$};
  50.  
  51. if ($self->{inc_re} !~ m/\(.*\)/) {
  52. die "inc_re does not contain '()'";
  53. }
  54.  
  55. if (defined($self->{filename}) && defined($hash_ref)) {
  56. $self->load_file($hash_ref);
  57. }
  58.  
  59. return $self;
  60. }
  61.  
  62. sub HETZNER::Interpolator::load_file {
  63. my ($self, $args) = @_;
  64.  
  65. # Guard against recursive include files
  66. if ($self->{depth} >= 10) {
  67. die "recursive include in file ".$self->{filename};
  68. }
  69.  
  70. if (!defined($self->{filename}) || $self->{filename} eq "") {
  71. die "HETZNER::Interpolator::load_file : filname not specified";
  72. }
  73.  
  74. open(F,$self->{filename}) ||
  75. die "Could not open " .$self->{filename}." : $!";
  76. my $local = join("",<F>);
  77. close(F);
  78.  
  79. # Do the parsing
  80. my $repl_re = qr/$self->{delim_re}/;
  81. while ($local =~ m/$repl_re/gmc) {
  82. my $token = $1;
  83.  
  84. if (!exists($$args{$token})) {
  85. die $self->{filename}." : $token does not exist";
  86. } elsif (!defined($$args{$token})) {
  87. die $self->{filename}." : $token is not defined";
  88. }
  89.  
  90. my $replace = $$args{$token};
  91. $local =~ s/$repl_re/$replace/e;
  92. }
  93.  
  94. # Resolve includes
  95. my $inc_re = qr/$self->{inc_re}/;
  96.  
  97. while ( $local =~ m/($inc_re)/gmc ) {
  98. my ($repl, $fname) = ($1,$2);
  99. my $p = new HETZNER::Interpolator({ filename => $fname,
  100. depth => $self->{depth} }, $args);
  101. $p->load_file($args);
  102. my $new = $p->contents();
  103. $local =~ s/$repl/$new/m;
  104. }
  105. # Return parsed contents, for recursive case
  106. $self->contents($local);
  107. }
  108.  
  109. sub HETZNER::Interpolator::Stringify { return $_[0]->{contents}; }
  110.  
  111. use overload
  112. '""' => \&Stringify;
  113.  
  114. 1;
Add Comment
Please, Sign In to add comment