Gossamer Forum
Home : General : Perl Programming :

Using if commands....

Quote Reply
Using if commands....
Hello. I am trying to write a script which allows you to define i you want to be sent an e-mail after every submission. I have used the following;

$sendmasteremail = 0; # 1 is yes, 0 is no

$submitteremail = 1;

and the the following code in the main script;

#################################################
# CODE #
#################################################

sub send_webmaster
{
if ($sendmaster ne 0){
open (MAIL, "|$sendmail -t") or &dienice("Can't access $sendmail!\n");
print MAIL "To: $webmasteremail \n";
print MAIL "From: $email ($organization)\n";
print MAIL "Reply-to: $email \n";
print MAIL "Subject: Someone has just submitted a site.... \n\n";
print MAIL "Someone just used your submission service to submit $url\; \n";
close(MAIL);
} else { }
}

# This sub sends the e-mail to the user (if option is enabled)
sub send_submitter
{
if ($submitteremail ne 0){
open (MAIL, "|$sendmail -t") or &dienice("Can't access $sendmail!\n");
print MAIL "To: $email \n";
print MAIL "From: $webmasteremail ($org)\n";
print MAIL "Reply-to: $webmasteremail \n";
print MAIL "Subject: Thanks for using our submission service... \n\n";
print MAIL "Thanks for using our submission service to submit the following info to the top 17 search engines \n";
print MAIL " \n";
print MAIL "URL: $url \n";
print MAIL "Site Title: $organization \n";
print MAIL "Address: $address \n";
print MAIL "City: $city \n";
print MAIL "State: $state \n";
print MAIL "Country: $country\n";
print MAIL "Tel. No.: $phone \n";
print MAIL "E-Mail Address: $email \n";
print MAIL " \n";
print MAIL "Thanks for using our submission service, and be sure to visit us often \n";
print MAIL "at $home. \n";
close(MAIL);
} else { }
}

##################################################
# END OF CODE #
##################################################

Any help would be much appreciated.

Thanks

A.J.Newby
webmaster@ace-installer.com
http://www.ace-installer.com


Quote Reply
Re: Using if commands.... In reply to
Help with what?

In Reply To:
open (MAIL, "|$sendmail -t") or &dienice("Can't access $sendmail!\n");
Shouldnt that be...

open (MAIL, "|$sendmail -t") or $die ("Can't access $sendmail!\n");


Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Using if commands.... In reply to
The following codes:

Code:

if ($sendmaster ne 0){


should simply be:

Code:

if ($sendmaster){


Same thing with the other variable.



Regards,

Eliot Lee
Quote Reply
Re: Using if commands.... In reply to
Actually i have now used the following code;

if ($variable eq "1") { &subok } else { &badsub }

Thanks anyway for your help...

Andy

Quote Reply
Re: Using if commands.... In reply to
Which you shouldn't, as you are doing unnecessary things.

As Eliot said,

if ($variable) {

would be perfectly fine. If you need explict values though, you should not be usingh

if ($variable eq "1") {

but instead:

if ($variable == 1) {

As I said in yor other post, == is for numerics, eq is for strings. placing 1 inside double quotes makes it a string, and that is totally unnecessary.

--mark



Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: Using if commands.... In reply to
$die?

umm.. i don't think there is a $... plus.. &dienice is probably a subroutine in the program it passes the error too...

also...

just to help youradds understand comparisons...

alphanumeric....

equals = "eq"
not equals = "ne"
um.. their are inequality ones too..
greater than = "gt"
greater or equal to = "ge"
less than = "lt"
less or equal to = "le"

numeric only
equals = "=="
not equals = "!="
greater than = ">"
greater or equal to = ">="
less than = "<"
less or equal to = "<="

ok.. the difference? you may not notice at first.. as do many other programmers.. i started perl using the numerical comparisons for alphanumeric strings.. an alphanumeric string is converted to a numeric value from the beginning of the string to the start of the first alphabetical or symbol (excluding a leading -)... ie: 123AbC -> 123..

if i did...
if (Tom == Bob)...
thats just like saying
if (0 == 0)....

if i did..
if (0 eq 5)
that would work correctly..

because alphanumeric comparisons are done based on ascii code value of the character... 0 is not equal to 5.. so the expression is false..

however.. if i did..

if (1000 gt 2)...
what does this look like? if ("1000" is greater than "2").. sure 1000 is greater than 2.. but "1000" is actually less than "2".. (notice the quotes).. putting numbers in an expression with a alphanumeric comparison operator assigns the compared values as strings.. strings are compared with ascii code value starting from the left... so perl compares.. "1" with "2"... "2" is greater than "1" in ascii code value..

if you said..
if (1000 > 2)...
this expression would be true..

you don't notice this common error until you start doing more in perl.. but i noticed a lot of links 2.0 modders doing it... they'd do this..

if ($rating gt "1") { $image = "1star.gif"; } etc...

that gives ratings of ten 1star.gif also..

Jerry Su
widgetz sucks
Quote Reply
Re: Using if commands.... In reply to
How do you use elseif tags? I want to have 3 different errors and then a success page (e.g a custom error for each missing field). I tried using 'elseif' tags, but they give me 500 Internal Server Errors.

Please Help Frown

A.J.Newby
webmaster@ace-installer.com
http://www.ace-installer.com

Quote Reply
Re: Using if commands.... In reply to
Code:

if ($in{'something')) {
&somesub;
}
elsif ($in{'somethingelse'}) {
&someothersub;
}
else {
&defaultsub;
}


Regards,

Eliot Lee
Quote Reply
Re: Using if commands.... In reply to
In Reply To:
if ($in{'something')) {
&somesub;
}
elsif ($in{'somethingelse'}) {
&someothersub;
}
else {
&defaultsub;
}
....that should be...

if ($in{'something'}) {
&somesub;
}
elsif ($in{'somethingelse'}) {
&someothersub;
}
else {
&defaultsub;
}


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