MAS4891 has asked for the wisdom of the Perl Monks concerning the following question:
Heres an example
if($value1==$value2){ print "Value1 and 2 are equal"; }else{ print "Value1 and 2 are not equal"; }
That is the correct way to put it right?
I think it is but for some reason it just wont work,
please help
Title edit by tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: If
by cwest (Friar) on Jun 08, 2000 at 18:59 UTC | |
So, if $value1 = 'a string' || $value2 = 'another string' You are using the incorrect syntax, otherwise, you should be good to go. If you're comparing strings, try this:
if ( $value1 eq $value2 ) {
print "I win!\n";
} else {
print "I loose.\n";
}
Enjoy!
-- Casey | [reply] |
|
RE: If (kudra: eq and ==)
by kudra (Vicar) on Jun 08, 2000 at 19:00 UTC | |
A recent discussion on the topic of == and eq can be found at Mysterious the ways: == and eq. ctweten beat me to replying, so this post is a kind of useless duplicate. | [reply] [d/l] |
|
DANGER!
by Fastolfe (Vicar) on Jun 09, 2000 at 03:01 UTC | |
You are doing a very dangerous open() call, passing the argument from your HTML form 'name' parameter directly to open. Thus, I can pass an argument of "mail me@example.com </etc/passwd; cat whatever |" and no one would be the wiser. It's not hard to change this into something considerably more destructive. Please consider running CGI apps with the -T and -w flags, which would catch major security issues like this. The 'perlsec' man page gives more detail. | [reply] |
|
RE: If
by dempa (Friar) on Jun 08, 2000 at 19:01 UTC | |
What does $value1 and $value2 contain? | [reply] [d/l] |
|
Re: If
by Ovid (Cardinal) on Jun 08, 2000 at 21:00 UTC | |
I usually wrap debug code in debug "start" and "end" tags. When I go back to clean up a large script, it's quick and easy. The first line of code above sets the font to a fixed width. This makes it easier to see a space embedded in the middle of a word. The next lines print the two passwords one above the other like so: This makes a character by character visual analysis easier and the single quotes make it obvious if you have padded white space before or after the password (but won't reveal an unchomped newline. Maybe you should use a <PRE> tag for that. Just a thought. Hope this helps! | [reply] [d/l] [select] |
|
Re: If
by MAS4891 (Novice) on Jun 08, 2000 at 19:31 UTC | |
They are strings,
What I want it to do is open a file thats named the same as
| [reply] [d/l] |
by chromatic (Archbishop) on Jun 08, 2000 at 19:51 UTC | |
There, that ought to work better. You did a couple of (syntactially valid, yes) things that could come back to haunt you, so I cleaned them up. First, you read the file in list context, into an array. If you just want the second line of it, we'll throw the first one away into a temporary variable and grab the second in another statement. That'll save reading the whole thing in if it's a potentially large file. (I don't particularly like the temporary variable throwaway thing here, but it's a good way to illustrate the point. @foo1 will give warnings with strict because it ought to be written $foo1 as it returns a scalar, not a list.) Notice also the chomp statement, to remove the input record separator (\n, a newline) from $file. That'll make things match successfully. Most of the other stuff just makes it work under the strict pragma. There's also a little more error checking. Be sure to do that. I like to wrap my open calls in blocks so I can use local on the filehandles. It doesn't matter much in small scripts, but as they have a way of growing into larger files, if you already have a filehandle named FILE active somewhere else, you'll clobber it here. And, yes KM, there ought to be an exit at the end of the error sub, so let's redact that in. :) | [reply] [d/l] |
by takshaka (Friar) on Jun 08, 2000 at 22:41 UTC | |
And if you could, $file would contain '1'; chomp ($file = <FILE>); | [reply] [d/l] |
by KM (Priest) on Jun 08, 2000 at 20:03 UTC | |
? Why not exit at the end of error()? Otherwise the script will continue, which would make no sense, and possibly cause other errors.
Cheers, | [reply] [d/l] |
by takshaka (Friar) on Jun 08, 2000 at 22:32 UTC | |
by KM (Priest) on Jun 08, 2000 at 22:48 UTC | |
by KM (Priest) on Jun 08, 2000 at 19:44 UTC | |
Actually, I would not open any file directly from user input. Others may disagree in this case, but I would validate that param('name') is something you expect, likely /^\w+$/, or something similar. If you had warnings turned on (which you should, -w) you would see that @file[1] is better written as $file[1]. You may want to consider putting all the HTML you are printing in a here doc, what you have is pretty hard on the eyes. As for why they do not match, you may want to chomp() the line you get from the file. If the file only contains one line, you should use this:
Cheers,
| [reply] [d/l] [select] |
|
Re: If
by MAS4891 (Novice) on Jun 08, 2000 at 21:55 UTC | |
Im 100% sure that @file1 is the pass, I already checked that,
| [reply] |
|
Re: If
by omega13 (Initiate) on Jul 23, 2000 at 14:27 UTC | |
| [reply] |
|
Re: If
by MAS4891 (Novice) on Jun 08, 2000 at 22:12 UTC | |
I got it to work! Finally... I just made a file for the
| [reply] |
|
Re: If
by MAS4891 (Novice) on Jun 08, 2000 at 20:05 UTC | |
Ok, I still cant get it to work... just one question though
| [reply] |
by zdog (Priest) on Jun 08, 2000 at 20:11 UTC | |
chomp LIST chomp This safer version of chop removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the English module). It returns the total number of characters removed from all its arguments. It's often used to remove the newline from the end of an input record when you're worried that the final record may be missing its newline. When in paragraph mode ($/ = ""), it removes all trailing newlines from the string. If VARIABLE is omitted, it chomps $_. Example: You can actually chomp anything that's an lvalue, including an assignment: If you chomp a list, each element is chomped, and the total number of characters removed is returned. (I got this from perlfunc:chomp) -- zdog (Zenon Zabinski) | [reply] [d/l] [select] |
by kudra (Vicar) on Jun 08, 2000 at 20:15 UTC | |
| [reply] |
by David Caughell (Monk) on Aug 05, 2003 at 07:18 UTC | |
Hi, Mas, it was just a few months ago that I was asking such questions, so I thought I would offer a little help. Firstly, chomp removes the newline characters from a scalar value (a string). That's not EXACTLY what it does, but it's close enough, for you, for now. (Since that's what you're probably using it for, and it will probably work.) You can find this information by going to http://www.perldoc.com/perl5.6/pod/perlfunc.html. Perl Functions are the easiest things to understand about Perl, with the exception of weird ones like map (it's cool, it's helpful, but it's weird, and I can't use it reliably yet). You probably want to get a good book on perl (generally speaking, one that's published by O'Reilly http://perl.oreilly.com/). Leaf through a few and select one that you can understand. I'm on the second chapter of Programming Perl, and I've had to read a few sections twice, but it's a good book so far. Something that you may find helpful (because I did, when I looked up the substr() function), is: a 1value (one-value) means the left side of the equation. So this would be an example of using the substr() function as a 1value:
Lastly, please look up functions before you ask about them (I've made this mistake quite a few times), and ask questions like this in the chatterbox before you post a message about them. Take care, "For fate which has ordained that there shall be no friendship among the evil has also ordained that there shall ever be friendship among the good." - Plato / Socrates | [reply] [d/l] |