Jan Eden wrote:
>
> I have a piece of HTML code containing two Perl variable names, which is to be
> used in 6 scripts. So I tried to put it into a separate file to be executed with
> "do page_head.pl", where page_head.pl contains something like (simplified):
>
> my $page_head = qq{<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC
> [snip HTML]
>
> The obvious problem is, that the variables are not interpolated according to
> their current value in the scripts, i.e. although
>
> $mother_id = 453;
> and
> $title = "Title";
>
> The $page_head variable will an contain empty title tag and show_local.pl
> parameter after the do command.
>
> How can I circumvent this without writing the html code in all the six scripts?
> eval does not seem to be an alternative here.

Hi Jan.

I felt like a quick Perl 'fix' :)

perldoc -f do says:

  [do] also differs in that code evaluated with "do FILENAME"
  cannot see lexicals in the enclosing scope; "eval STRING" does.

so you need to eval the contents of a file. If in page_head.pl you
write this:

q{ qq{
  <?xml version="1.0" encoding="utf-8"?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
  <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="de" lang="de">
  <head>
    <title> $title </title>
  </head>
  <body>
    <a href="show_local.pl?id=$mother_id" class="head" target="_self">UP</a>
    <div class="textbox">
    <!-- begin content -->
  </body>
  </html>
} };

then I hope you can see that

  do 'page_head.pl'

returns the string without the enclosing q{ .. }. i.e. the double-quoted
string that you want to eval. So you can then write

  use strict;
  use warnings;

  my $title = 'Title';
  my $mother_id = '453';

  print eval do 'page_head.pl';

or, to avoid recompiling the string every time you use it, I would prefer

  use strict;
  use warnings;

  use constant PAGE_HEAD => do 'page_head.pl';

  my $title = 'Title';
  my $mother_id = '453';

  print eval PAGE_HEAD;

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to