tags:

views:

44

answers:

1

Hi All,

I am very new to perl coding, I am calling a method, which again calls some other method and then generate an html code. I need to embed the htmlcode in my current code, so as to add that to the current html code.

I am calling the method like this

my $test = $frek->xyz(); where xyz generated an html.

now i need to embed the $test in my html, but not finding the way out. PLease help

+3  A: 

Its not entirely clear what you want, but maybe it's a

what would answer your question. What exactly do you try do do? Can you give a specific example?


Addendum

After reading another of your comments, I think I got what you are trying to accomplish.

Lets imagine we have a Perl class 'MyClass' that contains a method xyz():

package MyClass;
sub new {
 my $class = shift;
 my $self = { x => shift, y => shift, z => shift };
 bless $self, $class;
 return $self
}
sub xyz {                      # <== here we go
 my ($self) = @_; 
 return $self->{x} * $self->{y} * $self->{z}
}
1;

If your Perl program (e.g. cgitest.pl) works as a simple CGI-script from a cgi-bin directory, it would look like this:

#!/usr/bin/perl
use strict;

# here we have html included in source
my $html = q{
<html>
<head></head>
<body>
  <h1>Test</h1>
  <div id='test_results'> #{$test}# </div>
</body>
</html>
};

use MyClass;                         # lets hope it'll be found
my $frek = new MyClass(10,10,10);    # create instance
my $test = $frek->xyz();             # get value

$html =~ s/#{(\$\w+)}#/$1/eeg;       # now replace #{$test}# in html by $test

print "Content-type: text/html\n\n"; # output modified html to browser
print $html;

This would replace the marker #{$var}# by the value of the actual $var and print the resulting html. Note the (double) /ee after the substitution pattern.

But then, if your web site is a Mason site, your test.html simply looks like:

 <h1>Test</h1>

 <div id='test_results'> <% $test %> </div>

 <!-- Perl initialization code goes below -->

 <%init>
  use MyClass;
  my $frek = new MyClass(10,10,10);
  my $test = $frek->xyz();
 </%init>

which can be written similar with a %perl code block :

<h1>Test</h1>

<%perl>
  use MyClass;
  my $frek = new MyClass(10,10,10);
  my $test = $frek->xyz();
</%perl>

<div id='test_results'> <% $test %> </div>

but now, you have intermingled html parts and Perl parts, whereas in the example above, all Perl code goes below the html. If your Web- Server is properly configured for HTML::Mason, it will handle either of them fine. Mason is available for Windows, Unix and whatever systems there are.

Regards

rbo

rubber boots