Web Hosting Talk







View Full Version : What's wrong with this scripts logic?


Dylan
04-12-2002, 10:46 PM
Any help would be appreciated:

$tmpcntr = 1;
open(TEMPLATE, $template_file);
while(<TEMPLATE> )

{
$buffer .= $_;
if ($_ =~ /<\/BODY/i){$_ .= "<a href=\"http://d.com\">dot</a>";$buffer .= $_;}
}

close(TEMPLATE);
$temp = "<a href=\"http://d.com\">dot</a>";
if ($buffer =~ /<\/BODY/i){ $bufer .= $temp;}

bitserve
04-13-2002, 12:24 AM
Um, what's it supposed to be doing?

It looks like it's doing this:

1. Open template file for reading.
2. While adding each line to $buffer, if it contains "</BODY" or "</body", then add a hyperlink also.
3. Close template file.
4. Store hyperlink in $temp.
5. If $buffer contains "</BODY" or "</body", then add $temp to $buffer.

If that's what you intended, then the logic is fine.

Dylan
04-13-2002, 12:33 AM
Hi Mark,

The template looks like this:

</body>
</html>

When the script does its thing, it looks like this:

</body>
</body>
<a href="http://d.com">dot</a></html>


I'm trying to get it to edit the template as follows:

<a href="http://d.com">dot</a>
</body>
</html>

Thanks!

banner
04-13-2002, 12:33 AM
I'm not sure if this was copied and pasted from your script, but if it is the problem might be due to a spelling error. In the last line you check $buffer for '/BODY' but then within the if block, you say "$bufer .= $temp", I think that it should be "$buffer .= $temp;". I hope this helps.

Chris Spangler

Edit: You posted while I was typing this and from the looks of things, this may not be the problem.

Dylan
04-13-2002, 12:42 AM
Hi banner,

I tried changing bufer to buffer, but then the template looks like this:

</body>
</body>
<a href="http://d.com">dot</a></html><a href="http://d.com">dot</a>

banner
04-13-2002, 01:34 AM
Well, I think that I may know why the output is the way it is. The reason for the two occurrences of the link is because your are appending it to the buffer twice (once inside the while loop and once after you close the template). The reason why the link appears after the /BODY is because /BODY must be in the buffer before the link will be added and you are just appending the link to the end of the buffer.
My recommendation would be to set it up so that when you find /BODY you replace it with "link</body>". This is the easiest way I can think of to get the desired results. To do this the code would look something like this:

$temp = "<a href=\"http://d.com\">dot</a></body>";
if ($buffer =~ /<\/BODY/i){ $buffer =~ s/<\/BODY/$temp/g;}


Oh, and you may want to remove the section where you append the link to the buffer inside the loop. Either that or remove the similar section after you close the template.

Does this look similar to what you want?

Chris Spangler

Tim Greer
04-13-2002, 09:41 AM
You don't have all your code, but by the looks of it, and the description of your problem, change it to this:



# First, clean up this code and remove any
# irrlevant stuff...
# Just do this below, for all the functions
# and it'll work how you want:

# START HERE:

open (TEMPLATE, $template_file) or die "Can't open $template_file $!";
while (<TEMPLATE>) {
s,</BODY,<a href="http://d.com\">dot</a>\n</body",ig;
# Now that you've done the checking
# THEN you can append (after)!
$buffer .= $_;
}
close(TEMPLATE) or die "Close failed for file descriptor TEMPLATE on $template_file $!";

# If you want, you can do a:

print $buffer;

# after those functions to test and see what the
# result/output is, and I imagine you'lll want it
# to print the result or perhaps update the new
# information to a file.

# BTW, what are these lines below suppose to be
# for? This is confusing...

#$temp = "<a href=\"http://d.com\">dot</a>";
#if ($buffer =~ /<\/BODY/i){ $bufer .= $temp;}
# ^^^^^ Remove that crap too, it's not needed.

# Those are supposed to do what anyway?



That should fix the problem you are having...

bitserve
04-13-2002, 10:38 PM
Here's how I would do it then:



open (TEMPLATE, "$template_file");
while (<TEMPLATE>) {
if ($_ =~ /<\/body/i) {
$buffer .= "<a href=\"http://d.com\">dot</a>\n";
}
$buffer .= $_;
}
close (TEMPLATE);

Dylan
04-13-2002, 10:54 PM
Thanks for all the replies! I'm going to go and play around with these suggestions now... :D

Tim Greer
04-13-2002, 10:59 PM
Originally posted by bitserve
Here's how I would do it then:



open (TEMPLATE, "$template_file");
while (<TEMPLATE>) {
if ($_ =~ /<\/body/i) {
$buffer .= "<a href=\"http://d.com\">dot</a>\n";
}
$buffer .= $_;
}
close (TEMPLATE);



Okay, let's clean this up a little though, first:



open (TEMPLATE, "$template_file");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You needn't put quotes around a lone $variable. You should check the call for errors, too.

open (TEMPLATE, $template_file) or die $!;

while (<TEMPLATE>) {
^^^^^^^^^^^^^^^^^^^^
Well, that can't get any better. :-)


if ($_ =~ /<\/body/i) {
^^^^^^^^^^^^^^^^^^^^^^^
Why all of this?

$buffer .= "<a href=\"http://d.com\">dot</a>\n";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Do it all at once anyway, if it finds it. But that
works too...

I'd still do this:

s,</body,<a href=\"http://d.com\">dot</a>\n</body,ig;

And possibly even this, to make it more efficient,
assuming the syntax and/or format is standard.

s,^\s*</body,<a href=\"http://d.com\">dot</a>\n</body,i;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That way, it will preserve the closing body tag
line, or at least the part of the tag to be
replaced. It looked to me like he wanted to keep that.

}
^
Don't need that now.

$buffer .= $_;
}
^^^^^^^^^^^^^^^^
Nifty stuff... :-)

