Gossamer Forum
Home : General : Perl Programming :

Two small questions...

Quote Reply
Two small questions...
1) Is it possible to require another CGI script that's written in another language? I want to write a small Perl script that will log a user into something automatically, but the other app is written in C or C++.

2) Do these all do the same thing?

if ($somevar eq "") { &dosomething; }

if ($somevar == '') { &dosomething; }

if (!$somevar) { &dosomething; }

Or is there some vital difference between them? (Oops! I realise that the first two are slightly different, I just mean do they have the same ACTION)

Cheers,
adam

[This message has been edited by dahamsta (edited April 08, 1999).]
Quote Reply
Re: Two small questions... In reply to
 
Quote:
2) Do these all do the same thing?

if ($somevar eq "") { &dosomething; }

if ($somevar == '') { &dosomething; }

if (!$somevar) { &dosomething; }

Or is there some vital difference between them? (Oops! I realise that the first two are slightly different, I just mean do they have the same ACTION)

The action of the first two are identical but use "eq" for strings and use "==" for numeric checks. That is:

if ($something eq "string")
if ($something == 5)

The last one will execute $dosomething if $somevar is greater than 0 or not NULL (it is a TRUE/FALSE test where 0 or NULL equal FALSE, a 1 or non-null value represent TRUE). This is a simplistic explanation and may not be absolutely correct or complete.
Quote Reply
Re: Two small questions... In reply to
Hi,

1. Use system() to run the other program, or backticks `` if you want to capture the other programs output and manipulate it. You can't just require() it and then run it by calling a subroutine or something (although yes, you can embed C in Perl and Perl in C but that's getting pretty advanced). Wink

2. They are all different:

$something eq "" -- TRUE if and only if $something is equal to "" or $something is undefined.

$something == "" -- TRUE if and only if $something is a string that can't be converted to a number, or $something is 0, or $something is undefined. You'll notice "a" == "" is TRUE, something you might not expect. One good reason not to use numerical comparison when comparing strings.

!$somevar -- is TRUE if and only if $somevar is 0 or "" or undefined.

Hope that helps,

Alex
Quote Reply
Re: Two small questions... In reply to
Oops, I didn't see the "!" in the last condition. Sorry 'bout that.
Quote Reply
Re: Two small questions... In reply to
Hi gents,

1) Ok, I've had a little look around the perl docs, and although it's mostly gibberish to me Smile I've seen that system() executes a system command and that there's a related function -- exec(). So now so, here's what I want to do:

The other program is written in C or C++, and it's a web-based mailer. I want to use my Perl CGI to get the users username from $ENV{'AUTH_NAME'} or a cookie, read the rest of their data from a flat-file database, and execute the mailer cgi using the variables I've now got. Normally I'd do that with { $var=$val; $var2=$val2; &login; }.

So is it possible to feed these variable into the other program using system() or exec()? And wouldn't it be exec(), because I won't be returning to the login script?


2) Well, I think I get the matching things pretty much now, apart from one question -- do the variables actually have to exist? I'm asking because although I can get them to work the way I want with a little trial and error, I'd much prefer to actually understand why.

So if I fed in:

htp://myserver.com/script.cgi?var1=&var2=val2

I reckon that if ($var1 eq "") or if ($var1 == "") or if (!$var1) would all give me TRUE right?

But what if I used it for a variable that wasn't in the query, say $var3?

Thanks again folks, this is all very helpful,

adam