Gossamer Forum
Home : General : Perl Programming :

variable value missing

Quote Reply
variable value missing
i have no idear why partial variable value missing. and these missing value are alway between " and ". hope to get help here.

For example: $value1= 'dog "cat1 cat2" '; The final reads is dog, any value between " and " is disappeared.( value of cat1 and cat2 is disapeared)



I am useing following code to fetch the html page: $value1 is enbeded in that html papge.

sub Template {
my ($template) = @_;
my $HTML="";

open (TEMPLATE, "$template") || die print "CAN NOT OPEN TEMPLATE FILE : $template : $!";
#open (TEMPLATE, "$template") || die print "we are in difficulty in finding the file requested, please relogin and try again";
while(<TEMPLATE>) { $HTML .= $_; }
close(TEMPLATE);

$HTML =~ s/\$(\w+)/${$1}/g;



print $HTML;
}



THANKS
Quote Reply
Re: [courierb] variable value missing In reply to
I wouldn't recommend doing it like that. Store all your tags/values in a hashref then do something like:

Code:
sub Template {
#-----------------------------------------------------------------
# Parse the template.

my $template = shift;
my $tag_hash = shift;

# read the template into $HTML
open TEMPLATE, $template or die print "CAN NOT OPEN TEMPLATE FILE : $template : $!";
read TEMPLATE, my $HTML, -s TEMPLATE;
close TEMPLATE;

# Substitute tags.
$HTML =~ s/\$(\w+)/exists $tag_hash->{$1} ? $tag_hash->{$1} : "Unknown Tag : $1"/sg;

# Print
print $HTML;

# Make sure you know whats happening here...you will be returning to the calling sub-routine.

}

So you'd then pass in your template and tag hash to Template() like:

Template('some_file', \%tag_hash );

Say for example tag_hash looked like:

( foo => 'bar' )

In your template you would add:

$foo

...and bar should print.

The way you have it is a bit awkward as you are trying to load scalars using the matched value. It's hard to explain but I think using a hash is far better.
Quote Reply
Re: [Paul] variable value missing In reply to
THANKS PAUL.

I just make a try on the code. it return unknow tag error.

could you make a more specific sample of the tag?

Let 's say. the varible is get from a form data. i have already parse the form data and defined it as

$animal= $input{animal};



in my last try, i enbed $animal in the temp file, and it return me unknow tag.

Thanks

Last edited by:

courierb: Oct 9, 2002, 7:25 AM
Quote Reply
Re: [courierb] variable value missing In reply to
You can send the input hash directly into the Template() sub-routine as a reference eg:

Template('your_file', \%input);

Then you can use any input parameter name as a tag...eg...

$animal
Quote Reply
Re: [courierb] variable value missing In reply to
using

&Template($htmltemplate, \%tag_hash);



Thanks
Quote Reply
Re: [courierb] variable value missing In reply to
Remember though that you will get unknown tag errors if the tag name doesn't exist, which is why you need to start working on something like GT::Template::Parser Wink j/k

Last edited by:

Paul: Oct 9, 2002, 7:30 AM
Quote Reply
Re: [courierb] variable value missing In reply to
Paul. I do not think it is the problem of template parse. below code will print out different result.

If $name= a"nima"l

print<<"[HTML]";

1. <input type="text" name="animal" value="$name" style="font-size:12px" size="20" maxlength="38">

2. animal="$name"

[HTML]

the first one will only print letter a in the form, all other letter are missing

the secound one will print the completed value animal=a"nima"l



how to solve this problem?



Thanks
Quote Reply
Re: [courierb] variable value missing In reply to
Based on your template code in post one, code like this:

Quote:
If $name= a"nima"l

print<<"[HTML]";

1. <input type="text" name="animal" value="$name" style="font-size:12px" size="20" maxlength="38">

2. animal="$name"

[HTML][/code]
....won't work. Does your parser parse all the "If"'s and "=" and "print"s etc?

As it stands (based on the code you showed in your original post) the parser only supports you placing simple tags into your code like $foo and $bar

Last edited by:

Paul: Oct 10, 2002, 5:27 AM