close (TEMPLATE);
^^^^^^^^^^^^^^^^^
Don't forget to check the calls:

close (TEMPLATE) or die $!;



My above example, less the explaining, in my previous post, should do exactly what he wants. Mind you, he didn't specify if a file is updated, or if this is just parsing a file to modify the output to someone's web browser or not.

Dylan
04-14-2002, 12:02 AM
:) With regards to file updates, there's no need to worry. It's sort of a link script, so whenever a new link is added, the script will verify the template.

:beer:

bitserve
04-14-2002, 06:36 PM
As long as we're giving opinions on eachother's code:

[QUOTE]Originally posted by Tim_Greer
[B]You don't have all your code, but by the looks of it, and the description of your problem, change it to this:



open (TEMPLATE, $template_file) or die "Can't open $template_file $!";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You should put quotes around a file name, whether you have to or not,
for the sake of standards. If you're not going to code like everyone
else, then don't check it in.

You should also not kill a program because of an error. Especially if
it's a CGI program. If you really want error checking, then add it in.

while (<TEMPLATE>) {
^^^

Why did you indent three space here and then four further down? Keep it
standard, or don't check it in.

s,</BODY,<a href="http://d.com\">dot</a>\n</body",ig;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The intent of the program was to add a hyperlink on a line before the
line with the </body> tag, not to substitute the beginning of all
</body> tags with a hyperlink and your own beginning of the body tag.

In yours, you would be replacing:

</BODY with hyperlink</body

You've just changed the case of the body tag, which was not our intent.

In yours, you would be adding multiple hyperlinks if we had:

</body></body>

Agreeably, both of our scripts would multiply if we had:

</body>
</body>

If we wanted to be super smart, we'd count the number of </body> tags in
the file, and just add the hyperlink before the last one.

And why are you using a comma for a delimiter? If it's because you didn't
want to escape your slashes, you should have used the pipe, which is more
standard.

As long as you're paranoid about "checking calls", why didn't you evaluate
your substitution to see if it was successful?

# Now that you've done the checking
# THEN you can append (after)!
$buffer .= $_;
}
close(TEMPLATE) or die "Close failed for file descriptor TEMPLATE on $template_file $!";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

again with the die?



With some error checking related to files.



open (TEMPLATE, "$template_file") or $error = $!;
if (!$error) {
while (<TEMPLATE> ) {
if ($_ =~ /<\/body/i) {
$buffer .= "<a href=\"http://d.com\">dot</a>\n";
}
$buffer .= $_;
}
}
close (TEMPLATE) or $error .= $!;

Tim Greer
04-14-2002, 07:02 PM
Originally posted by bitserve
As long as we're giving opinions on eachother's code:


Try not to take offense. I was stating the information for the user's sake and for good programming practices.


Originally posted by Tim_Greer
You don't have all your code, but by the looks of it, and the description of your problem, change it to this:


open (TEMPLATE, $template_file) or die "Can't open $template_file $!";


You should put quotes around a file name, whether you have to or not, for the sake of standards.


Actually, it's encouraged that you do *not* put it (the variable) in the double quotes.


If you're not going to code like everyone else, then don't check it in.


Again, you shouldn't take offense.


You should also not kill a program because of an error. Especially if it's a CGI program. If you really want error checking, then add it in.


When did he ever say this was ran via CGI? Perl and CGI are not the same thing. For all I knew, he was running a cron task executing a script (or manually via command line) to have it automatically parse and modify template files. I wouldn't assume either way, I simply stated to check the calls -- you have a problem with that? Moreover, look at this logically. Why didn't you do proper checking then (any!)? I did, you did none at all. What do you think happens if that file fails to open in CGI anyway? And your attempt to do error checking in your example below, was a joke (seriously, was it? Since it didn't actually do anything, that is!)




while (<TEMPLATE>) {
^^^


Why did you indent three space here and then four further down? Keep it standard, or don't check it in.


I think it looks prettier that way. I'd have to question your motivation here -- obviously not to help the user to use better programming practices, therein helping them better understand and troubleshoot more future problems themselves. Apparently, you'd rather argue about stupid things.

There's a difference between programming well and nitpicking about things you personally don't like. I follow the standards, and I'm not going to worry about formatting perfectly on a web forum post. If you're going to worry about that, I suggest you go back and fix your own indention specs.




s,</BODY,<a href="http://d.com\">dot</a>\n</body",ig;


The intent of the program was to add a hyperlink on a line before the
line with the </body> tag, not to substitute the beginning of all
</body> tags with a hyperlink and your own beginning of the body tag.


If you understood, you'd see that's exactly what that code does (put a hyperlink before the closing body tag). How many closing body tags will he have? If that's a problem, why don't you just remove one character? (That's the 'g', in case you don't know). Your code example did *exactly* the same thing as this above code does, you know! And, you're right, I think all the backwacking and slashes look too cluttered.


In yours, you would be replacing:

</BODY with hyperlink</body


No, read it again, skippy. I can suggest a good Perl programming book to help you understand. I mean, since we're being all mean and such to each other... why beat around the bush?


You've just changed the case of the body tag, which was not our intent.


HTML is case insensitive, don't worry about it.


In yours, you would be adding multiple hyperlinks if we had:

</body></body>



Yes, that is true (oh, I see what you mean above). Had he specified he'd have multiple closing body tags, or that was possible, I'd have changed the syntax of the code. I'm not psychic, but I'm glad to see one of us is. You *can* remove the global on the regex, I know that one character is a big change.



Agreeably, both of our scripts would multiply if we had:

</body>
</body>


B.. B... But... but, I thought you were psychic? :-)


If we wanted to be super smart, we'd count the number of </body> tags in the file, and just add the hyperlink before the last one.


That's not so 'super smart', never assume what the user wants.


And why are you using a comma for a delimiter? If it's because you didn't want to escape your slashes, you should have used the pipe, which is more standard.


I use whatever appeals to me, that's not "more standard", it either is or isn't. Talk to Randall, Tom and Larry if you have problems with commas. I corrected a major oversight by you, and you are being irrational about this in response.



As long as you're paranoid about "checking calls", why didn't you evaluate your substitution to see if it was successful?


Because, skippy, it doesn't need to be successful. If he doesn't have an ending body tag, he doesn't want to add the hyperlink. If he did, he could state so. If the ending body tag is present, it'll work. I didn't assume someone would act like such a tool, simply because I stressed the importance of good programming practices. Mind you, I'm not talking about "preferences", but things that are needed. Feel free to argue about trivial personal preferences all you like though -- and that it's going to educate people.



$buffer .= $_;
}
close(TEMPLATE) or die "Close failed for file descriptor TEMPLATE on $template_file $!";


