Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl Syntax Help

         

drewsuf

8:27 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



Hi,
I'm using NewsPro, a CGI program for my web site. I'm having a bit of trouble configuring the Perl script to display the HTML output I want. I guess that's probably because I have very little knowledge of Perl.

Here is what I have:


sub DoNewsHTML {
if ($uservar1 ne "") {
$newshtml .= qq~ <p><strong><a href="$uservar1" target="_blank">$newssubject</a> </strong><br><font size="2">Posted
$newsdate by <a href="mailto:$newsemail">$newsname</a><br> ~;
}
$newshtml .= qq~ <p><strong>$newssubject</strong><br><font size="2">Posted
$newsdate by <a href="mailto:$newsemail">$newsname</a><br> ~;
}
if ($uservar1 ne "") {
$newshtml .= qq~ <img src="$uservar3" align="right" border="0"></a> ~;
}
$newshtml .= "</p>";
}
$newstext<br> ~;
if ($uservar1 ne "") {
$newshtml .= qq~ <i>Source: <a href="$uservar1" target="_blank">$uservar2</a></i> ~;
}
$newshtml .= "</p>";
}

When I try to run the program, I get a fatal error. Can anyone tell me what I'm doing wrong? I think I have some extra brackets or something. I'm also a little unsure about the conditions. If you see the problem, it would be great if you could just fix it for me.

Just for reference, when I use the following code, everything works fine.


sub DoNewsHTML {
$newshtml = qq~ <p><strong><a href="$uservar1" target="_blank">$newssubject</a> </strong><br><font size="2">Posted
$newsdate by <a href="mailto:$newsemail">$newsname</a><br>
$newstext<br> ~;
if ($uservar1 ne "") {
$newshtml .= qq~ <i>Source: <a href="$uservar1" target="_blank">$uservar2</a></i> ~;
}
$newshtml .= "</p>";
}

So I think it's one of the conditionals that I added that is causing the problem.

Thanks in advance ;)

[edited by: drewsuf at 10:36 pm (utc) on Jan. 22, 2003]

andreasfriedrich

8:55 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] drewsuf.

Be sure to read Marcia`s WebmasterWorld Welcome and Guide to the Basics [webmasterworld.com] post.

The function you posted works fine for me.

You might want to test the script from the command line with the -w switch to make the perl interpreter report warnings.

If you cannot figure out the problem yourself post the error messages you get.

Andreas

drewsuf

10:45 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



You're right about that code working - I posted the wrong one.

I edited my post so you will see the faulty code now.

Thanks.

andreasfriedrich

10:59 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your curly braces do not match:

sub DoNewsHTML {[1]
if ($uservar1 ne "") {[2]
$newshtml .= qq~ <p><strong><a href="$uservar1" target="_blank">$newssubject</a> </strong><br><font size="2">Posted
$newsdate by <a href="mailto:$newsemail">$newsname</a><br> ~;
}[2]
$newshtml .= qq~ <p><strong>$newssubject</strong><br><font size="2">Posted
$newsdate by <a href="mailto:$newsemail">$newsname</a><br> ~;
}[1]
if ($uservar1 ne "") {[3]
$newshtml .= qq~ <img src="$uservar3" align="right" border="0"></a> ~;
}[3]
$newshtml .= "</p>";
}[no opening curly brace]
$newstext<br> ~;
if ($uservar1 ne "") {[4]
$newshtml .= qq~ <i>Source: <a href="$uservar1" target="_blank">$uservar2</a></i> ~;
}[4]]
$newshtml .= "</p>";
}[no opening curly brace]

I suggest you use an editor that will show you matching braces and that uses code highlighting to avoid such errors in the future.

Have a look at the Editor forum at WebmasterWorld for some suggestions on nice text editors.

Andreas

andreasfriedrich

11:03 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I edited my post so you will see the faulty code now.

It would have been a good idea to post the new code in your new message. The editing feature is meant to allow you to correct spelling mistakes or other small glitches.

Changes that change the meaning of a message should be avoided and just posted as a new message. This will help other readers who do not know the editing history of a certain thread.

Andreas

drewsuf

11:04 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



Heh, well I'm just using Notepad as my editor.

I'm still not quite sure how the curly braces work. I guess they're like the >< commands in HTML. Could you please fix them for me? I'm not planning on using Perl very often, so I don't need an editor. I just need to customize this one setting for NewsPro.

Thanks a lot :)

andreasfriedrich

11:16 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm still not quite sure how the curly braces work.

In Perl curly braces are used to delimit a block of code. Blocks may be nested.

function

if(this)
do this

if($uservar1) { 
executed when $uservar1 is true, i.e. ne '';
executed when $uservar1 is true, i.e. ne '';
}

This code is syntactically correct:

sub DoNewsHTML { 
#
if ($uservar1) {
$newshtml .= qq~ <p><strong><a href="$uservar1"
target="_blank">$newssubject</a> </strong><br><font
size="2">Posted $newsdate by <a href="mailto:$newsemail"
>$newsname</a><br> ~;
} # end of if ($uservar1 ne "") {
#
# this is done unconditionally
$newshtml .= qq~ <p><strong>$newssubject</strong><br><font
size="2">Posted $newsdate by <a href="mailto:$newsemail"
>$newsname</a><br> ~;
#
if ($uservar1) {
$newshtml .= qq~ <img src="$uservar3" align="right"
border="0"></a> ~;
} # end of if ($uservar1 ne "") {
#
# this is done unconditionally
$newshtml .= "</p>";
$newstext<br> ~;
#
if ($uservar1) {
$newshtml .= qq~ <i>Source: <a href="$uservar1"
target="_blank">$uservar2</a></i> ~;
} # end of if ($uservar1 ne "") {
#
# this is done unconditionally
$newshtml .= "</p>";
}

I do not know whether it will do what you want.

Andreas

drewsuf

11:41 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



Hmmm, I tried using that code exactly as you posted, and am getting the following error:
An error has occurred. The exact description of the error is: Untrapped Error: syntax error at ndisplay.pl line 53, near "br>" Compilation failed in require at /u/web/drewla/cars/newspro.cgi line 789.

Not sure what's wrong.

Thanks again. That description of conditionals helped my understanding of the code. I'm still a little shaky with syntax though.

andreasfriedrich

11:51 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I missed that error:

$newstext<br> ~;
is no valid perl.

The code works when you comment out that line. What did you try to achieve with that line of code?

Andreas

drewsuf

12:00 am on Jan 23, 2003 (gmt 0)

10+ Year Member



The $newstext command displays the text that is entered via an HTML form. It is necessary for the script to operate.

Is there any way to fix the problem?

andreasfriedrich

12:15 am on Jan 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IŽm not sure where you want the $newstext to go into your html code but here is one way that would work:

$newshtml .= qq~$newstext<br>~;

Note that qq~some string~; is just some fancy perl way of quoting the words "some string". In fact it is syntactically equivalent to those double quotes. But you do not have to escape double quotes when you want to use them within your string. So you can put the $newstext variable everywhere in your code between qq~ and ~.

Andreas

drewsuf

3:37 am on Jan 23, 2003 (gmt 0)

10+ Year Member



Thanks! It works perfectly

drewsuf

5:36 am on Jan 23, 2003 (gmt 0)

10+ Year Member



Ugh, I got the script working, but the output on my website is a little weird. When I post a news article with NewsPro, the article always appears twice if it has a picture. Check it out for yourself at <snip>

This is the code I'm using for my HTML output:
sub DoNewsHTML {
#
{
$newshtml .= qq~ <strong><a href="$uservar1"
target="_blank">$newssubject</a></strong><br><font
size="2">Posted $newsdate by <a href="mailto:$newsemail"
>$newsname</a><p> ~;
} # end of if ($uservar1 ne "")
#
if ($uservar3) {
$newshtml .= qq~ <a href="$uservar1" target="_blank"><img src="$uservar3" align="right"
border="0"></a> ~;
} # end of if ($uservar3 ne "") {
#
$newshtml .= qq~$newstext<br>~;
#
if ($uservar1) {
$newshtml .= qq~ <i>Source: <a href="$uservar1"
target="_blank">$uservar2</a></i></font> ~;
} # end of if ($uservar1 ne "") {
#
$newshtml .= qq~ <hr color="#ffffff" size="1" width="30%"><p> ~;
}

Is there anything in this code that could be causing the double posts? This is basically the only Perl setting I've messed around with, so I think it's either a problem with the program or with my coding.

Thanks again.

[edited by: jatar_k at 4:56 pm (utc) on Jan. 23, 2003]
[edit reason] no url's please [/edit]

andreasfriedrich

12:56 pm on Jan 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is not the function you posted that is causing this. If you look at the source code of your page youŽll notice that the entry about pontiac that appears twice has different newsids. So the error must be somewhere in the logic that generates entries.

Andreas