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: Intermediate:
Limit Print

 



cir
Novice

Dec 15, 2000, 8:05 AM

Post #1 of 11 (897 views)
Limit Print Can't Post

I'm doing a random quote on my site that is powered by perl(duh!) and javascript. I've got everything worked out, except if a document.writein() is too long the browser will produce an error. How can I use perl to limit the size printed into javascript.

http://cirw.cjb.net
http://cbible.cjb.net
http://gmail.cjb.net


Jasmine
Administrator

Dec 16, 2000, 11:06 AM

Post #2 of 11 (888 views)
Re: Limit Print [In reply to] Can't Post

Perl is a server-side programming language. JavaScript is a client-side scripting language.

Perl can't affect JavaScript's writein funcion unless the form is submitted to the server for processing.

Or am I misunderstanding your question?


cir
Novice

Dec 16, 2000, 12:22 PM

Post #3 of 11 (886 views)
Re: Limit Print [In reply to] Can't Post

I do understand that. But I wrote a perl program that prints random quotes. My perl has no errors, no problems. Basicly this is how it works, it opens the file with the quotes in it. Each quote is seperated by |. So it splits the file up with this. Then I run the following (perl)command:
print "document.writeIn(\'$quote\');";
When I load the script it works just fine. However when I run it on my site (<script src=location.pl"></script>) it(the browser) produces the error: unterminated string constant.

What I want to know is: is there a way(in perl) to limit the var $quote to xx amount of charecters? And if $quote goes over can I split it up? Changing each quote to fit size restrictions is impractical, there are over 43,000 of them. :)

http://cirw.cjb.net
http://cbible.cjb.net
http://gmail.cjb.net


japhy
Enthusiast

Dec 16, 2000, 12:47 PM

Post #4 of 11 (885 views)
Re: Limit Print [In reply to] Can't Post

I think your problem could be due to the quote having an apostrophe in it. I'm not well-versed on figuring out JavaScript's error messages (or rather, a given browser's interpretation of a JavaScript error message), but this could be a definite possibility.

Perhaps you should backslash all ' characters in the quote before you display it, or perhaps you should use "..." around the quote:


Code
$quote =~ s/'/\\'/g;  # backslash all ' chars 
print "document.writeln('$quote');";

# or

print qq[document.writeln("$quote");];

I have an article at PerlGuru.com's learning center, Quoting Operators and Interpolation, where you can read more about q(), qq(), and the like.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


cir
Novice

Dec 16, 2000, 2:26 PM

Post #5 of 11 (880 views)
Re: Limit Print [In reply to] Can't Post

Already ahead of you. That was one of the first things I did. Both browsers(NS & IE) limit the size javascript can print. Which is what that error is(I'm better @ javascript than perl Smile) What I want to do is stop the error all together by limiting the size of the var $quote. I know that length($quote) would return the no of charecters in the string. But how can use this? I can't just do:
split(/length($quote)\10/, $quote);
can I?

http://cirw.cjb.net
http://cbible.cjb.net
http://gmail.cjb.net


japhy
Enthusiast

Dec 16, 2000, 2:53 PM

Post #6 of 11 (879 views)
Re: Limit Print [In reply to] Can't Post

Oh, you're in luck. I recently posted a quiz to the "Perl Quizzes" forum, and it was based on splitting a string into pieces. Here's a solution I strongly suggest:


Code
$len = 30;  # 30 chars at a time 
push @parts, substr($string, 0, $len, "") while $string ne "";

Then you do something with each element of @parts. In your case, you don't really need the array at all:


Code
$len = 30;  # 30 chars at a time 
print "document.write('", substr($string, 0, $len, ""), "');\n"
while $string ne "";

I believe that will quell the JavaScript error message.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


cir
Novice

Dec 16, 2000, 3:27 PM

Post #7 of 11 (877 views)
Re: Limit Print [In reply to] Can't Post

There are a few holes in your code. First, while is an unlimited loop!(since $print will never be nothing) Second, it is stuck printing the first 30 charecters! My fix is the following:

$len = 30; # 30 chars at a time
$pos = 0;
while (length($print) >= $pos) {
print "document.writeIn(\"" . substr($print, $pos, $len) . "\");\n";
$pos += $len;
}

That way the postition is allways moving. With this updated code it works great! However, my javascript-phobic browser is still producing the same error. When I get home I'll try it on NS, and that IE. Maybe it will turn out differently. If not, I'll take a look my javascript book, which is stupid because document.write is the simplest command!

Thanks for your help!

http://cirw.cjb.net
http://cbible.cjb.net
http://gmail.cjb.net


japhy
Enthusiast

Dec 16, 2000, 4:41 PM

Post #8 of 11 (876 views)
Re: Limit Print [In reply to] Can't Post

I beg to differ, cir. 4-argument substr() modifies the string, and I'm reading 30 character chunks and replacing them with nothing (effectively destroying the string, by the way).

If you try my code, you'll see the while-loop does terminate, because eventually, $string becomes empty:


Code
$text = join "", 'a' .. 'z'; 
$chunk = 7;
print "CHUNK: ", substr($text, 0, $chunk, ""), "\n"
while $text ne "";

The output I receive is:

CHUNK: abcdefg
CHUNK: hijklmn
CHUNK: opqrstu
CHUNK: vwxyz

If you ran my code and got errors, I'd like to know what they were, but I think you jumped the gun when you found "bugs" in my code.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


cir
Novice

Dec 17, 2000, 9:27 AM

Post #9 of 11 (872 views)
Re: Limit Print [In reply to] Can't Post

Maybe it's just my server that doesn't destroy the string. It went on forever. Well anyway, I figured it out with substr() and it works great! Thanks for the advice.

http://cirw.cjb.net
http://cbible.cjb.net
http://gmail.cjb.net


japhy
Enthusiast

Dec 17, 2000, 9:32 AM

Post #10 of 11 (871 views)
Re: Limit Print [In reply to] Can't Post

You might be running an older version of Perl, then. Glad to know you have the code working now, though.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


cir
Novice

Dec 17, 2000, 10:24 AM

Post #11 of 11 (869 views)
Re: Limit Print [In reply to] Can't Post

Must be... I just added one line of code after substr() executed. I had it print out the value of quote after substr did its stuff, and $quote didn't change. I'm just glad to know how I can use substr()!

http://cirw.cjb.net
http://cbible.cjb.net
http://gmail.cjb.net

 
 


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

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