again with the die?


Yeah, again with me and those 'silly' call checks and you with none.



With some error checking related to files.


open (TEMPLATE, "$template_file") or $error = $!;
if (!$error) {
while (<TEMPLATE> ) {
if ($_ =~ /<\/body/i) {
$buffer .= "<a href=\"http://d.com\">dot</a>\n";
}
$buffer .= $_;
}
}
close (TEMPLATE) or $error .= $!;





Yeah, that'll work too (actually, it won't fully work -- it'll do the job that user was wanting, but it will still bomb if there's any errors (I'll explain to you why in a moment)), but remember, if you're going to troll, do a better job next time. After all, you still have the problem of multiple closing body tags. Also, since you bitched about it being CGI and not using 'die', do you care to justify exactly what good the $error variable is going to do at this point? You know what that does? *Absolutely _nothing_*! If you want to create an error print/check routine, do it if you're going to rant on about it, but this example is worse and fails to do anything! Further, why would you want to have it continue running? Sure, other methods can be used, but I stressed the importance of checking the calls for a reason and your sarcastic response to good programming practices results in no error reporting at all. At least die would tell you something, whereas you will still errors via CGI and you see no indication by either means now. Then there's the issue of appending to the error variable on the FD close function...why? What do you *think* happens when it tries to open the file and can't? Well, you put in a "nifty" little check to not do a while loop and check, but you completely neglected the fact that it's still going to want to CLOSE that FD (which isn't open!) Then what? What does the user know? Nothing at all! No error, not knowing where it erred at (open or close, or both) and so on. You append this thing, which is confusing when you're printing the cause of the error, and so on. Even if you did print or log that, you've got how many errors clobbering one string? Where is it logged or printed? How does the user know where and what's wrong? They can't! Where's it telling them what's up? What's the point of checking then? There isn't a point then. I could go on, and on, and on.

The waste of extra syntax in your code, isn't any more appealing and I find it unneeded clutter -- that's not an advantage, btw. Also, why are you using the default $_, when it's not needed? Also, the quotes around the variable, is still bad practice. Also, also, also... (fill in the blank). Again, you might get pissed when someone helps (corrects) you, but you're going to have to get used to that on the Internet. I'll leave you to your fuming, but you should realize it's nothing personal and it's not a big deal... really. After all, if you're going to rip into me for pointing out obvious stuff like this, you should come up with better points. The comment about my routine (which I see I didn't even edit my post to check that I didn't remove all the backwacks (as I no longer needed them)) and how it would replace multiple closing body tags, was relevant, even if it's assuming. This was true of your code too, and in a different manner. Now you've completely polluted it and caused more problems in the code. Simply understand why I stressed the checking and don't take it personally.

So then, here you go:


open (TEMPLATE, $template_file) or error($!);
while (<TEMPLATE>) {
$i = (s,</body,<a href="http://d.com">dot</a>\n</body,i) if !$i;
$buffer .= $_;
}
close (TEMPLATE) or error($!);

sub error {
my $reason = shift;
print "Content-type: text/html\n\n";
print "Oh no, we erred while in CGI! $reason";
exit 1;
}


And there we have a nice CGI friendly, only one replacement regex per line, and it only does it once altogether. Now we're immune to the </body></body> issue on one line (remove the "g" from the regex, I know it's hard!), and we're immune to the multiple lined (Ooooh!)

"</body>
</body>"

issue as well. Of course, we'd have to add a little more to the routine to make it just replace it on the last closing body tag if it had more than one.... geez.

bitserve
04-15-2002, 02:14 AM
Originally posted by Tim_Greer
Try not to take offense. I was stating the information for the user's sake and for good programming practices.

Actually, it's encouraged that you do *not* put it (the variable) in the double quotes.

You were providing your uninformed opinion on my coding. Your opinion is only good programming practice in your little world, and not the rest. I doubt that you have even been on a development team that developed in PERL. And this entire time, we've both been assuming that this was PERL, based on the context.

Again, you shouldn't take offense.

I wasn't offended. Instead of providing opinion on your code, I provided code for how I would have done it. You provided your opinion on my code which would have lead dylan, or anyone else who might read this thread, astray with your misinformation. I thought I'd better make sure that this didn't happen. Now, I'm offended, but only because you chose to refer to me as skippy (which in your perverted world is probably something bad), a tool, stupid, irrational, and a troll.

When did he ever say this was ran via CGI? Perl and CGI are not the same thing. For all I knew, he was running a cron task executing a script (or manually via command line) to have it automatically parse and modify template files. I wouldn't assume either way, I simply stated to check the calls -- you have a problem with that?

Did I say that it was being used for CGI? No. You're one of them selective readers.

Moreover, look at this logically. Why didn't you do proper checking then (any!)? I did, you did none at all. What do you think happens if that file fails to open in CGI anyway? And your attempt to do error checking in your example below, was a joke (seriously, was it? Since it didn't actually do anything, that is!)

If the file did not open for reading, then obviously the script was not going to perform. Are you really asking me if I knew that? The user's original script did not have any error checking, and I also did not feel that it was necessary. It probably would have been more appropriate for you to ask if it was needed, instead of just throwing it in, since the original script didn't have it.

My error checking did not kill the program, yours did. Yours was the joke. What if you had managed to open the file enough that it needed closing? You would need to close it. You didn't. You died. Obviously you would need to do something with the $error variable to get any results. Are you stupid enough to assume that that was the entire script, and not just the small portion of it that does that function? Later, a sane person would have done something with the results. Printed them most likely.

I think it looks prettier that way. I'd have to question your motivation here -- obviously not to help the user to use better programming practices, therein helping them better understand and troubleshoot more future problems themselves. Apparently, you'd rather argue about stupid things.

There's a difference between programming well and nitpicking about things you personally don't like. I follow the standards, and I'm not going to worry about formatting perfectly on a web forum post. If you're going to worry about that, I suggest you go back and fix your own indention specs.


I don't know what planet your from, where it's prettier to indent three spaces for open statements and four spaces for a while statement. You're the only one I know who would even indent things between a file open and close statement. I could care less, where you indent. It is personal preference. If you're developing on a team project, it's project preference. But if you're going to indent, keep it consistent!

Mine was consistent!

If you understood, you'd see that's exactly what that code does (put a hyperlink before the closing body tag).

Can you not read PERL?

My code inserted a hyperlink on the line before the line containing the </body> tag.

Your code substituted the beginning of the </body> tag with a hyperlink and the beginning of your own custom </body> tag.

My code did what was intended! Yours reaches a similar result, but it does not do what was intended.

How many closing body tags will he have? If that's a problem, why don't you just remove one character? (That's the 'g', in case you don't know). Your code example did *exactly* the same thing as this above code does, you know! And, you're right, I think all the backwacking and slashes look too cluttered.

The original script, posted by dylan, did not contain even a substitution, let alone a global one! Why would you make it global? Somehow, I doubt that you knew what the "g" did, since you're the one that used it unnecessarily! You probably thought that it was just part of the substitution function, and had to be there.

No, read it again, skippy. I can suggest a good Perl programming book to help you understand. I mean, since we're being all mean and such to each other... why beat around the bush?

That is what you did! And you admit it in your next sentence. You may want to get a better programming in PERL book, and burn the one you're reading that made you think you know PERL.

HTML is case insensitive, don't worry about it.

So you're going to go modifying parts of the document, because you can get away with it? The script was not intended to change the case of the body tag!

Yes, that is true (oh, I see what you mean above). Had he specified he'd have multiple closing body tags, or that was possible, I'd have changed the syntax of the code. I'm not psychic, but I'm glad to see one of us is. You *can* remove the global on the regex, I know that one character is a big change.

I was only comparing your code to mine. Like you said, dylan did not specify it was possible for there to be multiple body tags. He also didn't specify that it was possible for the file not to be there. But if there were multiple instances of the body tag, your code failed more often than mine did, because it was not doing what it was supposed to be doing. It was supposed to be adding a hyperlink on the line before the line that contained a body tag. It was not supposed to be replacing the beginning of each body tag with a hyperlink followed by the beginning of a body tag!

B.. B... But... but, I thought you were psychic? :-)

That's not so 'super smart', never assume what the user wants.

I didn't assume that it was what the user wants. But I understood that the issue was not being addressed in the code, should it arise. You admit this yourself, by adding in your stupid "substitute once" code. Don't assume what the user wants, yet you'll go ahead and add in code to handle that situation? More than likely, dylan would only want to substitute the last occurence, and not the first occurence. Your code makes no sense.

Continued...

bitserve
04-15-2002, 02:18 AM
I use whatever appeals to me, that's not "more standard", it either is or isn't. Talk to Randall, Tom and Larry if you have problems with commas. I corrected a major oversight by you, and you are being irrational about this in response.

I'm not going to guess who you are referring to, but find one reference of a comma being used as a delimiter for the substitue function in the PERL man pages, and I'll show five times or more than that where a pipe is used. The pipe character is a much more unique character, especially when parsing an HTML document, than the comma is! You are welcome to code however you want, but you won't be checking your nasty code into any cvs respository of any PERL projects that I know of.

You corrected no oversight in my code, let alone a major one! You were the one being nitpicky, when you obviously don't even know what you're talking about!

Because, skippy, it doesn't need to be successful. If he doesn't have an ending body tag, he doesn't want to add the hyperlink. If he did, he could state so. If the ending body tag is present, it'll work.

So you care if the file is there and can be opened for reading, but not if it has a body tag. I was saying that if you're going to initiate some checks, you might as well check to see if your stupid substituion succeeded. Even in your last revision, you're coming closer by assigning the value to $i, why not check the value, as long as you have it? Probably because you want to be selective on the checks that should be recommended, saying that one check is better than another, or more important than another.

I didn't assume someone would act like such a tool, simply because I stressed the importance of good programming practices. Mind you, I'm not talking about "preferences", but things that are needed. Feel free to argue about trivial personal preferences all you like though -- and that it's going to educate people.

I didn't think that you would be such a moron while trying to prove, and failing miserably, that your PERL code makes any sense. I didn't ridicule anything that I would consider preferences, only your logic. If you want to stress good programming practices, you might want to learn some.

Yeah, again with me and those 'silly' call checks and you with none.

I already addressed this. And you yourself called them silly.

