CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
why does this not work

 



jason
Deleted

Dec 2, 2000, 5:25 PM

Post #1 of 2 (763 views)
why does this not work Can't Post

please help this is driving me crazy?

$output_stuff = qq(<CENTER><h2>You must log in <a href="$login_page">HERE</a></h2>You came from $ENV{'HTTP_REFERER'}.</CENTER> );

open (FILE, "<$template") &#0124; &#0124; warn "Can't open template: $!\n";

$template_string = <FILE>;

$template_string =~ s#(<!-- CONTENT --> )\s*(<!-- /CONTENT --> )#$1\n$output_string\n$2#s;

print $template_string;

exit;


rGeoffrey
User / Moderator

Dec 6, 2000, 7:26 AM

Post #2 of 2 (763 views)
Re: why does this not work [In reply to] Can't Post

It does not work because you are only reading the first line of the file into $template_string.

You need to slurp the whole file into $template_string so you can play with it.

A simple way to do this is...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$template_string = join ('', <FILE> );
</pre><HR></BLOCKQUOTE>

A more clever way to do it is to play with $/ (the input record separator that is "\n" by default). But because $/ is frequently used behind the scenes it is important that if you do play with it you ALWAYS do it inside a block so you can undo your changes as quickly as possible. Also $/ needs to have 'local' not 'my' because is is one of those special variables.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
local $/;
$template_string = <FILE>;
}
</pre><HR></BLOCKQUOTE>

Or to get it back on one line you can throw in a 'do'...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$template_string = do { local $/, <FILE> };
</pre><HR></BLOCKQUOTE>

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives