Forum Moderators: coopster & phranque

Message Too Old, No Replies

How Do I use "If" and "Else"

How Do I use "If" and "Else"

         

lindajames

4:47 pm on May 8, 2003 (gmt 0)

10+ Year Member



Hi,

I have a script, that reads the value of "email" parsed to the script, like this:

my $query = new CGI;
my $email = $query->param('email');

And i put the value of email in a hidden form field like this:

<input type="hidden" name="email" value="$email">

However, i want $email to be the value of this hidden field only, if the email address ends with @yahoo.com otherwise i want it to print my email address as the value of that hidden field

Im assuming this can be done using If and Else statements but i dont know how. Can anyone tell me how this can be done?

Any help would be appreciated.

Cheers
Linda

Robber

8:04 pm on May 8, 2003 (gmt 0)

10+ Year Member



Hi,

try something like this:

if($email =~ m/@yahoo.com$/){
print '<input type="hidden" name="email" value="$email">';
}else{
print '<input type="hidden" name="email" value="abc@xyz.com">';
}

The regex might need a bit of a tweak though.

Cheers

DrDoc

8:08 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@ needs to be escaped when using Perl...

if($email =~ /\@yahoo\.com$/) {
print '<input type="hidden" name="email" value="$email">';
}
else {
print '<input type="hidden" name="email" value="abc\@xyz.com">';
}

Robber

10:50 pm on May 8, 2003 (gmt 0)

10+ Year Member



Cheers Doc - knew I'd forgotten something!

Marcia

11:12 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using conditional logic is one of the basics in any programming language. It's elementary programming theory, independent of which language it is.

Depending on which programming language it is that someone wants to use and learn, the best thing is to look online for the official documentation for the syntax and usage conventions and read through to learn it from the ground up.

jeremy goodrich

11:14 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, Marcia, along those lines Perl documentation [perldoc.com] is a great resource for looking up syntax.

afaik, it doesn't get more 'official' for perl than that one. Great reference for all the if () elsif () etc. syntax. ;)

Robber

11:20 pm on May 8, 2003 (gmt 0)

10+ Year Member



A bit off topic but in reply to the last couple of posts, perhaps the forum charter could include links to documentation/authority websites such as perldoc, php.net etc

amoore

12:00 am on May 9, 2003 (gmt 0)

10+ Year Member



DrDoc - when you use single quotes as you have in your print statement, the contents are not evaluated for variables. That means your $email will not get evaluated and you don't need to backslash the "@".