Yeah, that'll work too (actually, it won't fully work -- it'll do the job that user was wanting, but it will still bomb if there's any errors (I'll explain to you why in a moment)), but remember, if you're going to troll, do a better job next time. After all, you still have the problem of multiple closing body tags.

You never explained why my code would bomb. Probably because it wouldn't. There is no excuse for a troll, and so you should leave. I was not being one.

So now you state that we were supposed to be psychic and address the issue of the multiple body tags? When before, it was okay that it wasn't handled because dylan didn't say that we should expect them. Your code doesn't even handle them appropriately, as I said before. You're doing it on the first body tag, and not the last. I thought it was pretty clear that dylan wanted to add it at the end of the document, right before the body tag.

Also, since you bitched about it being CGI and not using 'die', do you care to justify exactly what good the $error variable is going to do at this point? You know what that does? *Absolutely _nothing_*! If you want to create an error print/check routine, do it if you're going to rant on about it, but this example is worse and fails to do anything! Further, why would you want to have it continue running? Sure, other methods can be used, but I stressed the importance of checking the calls for a reason and your sarcastic response to good programming practices results in no error reporting at all. At least die would tell you something, whereas you will still errors via CGI and you see no indication by either means now. Then there's the issue of appending to the error variable on the FD close function...why?

I made this clear (to a sane person) earlier. Obviously we weren't dealing with the entire script. If you're not smart enough to admit it, then you forgot to add in dylan's "$tmpcntr = 1;" line. Obviously, you would want to do something with the $error variable. You don't want to just kill the program, especially if it's a CGI program. That is the reason for not killing the program! If you didn't want to run the program to completion in the first place, you shouldn't have executed it! Dylan could have a lot of other functions in his script that he wanted to complete even if that one didn't! There was no reason to use a different variable for your error messages. If you can't read them both from the same variable, then you're broken.

What do you *think* happens when it tries to open the file and can't? Well, you put in a "nifty" little check to not do a while loop and check, but you completely neglected the fact that it's still going to want to CLOSE that FD (which isn't open!) Then what? What does the user know? Nothing at all! No error, not knowing where it erred at (open or close, or both) and so on. You append this thing, which is confusing when you're printing the cause of the error, and so on. Even if you did print or log that, you've got how many errors clobbering one string? Where is it logged or printed? How does the user know where and what's wrong? They can't! Where's it telling them what's up? What's the point of checking then? There isn't a point then. I could go on, and on, and on.

Your the one here that doesn't understand how PERL accesses files. It is possible for a file open to fail, and still require a close. Even if your open fails, you should attempt to close it. At the worst, you'll get an error that the file is not open. If you fail to close it, then you have an open file descriptor! You weren't even smart enough to print the actual error message in your first attempt. You just made up your own! How helpful was that going to be? My last revision printed an actual error message, that would be able to be interpreted by a sane person, whether they had much experience at all. PERL has very descriptive error messages that would have made it clear that the error occured while closing or opening the file. If this was going to confuse you, since you're not sane, you could obviously use a different variable for each error. You probably didn't even know how to get the actual error message with the $! variable until I posted it.

The waste of extra syntax in your code, isn't any more appealing and I find it unneeded clutter -- that's not an advantage, btw. Also, why are you using the default $_, when it's not needed? Also, the quotes around the variable, is still bad practice. Also, also, also... (fill in the blank). Again, you might get pissed when someone helps (corrects) you, but you're going to have to get used to that on the Internet. I'll leave you to your fuming, but you should realize it's nothing personal and it's not a big deal... really. After all, if you're going to rip into me for pointing out obvious stuff like this, you should come up with better points. The comment about my routine (which I see I didn't even edit my post to check that I didn't remove all the backwacks (as I no longer needed them)) and how it would replace multiple closing body tags, was relevant, even if it's assuming. This was true of your code too, and in a different manner. Now you've completely polluted it and caused more problems in the code. Simply understand why I stressed the checking and don't take it personally.

So now I broke the code by adding the stupid little file checks that you were so persistent about? I never even once commented on your coding style (what you earlier called personal preference), but you'll ridicule mine, after stating that it was wrong to do so? The style that I posted is the most common in all of the PERL programming projects of which I have worked on. Whether you think that it is a waste of space or not, it is very readable, and is lost in the compilation anyway. Please don't try to correct my code until (if ever) you become capable of doing it corrrectly.

You should get used to posting on a public message board (note to be confused with the Internet), where people are allowed to provide their opinions. I have said in may posts, if you have a different opinion, post it. You don't need to pick apart others opinions while doing it. In this case, we weren't even talking about opinions, we were talking more or less about logic. Yours was worse, if there was any there to begin with. If you can't handle being ridiculed, don't ridicule others. If you can't handle being called names, don't call others names.

I'll try not to take peronsally you calling me skippy, a tool, stupid, irrational, and a troll. I'm sure that you didn't mean it personally. Yeah, right!

Continued...

bitserve
04-15-2002, 02:19 AM
So then, here you go:


open (TEMPLATE, $template_file) or error($!);
while (<TEMPLATE>) {
$i = (s,</body,<a href="http://d.com">dot</a>\n</body,i) if !$i;
$buffer .= $_;
}
close (TEMPLATE) or error($!);

sub error {
my $reason = shift;
print "Content-type: text/html\n\n";
print "Oh no, we erred while in CGI! $reason";
exit 1;
}


And there we have a nice CGI friendly, only one replacement regex per line, and it only does it once altogether. Now we're immune to the </body></body> issue on one line (remove the "g" from the regex, I know it's hard!), and we're immune to the multiple lined (Ooooh!)

"</body>
</body>"

issue as well. Of course, we'd have to add a little more to the routine to make it just replace it on the last closing body tag if it had more than one.... geez.

I'm glad that you were at least able to learn how to properly report errors. At least you came out of this with something.

Tim Greer
04-15-2002, 04:44 AM
Originally posted by bitserve
You were providing your uninformed opinion on my coding.


Wrong, it's fact that checking your calls is good programming practice.


Your opinion is only good programming practice in your little world, and not the rest.


Yeah, yeah, yeah... I love how people get pissed off and try and discredit people by using phrases like "your 'little' world". Yes, my "little world" and the "rest of the Perl community". I guess since I don't agree with _your_ flawed opinion, then my knowledge must be limited.


I doubt that you have even been on a development team that developed in PERL.


Uh huh... and this rom a guy that is freaking out about someone suggesting the *obvious* about checking calls.


And this entire time, we've both been assuming that this was PERL, based on the context.


Assuming? Look at the code, skippy. This isn't Perl code?



I wasn't offended.


So you're not intentionally being dense about this just because it's obvious you are ignorant and became insulted by being corrected by someone that *does* know? That's not encouraging.


Instead of providing opinion on your code, I provided code for how I would have done it.


I corrected an oversight by you, you tried to rip into my example. Your preference does not take priority over fact.


You provided your opinion on my code which would have lead dylan, or anyone else who might read this thread, astray with your misinformation.


Now you're just trying to piss me off here. No one was left "astray" and there was *no* misinformation whatsoever. Someone that's as uneducated about this language as you are, denying that checking your calls for errors isn't important and are going to be an ass about it. Well, I'm not going to allow that. You obviously have no clue who I am or what I know. You obviously know nothing and I won't tolerate this.


I thought I'd better make sure that this didn't happen.


Complete Horseshit! Your opinion against facts every real programmer knows, holds no water.


Now, I'm offended,


Good!


but only because you chose to refer to me as skippy (which in your perverted world is probably something bad), a tool, stupid, irrational, and a troll.


Now it's not my "little world", it's my "perverted world"? Make up your mind. Are you that new to the Internet? I see you've never been to usenet or in a chat room. These immature comments are just making you look foolish. Why don't you try responding to the issues at hand -- your bad and broken code!


Did I say that it was being used for CGI? No. You're one of them selective readers.


Yeah, that tends to happen, when you're specific about mentioning CGI. Gee, go figure. Try and keep up, huh?


If the file did not open for reading, then obviously the script was not going to perform.


That function isn't going to, no. The script, I don't know -- that depends on what you have it do, now doesn't it!? What's your point? Your code was BROKEN!


Are you really asking me if I knew that?


No, I pretty much figure you didn't given this nonsense you're posting.


The user's original script did not have any error checking,


Right, and it should have, and I added it and I suggested you did as well. Go to the Perl newsgroup and post some examples without any error checking and they'll tell you exactly how foolish that is -- there's a reason for that.


and I also did not feel that it was necessary.


Given your blatant lack of knowledge, I would agree with that statement. I'm sure you "feel" that broken code is just fine to post for examples (oh wait, that's what you did!).

