Template-Plex

 view release on metacpan or  search on metacpan

lib/Template/Plex/Internal.pm  view on Meta::CPAN

		if(ref($path) eq "GLOB"){
			#file handle
			$options{file}="$path";
			<$path>;
		}
		elsif(ref($path) eq "ARRAY"){
			#process as inline template
			$options{file}="$path";
			join "", @$path;
		}
    elsif(ref($path) eq "SCALAR"){
      # Make relative to callers path
      my $cpath=$options{caller}->meta->{file};
      use File::Basename "dirname";
      my $rpath=dirname $cpath;
      $rpath.="/".$$path;
      $options{file}=$rpath;

			my $fh;
			if(open $fh, "<", $rpath){
				<$fh> 
			}
			else {
				die "Could not open file: $rpath $!";
				"";
			}
    }
		else{
			#Assume a path
			#Prepend the root if present and if not absolute
      

      # only prepend root if relative path
      #unless($path=~m|^/|){
      unless(file_name_is_absolute($path)){
        # Assume working dir if no root
        $path=join "/", $root, $path if $root;
      }
			$options{file}=$path;

			my $fh;
			if(open $fh, "<", $path){
				<$fh> 
			}
			else {
				die "Could not open file: $path $!";
				"";
			}
		}
	};

	$args//={};		#set to empty hash if not defined
	
	chomp $data unless $options{no_eof_chomp};
	# Perform inject substitution
  #
	_subst_inject($data, root=>$root) unless $options{no_include};
	# Perform superfluous EOL removal
  #
	_block_fix($data) unless $options{no_block_fix};
  #use feature ":all";
  #say STDERR  $options{file};
  # Only do an init fix if the file has plex|plx in the name
  # of if it is an array?
  if($options{file}=~/\.plex|\.plx|^ARRAY/){
    # Actually a template
    #say STDERR "ACTUAL TEMPLATE $options{file}";
	  _init_fix($data) unless $options{no_init_fix};
  }
  else {
    #say STDERR "NOT ACUTALLY A TEMPLATE $options{file}";
    # Assume a raw file..
    # enode into hex
    $data='@{[pack "H*","'. unpack("H*", $data).'"]}';
  }

  _comment_strip($data) if $options{use_comments};

	if($args){
		#Only call this from top level call
		#Returns the render sub

		state $package=0;
		$package++;
		$options{package}="Template::Plex::temp".$package; #force a unique package if non specified
		$prepare->($plex, $data, $args, %options);	#Prepare in the correct scope
	}
	else {
		$data;
	}
}


#Join map
sub jmap :prototype(&$@){
	my ($sub,$delimiter)=(shift,shift);	#block is first
	$delimiter//="";	#delimiter is whats left
	join $delimiter, map &$sub, @_;
}



1;



( run in 0.755 second using v1.01-cache-2.11-cpan-302cb4679cc )