Gossamer Forum
Home : General : Perl Programming :

Getting a file into a variable...

Quote Reply
Getting a file into a variable...
Hi. Could someone please tell me why this code will not put the contents of template.html into the variable $template?

I have tried and trid, but i'm stumped!!!

Any help you can offer would be much appreciated

Yours

A.J.Newby

Quote Reply
Re: Getting a file into a variable... In reply to
How about:

open (DAT, $file) || die ("Cannot open $file - $!");
my @file = <DAT>;
close DAT;

my $template;
foreach (@file) { $template .= $_; }


Dan Cool


Quote Reply
Re: Getting a file into a variable... In reply to
Hi. It doesn't work. I keep getting blank pages now.

Have a look at;

http://www.installers.ace-installer.com/cgi-bin/aceclassifieds.cgi

Thanks

Andy

Quote Reply
Re: Getting a file into a variable... In reply to
Don't worry!!!! It was just i used

open (DAT, template.html) || die ("Cannot open $file - $!");
my @file = <DAT>;
close DAT;

my $template;
foreach (@file) { $template .= $_; }

WHEN i should have used;

open (DAT, "template.html") || die ("Cannot open $file - $!");
my @file = <DAT>;
close DAT;

my $template;
foreach (@file) { $template .= $_; }

Thanks

andy

Quote Reply
Re: Getting a file into a variable... In reply to
The code snippet is sound so there must be something wrong somewhere else in your script. Do you print proper content headers first? The URL you posted is password protected and besides, seeing a blank page won't help debug the code - I take your word for it that it produces no output ;)

Post your code here - if large (e.g., > 100 lines), make it available as a text file on your site and post the URL to it.


Dan Cool


Quote Reply
Re: Getting a file into a variable... In reply to
How would you put a selection of code into a variable? I am using;


$template = qq|

Code Here

|;

But it isn't working. Any ideas?

Andy Cool

Quote Reply
Re: Getting a file into a variable... In reply to
Good stuff although you may wish to replace $file with template.html. BTW, UBB formating codes use square brackets and not angular brackets. Have a great one, I'm outta here for the night.


Dan Cool


Quote Reply
Re: Getting a file into a variable... In reply to
Thanks. I made the changes!!!!

Andy

Quote Reply
Re: Getting a file into a variable... In reply to
Use reg exp like:

$template =~ s/replace/with/gise;

Case-insenstive and replace all incidences. Now I'm outta here.


Dan Cool


Quote Reply
Re: Getting a file into a variable... In reply to
$template =~ s/replace/with/gise;

is called 'sed' not 'reg exp'.. it uses regexp in the portion that tells it what to look for though..

Jerry Su
widgetz sucks
Quote Reply
Re: Getting a file into a variable... In reply to
to make variables like perl.. do this..

$template =~ s/\${?(\w+)}?/${\1}/gise;

i think thats how it works... not quite sure.. ummmmmm.... basically it looks for scalar variables in the file..

ie:

$anything (in file) gets replaced with $anything (in perl code)..

if you have things like.... if you want to print something like "99s" where 99 is the variable.. then you'd do..

${seconds}s and it will replace ${seconds} with $seconds..

Jerry Su
widgetz sucks
Quote Reply
Re: Getting a file into a variable... In reply to
Shouldnt it be...

$template = print qq|

Code

|;


Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Getting a file into a variable... In reply to
Don't know, i'll try it!!!

Andy

Quote Reply
Re: Getting a file into a variable... In reply to
or you could do...

$template = &html();

sub html {

print "Content-type: text/html\n\n";
print qq|

HTML CODE HERE

|;
}
[/blue]

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Getting a file into a variable... In reply to
umm.. no.. that would print out a binary byte

it should be..

$template = html ();

sub html {
my $html = qq~

blah

~;
return $html;
}

Jerry Su
widgetz sucks
Quote Reply
Re: Getting a file into a variable... In reply to
I use that code in one of my cgi scripts and it seems to work ok for me..

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)