[continued]

Tim Greer
04-15-2002, 04:45 AM
It probably would have been more appropriate for you to ask if it was needed,


It's always needed, especially in examples. Of course, a programmer would know this, not you.


instead of just throwing it in, since the original script didn't have it.


And that was a flaw in the original script, any programmer would agree with that.


My error checking did not kill the program, yours did.


No, your code didn't kill it. It ruined it. It was flawed. This guy is asking about flawed logic in his code and you produce blatantly flawed logic in your response. This is not helpful, no matter how you "feel" about it. If that file was missing or couldn't be read, your error checking would have caused problems, it was broken. My original example would have made the script die with an error to report about WHY it stopped -- and that we don't want it to continue to run -- as problems can arise, and that is not a good thing. What are you missing here exactly?


Yours was the joke.


Was it now? Explain? Because I coded it properly and you're embarrassed that you made some poor and broken examples?


What if you had managed to open the file enough that it needed closing?


What in the heck are you trying to mumble about now? You're not making any sense! That was a ridiculous response! You have no idea what you're talking about! Your example either opened the file and was able to, or not (for a great number of reasons), so it's not going to just "sort of" open it. Good grief!


You would need to close it.


It was either able to be opened and read, or not. If it's not, it will error and it will cause problems if it tried to close it. I really wish you'd learn Perl before you start ranting this nonsense to me.


You didn't.


Uh, yes, I did close the FD.


You died.


I had it die if it couldn't open it, and after it was done (if it could), it would die if it couldn't close it. Do you understand anything about file access? Obviously not!


Obviously


Oh, well, OBVIOUSLY... yuck, yuck...


you would need to do something with the $error variable to get any results.


The error variable/string was dumb. It didn't make any sense, it didn't do anything. I've explained this. You don't get it still! Even if you had it PRINT the error variable, it would STILL error and would still be flawed upon trying to close an FD that was never opened! You didn't use ANY logic in that poor attempt to be sarcastic and correct anyone.


Are you stupid enough to assume that that was the entire script,


You seem to be "stupid enough" to assume that my example would be an entire script. I reiterate for the illiterate (pay attention bitserve); the small portion of your "imcomplete" example code, still showcased the flaw and that it would still error and that it was BROKEN! No matter what the rest of the unfinished masterpiece of this script would have been, that would not have changed that FACT. What the heck good is a borken example, bitserve?


and not just the small portion of it that does that function?


What are you even rambling about now? This is sad, how you are trying to save your "little" ego, you "pervert". Heh.


Later, a sane person would have done something with the results. Printed them most likely.


HA HA HA HA HAAAAAAA!!!!!!! This is actually really sad... really. Printing your "little" results, wouldn't have made any difference or bypassed your FLAWED code, you "pervert".



I don't know what planet your from,


Well, I've been out-wit-ed again...


where it's prettier to indent three spaces for open statements and four spaces for a while statement.


I hope your posts here aren't trying to be serious.


You're the only one I know who would even indent things between a file open and close statement.


What the...? Do you neeed coffee or an injection, or??? And, now that you mention it, where and when did I indent between the open and close functions? Even if I had, uh, soooo?


I could care less, where you indent.


Then don't ramble on about it, weirdo!


It is personal preference. If you're developing on a team project, it's project preference. But if you're going to indent, keep it consistent!

Mine was consistent!


Man, you are high. Who gives a rat's ass. Big deal, my friggin' post had a white space I didn't see when I edited a post, are you really going to lose sleep about it? Heh heh.


Can you not read PERL?


What are you even going on about now?

[continued again, admit it, you can't get enough of me! :-) ]

Tim Greer
04-15-2002, 04:46 AM
My code inserted a hyperlink on the line before the line containing the </body> tag.


YOUR code was BROKEN!


Your code substituted the beginning of the </body> tag with a hyperlink and the beginning of your own custom </body> tag.


And YOU could have removed the global substitution character if YOUR HTML knowledge is so poor, that you would have multiple ending body tags. I gave a quick and effective example of removing it and how it will not replace over one occurance if someone was that bad at HTML. YOUR code also would add it for each line occurance. My example in response to that, also avoided that too, if that was the concern. It's pretty easy to adapt and understand this stuff, provided you have a clue about what you're doing -- if that's what needs to be done at the user's request.


My code did what was intended! Yours reaches a similar result, but it does not do what was intended.


If you want to complain that it didn't do what was intended compared to your example, then you need to get your head out of your ass, fast. Seriously. Both code examples were based on adding the hyperlink above the closing body tag, if it found it. Both did this. Neither of us made adjustments to accomodate poor HTML coding, because we didn't assume that there would be more than one closing body tag. Therefore, if that's your basis for this claim, your code did not function any better, even though the rest of your example was bad, and I did error checking. So, you would have to say that neither of our examples did what they were supposed to, and that's simply not the case. Had I been coding a script like this to do this task, I'd have put in checking to ensure that it only added that hyperlink in only one, correct place. Neither of us did that. THAT is the part where we say "I don't feel it was needed for an example", not be a moron and say "I don't feel there was a need to code working code or to do any error checking and not practice sane and good programming". Do you get it, yet? Your claims are only more attempts to troll, and you obviously don't have a clue about what you are saying!



The original script, posted by dylan, did not contain even a substitution,


So?


let alone a global one!


So? Yours did.


Why would you make it global?


Why not? That actually was just me typing out a quick example. I'd not have made it global, but it's not a big deal. I stated the global can be removed. I wasn't going to go back, edit my post and act like I never posted it like that. That again, is not an incorrect or broken or imcomplete or poor example, yours however, was. That was the problem I had with it. I also was not going to guess about this template or anything about it. FOR ALL I KNEW, and for ALL YOU KNEW, this user could have had a template with 5 HTML pages in it, that generated something, in which case they wanted it to replace every occurance. If he stated otherwise (I don't think he did), then I'd have conciously not made it global. I'm not trying to make excuses like you are, or to defend myself. I'm simply explaining the obvious to you, once more. So, why not?


Somehow, I doubt that you knew what the "g" did, since you're the one that used it unnecessarily!


Refer to the above. Try and discredit me all you like. I made no mistake, I didn't screw up the code example. That's on your head, not mine. I imagine, unlike myself, you probably would have a difficult time explaining what the heck you were thinking. (Should I have bolded YOU?).



You probably thought that it was just part of the substitution function, and had to be there.


Yeah, whatever you say that's defaming about me, must be true and I must have had no idea that meant global (even though I said so myself previously). After all, I pissed you off, so that's good enough for me to buy. I guess you really put me in my place. Heh. Now, again, care to explain what you were thinking?


That is what you did!


What is what I did? made it global? Yeah, and??? Do you know this was definitely not what the user wanted? Oh, right, you're psychic. You might be right that he didn't want that... again, so? I never said he did, I said I didn't know.


And you admit it in your next sentence. You may want to get a better programming in PERL book, and burn the one you're reading that made you think you know PERL.


Oh, give me a break... If you don't get this by now, you should just pull your power cord now.



So you're going to go modifying parts of the document, because you can get away with it? The script was not intended to change the case of the body tag!


I never said it was. I said it doesn't matter. I never said that provided you REMOVE the FLAWS and lack of LOGIC in YOUR EXAMPLE, that it wouldn't work. Did I? If this user wanted this hyperlink above each and every instance of multiple occurances on one template, then my example would have been perfect. I'm not going to act like I claim that's what the user wanted though -- that would be something you'd do. However, this nonsense you are posting out of desperation to try and defend your horrid example, is just going overboard and is the wrong way to do it.


I was only comparing your code to mine.


You call that code? Maybe a little extra in my code, admitting I didn't try and dwell on the exact example he needed, but just wanted to give him a working example by his description (after all, WHO in their right mind would A: think that someone would have more closing body tags!? and B: Bitch so much about it and act like a jerk... all because HE couldn't post workable, standard code that isn't FLAWED!?) Once again, my code was a complete example and worked. It was easily adjusted, no problem at all. It at least wasn't flawed like your example. That is the problem here, nothing more -- no matter what you try and make of it, will not change that fact.

