Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to import markup containing quotes?

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to leave my markup in external files and bring it in like this:

# Read the content for the <head> element into a variable.
my $path = $markup_dir . '/head.htm';
open(FILE, $path) or die "Cannot read file $path: $!";
my $head = <FILE>;
close (FILE);

print $head;

The file contains the entire content for my <head> element. But then I find that my <head> element is not terminated, and in fact contains nothing at all:

<!DOCTYPE html><html><head><body><div id='leftbackground'>...

It seems like it must be having a problem with quoted strings inside the file (all are single-quotes). How do I fix this?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be of TREMENDOUS help if you

a) posted a compete, runnable perl script that shows your problem. For someone to run your code, they'd have to figure out a bunch of issues just to see what you are seeing.
b) posted the input file - or at least, a small sample of it that illustrates your problem.

I was just able to write a perl script that reads this file:

this is a file
it has 'single quotes'
it has "double quotes"
it has `back quotes`
it works fine

and it printed this:
mars:hci:/export/home/hci/fbr1917/perlscripts>read.pl
this is a file
it has 'single quotes'
it has "double quotes"
it has `back quotes`
it works fine

The problem is not in the file or with the quotes. It must be something else...and we can't really tell without seeing all of it...

Now...i have a GUESS...but it is ONLY a guess, since I can't see your perl or your file.

The diamond operator only reads up to a newline. I am GUESSING your file contains some...so once it reaches the first one, it stops. If you want to read the entire contents of the file to a variable, you need to do something like this:


This while loop keeps reading a line at time, until there are no more lines, and appends them onto the $head2 variable. See if that works.

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right, thank you. For some reason I thought the syntax I was using would bring it all in in one go. Perl does take some getting used to. It's awfully cryptic and not altogether to my liking. Happily, my code is done, tested and working -- I used a loop to bring it in line by line -- and I can move on to other things now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic