By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,786 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,786 IT Pros & Developers. It's quick & easy.

Internet logging using c# & perl cgi script

P: 84
Hi Guys,
Sorry for duplicate posting as this Question refers to both c# and perl cgi script on the net, so please help.

Here is the original post

which contains the perl cgi script and c# code i am using.

Please help - any help will be highly appreciated.
Jun 11 '09 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 100+
P: 589
There's nothing in the Perl script that would cause it to duplicate the log entries so I'm not sure what you're wanting from us, unless you're wanting us to help you cleanup the very poorly written script.
Jun 11 '09 #2

P: 84
Any help to clean and make it better would be highly appreciated. Also can we return some text to the browser so that it shows some status from the script?

Sorry i am very new at perl. i am more experienced with .Net but the server doesnt support it:(
Jun 11 '09 #3

Expert Mod 100+
P: 589
The only thing your Perl script does is write a log entry of the passed in cgi params each time it's called. I don't see why you're not doing that in the c# program.

I cleaned up the script and left out the parts that didn't really do anything.
Expand|Select|Wrap|Line Numbers
  1. #!c:/Perl/bin/Perl.exe 
  3. use strict;
  4. use warnings;
  5. use POSIX 'strftime';
  6. use CGI ':standard';
  7. use Fcntl ':flock';
  9. my $counterfile = 'D:/output.txt';
  10. open my $FILE, '>>', $counterfile or die "Can't open '$counterfile' file $!";
  11. flock($FILE, LOCK_EX); # blocking write lock
  13. my @fields;
  14. $fields[0]  = param('username');
  15. $fields[1]  = param('contract');
  16. $fields[2]  = param('status');
  17. $fields[3]  = param('file'};
  18. $fields[4]  = $ENV{'REMOTE_ADDR'};
  19. $fields[5]  = strftime("%m/%d/%y %H:%M:%S", gmtime(time));
  20. $fields[6]  = $ENV{'HTTP_USER_AGENT'}
  21. $fields[7]  = param('iver');
  22. $fields[8]  = param('sys');
  23. $fields[9]  = param('sysloc'};
  24. $fields[10] = param('lang');
  25. $fields[11] = param('OpSys');
  27. print $FILE join(',', @fields), "\n";
  29. flock(FILE, LOCK_UN);
  30. close $FILE;
I don't know c# but I assume that it captures the STDOUT stream from the Perl script. So, if you need to pass back anything to the c# program, just print to the STDOUT handle, which is the default when using the Perl print function.

The die statement in the open call sends its output to the STDERR handle.
Jun 12 '09 #4

Expert Mod 2.5K+
P: 3,503
My first bit of advice is to include the following into every Perl script:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
You will find that there are a number of people here who won't touch your script unless you have these in there. By using them, you will be able to correct a lot of the simple coding mistakes that beginners make and allow us to better tackle the real issue(s) at hand.

As for your Perl script (this is before looking at Ron's version, I don't see anything in your script that says "I am CGI" other than your reference of the ENV hash. To output to the screen you will really need to use the CGI module as Ron has done.

There are plenty of tutorials on CGI in Perl on the internet, you just have to Google them. I would also highly recommend you get a copy of "Learning Perl'" from O'Reilly. It is a wonderful introduction into the world of Perl and I always keep a copy within reach.


Jun 16 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.