[you know what's going on... => ]

Tim Greer
04-15-2002, 04:46 AM
Like you said, dylan did not specify it was possible for there to be multiple body tags.


Exactly, and my code would work perfect then.


He also didn't specify that it was possible for the file not to be there.


You're comparing two completely different things. This is horse crap! If you knew what you were talking about, this wouldn't be happening! You can assume there would be multiple closing tags to try and discredit my example -- your's would have not resulted in what he wanted then either at that point. HOWEVER (PAY ATTENTION HERE!), he WAS (NO QUESTION ABOUT IT!) needing to open that file. That makes it NEEDED to do checking on the calls. You can not compare the two! It's like saying that "Well, you build a car for this person and you modeled it to have a break pedel under each steering wheel -- what were you thinking? If there was TWO steering wheels in that car (sure, who'd ever do that, but STILL), then you'd have another break pedal.. idiot, pervert, little man, ha ha ha!". Meanwhile you say "Well, there's my point. Just like you didn't know there could be two steering wheels, I didn't know he would even need BREAKS!" That is a perfect analogy. You NEED certain things, based on what you're given. I.e., open a file - you must do checking. That has NOTHING to do with your ****** examples of how you want to try and make it seem as if you didn't f**k up and I did. This is wrong, and unethical, and people that want to act like that, make me sick!


But if there were multiple instances of the body tag, your code failed more often than mine did,


But who needs breaks in a car, right?


because it was not doing what it was supposed to be doing.


It did *exactly* what it was supposed to be doing. If he specified otherwise, that would be different. This lame ranting of yours, is not helping you make sense. Your code example was poor and it was BROKEN. Deal with it. Stop bitching about trivial "what if"'s -- if that "what" was stated, it's easily changed, as I later even made a working example to demonstrate for you. You still don't get it, do you? Do you!!?


It was supposed to be adding a hyperlink on the line before the line that contained a body tag.


Yes... yes.. yes.. we're all completely friggin' aware of this. Both our "addition" (or "replacement") functions did just that.


It was not supposed to be replacing the beginning of each body tag with a hyperlink followed by the beginning of a body tag!


And it was not supposed to add a hyperlink to every single other line in the same template either, but if that template's HTML was that poor, both examples would have. So, big deal if the substitution would have replaced additional one's IF they had MULTIPLE CLOSING BODY TAGS ON THE SAME FRIGGIN' LINE! G-I-V-E M-E A B-R-E-A-K! Is this the best you can do? GEEEEEEEEZZZZ!


I didn't assume that it was what the user wants.


No, and nor did I. Neither one of us asked that, did we? Obviously if he had more than one ending body tag, neither example would have gave them what he wanted. So, stop this crap now! Enough already. YOU failed to even make sure he could OPEN the friggin' FILE -- and I think that's more important than assuming his template will be filled with completely invalid and messed up HTML code, don't YOU!? But you're right, let's dwell on this stupidity for a while longer, it's all you've for to work with apparently. Well, I'm impressed with your knowledge and claims NOW!


But I understood that the issue was not being addressed in the code, should it arise.


You didn't bother to address any of the issues.


You admit this yourself, by adding in your stupid "substitute once" code.


I did admit that it would replace mutilple lines, if there was (which there should never be!) multiple closing body tags. I then gave an example of how to avoid that. I'm not here to try and cover my mistakes. I made none. People do make mistakes and I do too. I didn't in this instance though, and a simple oversight of not asking him "Do you want to do this once, or are you going to have really bad, invalid HTML code and we have to adapt to that in our examples?". So, you act like me saying "Fine, if you have a problem with my example, simply do this, it's simple", is somehow admitting I screwed up and you're correct about claiming I don't know Perl? Boy, you have absolutely NO idea what you're saying. You comparing that to your reckless disregard to stick to good programming practices and not even checking to make sure a file was opened, is indicative that you, skippy, are the only one here that seems to lack the knowledge about this subject. I really don't think my choice of using a regex to substitute anything, supports your disorted claims and outlook. You f**ked up, that's just how it is.


Don't assume what the user wants,


I don't assume enough to write out entire code functions to write a complete script to check for everything I'd want or how I'd handle it, no. Nor do you.


yet you'll go ahead and add in code to handle that situation?


Yes, indeed, since you brought it up. I think you could have (and should have) done the same -- since it was such a big deal to you. It only took me a second to do, and show you what I meant, but it figures you wouldn't understand. I still never made it only replace the very last closing body tag though, if that makes you sleep any better at night.


More than likely, dylan would only want to substitute the last occurence,


If Dylan was such a poor HTML coder to have a lot of examples, sure. I don't assume he's that ignorant about it though. He didn't know Perl, he didn't ask for help with HTML. By the way, uh, did your example only add this hyperlink to the last accorance? I thought that was insanely ridiculous to assume. Can it happen, is it possible? Yes, indeed. You know what though, so what!? Who's going to do that!? Huh? If he ends the body tag, the body of the HTML, I believe, is technically ended, so the additional hyperlink above the closing body tag would be more appropriate to only replace it above the FIRST instance anyway -- which must make you want to cry after making such a fuss about all this. Heh.


and not the first occurence. Your code makes no sense.

Continued...

The code made perfect sense. I never said it would replace the last occurance out of a large list of closing body tags. I didn't try. Do you want to try and taunt me to get me to show you how it would be done? What is with you? I had simply showed you how my code could easily be modified to deal with two issues you brought up. One, to not be global (fine, I don't try and guess he wants it to be global, I don't mind changing that). Problem solved, it only added the hyperlink once -- no sweat, no effort (not a big deal to me, at least). Next, I showed you how it would not replace any other occurance either, therein resolving the other issue you brought up about it replacing it on other lines, if there was any. Okay, fine, that also handled that -- problem resolved. No big deal.... Next, the issue about CGI and die not being a good thing to use in CGI. Fine, I made a routine to deal with that, and print the error and exit from continuing running the rest of the script. Problem solved. Now, that's three issues you mentioned. The final, was a comment about if we really wanted to be moron's and assume there's tons of them and to just add the hyperlink above the very last match (which would be invalid HTML, not just because of the many closing body tags, but because it should be above the FIRST closing body tag anyway!). So, I didn't bother. It would make the HTML template more screwed up than it already was, if it did have more than one closing body tag. So, what exactly are you saying? It's better to go all out and cover that lame questional possibility too, even if it'll actually make the template more broken? So, once again, the example works perfectly, even for multiple closing body tags! If we actually ASKED the user how and what he was doing, we could modify it quickly to adapt. So, for now, there's nothing wrong. Still not only is your examples broken, but don't deal with even that much stuff, plus your error checking is flawed and will not work and actually make things worse. Indeed, in regards to your above ending of this post I'm responding to now, let's "continue"...

Dylan
04-15-2002, 05:06 AM
:bawling:

Now I am totally lost - and it seems at though this thread itself is stuck in a loop

:bawling:

kunal
04-15-2002, 05:20 AM
locked as requested.