return to first page linux journal archive
keywordscontents

Listing 2. Including Files using Perl

sub include_file
{
	# Get the filename
	my $filename = shift;
	# Get our output ready
	my $outputstring = "";
	# Open the file
	if (open (FRAGMENT, $filename))
	{
	  # Loop through the file, adding text
	  # to $outputstring
	  while (<FRAGMENT>)
	  {
		$outputstring .= $_;
	  }
	  # Close the filehandle nicely
	  close (FRAGMENT);
	}
	else
	{
      # If there was an error, return
      # an error message in the text
	  $outputstring .= 
         "<P>[Can't open $filename: $! ]</P>\n";
	}
	return $outputstring;
}