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

how to write data passed as an argument to a function ???

P: 65
Hi All,

am trying to call the function, which needs to write the arguements passed to this function in a file.
basically creating a log file : thought instead of printing message, i will write the data in seperate log file so that it will useful for later anlaysis.

script goes like this:
Expand|Select|Wrap|Line Numbers
  1. my $string;
  2. my $str="";
  3. my $datestamp = sprintf "%02d%02d%02d", 
  4.     ((localtime)[5]%100, (localtime)[4]+1, (localtime)[3]);
  5. #print $datestamp;
  6. my $time =localtime();
  7. my $subtime = substr($time,11,18);
  8. my $time_sub = substr($subtime,0,8);
  9. #my $datetime=$datestamp."_".$time_sub;
  10. my $logfile ="c:\\Performance_svap\\logfile_$datestamp.txt";
  11. open (Wlog,"> $logfile") or die "Can't open $logfile : $!";
  12. &writelogfile("hi All");
  13. &wirtelogfile("Testing Log file creation");
  14. sub writelogfile($str){
  15.     open(Wlog,">> $logfile") or die "Can't open $logfile : $!";
  16.     #write $str;
  17.     close Wlog;
  18. }
  19. close Wlog;
  20.  
when i try to run this script i get this error message :
Expand|Select|Wrap|Line Numbers
  1. C:\Performance_svap\misc>chkempty.pl
  2. Undefined subroutine &main::wirtelogfile called at :\Performance_svap\misc\chke
  3. mpty.pl line 197.
  4.  
but when i comment one of the calling function then, logfile gets created but there is not content in it.
All i need is while call this function
Expand|Select|Wrap|Line Numbers
  1. &writelogfile("hi All");
  2.  
in the output logfile i should get the string which is passed to this function.
somethig like this :
Expand|Select|Wrap|Line Numbers
  1. in logfile: "hi All" should be written
  2.  
can anyone tell me how to acheive this ???

Regards,
Vijayarl
Oct 1 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 126
If you refuse to read the warning messages or the code, you will never be able to fix your "own" work.

Firstly:
Expand|Select|Wrap|Line Numbers
  1. &wirtelogfile("Testing Log file creation");
  2.  
You see how this is spelt wrong? It should be &writelogfile, as the warning tells you.

Secondly:
Expand|Select|Wrap|Line Numbers
  1. sub writelogfile($str){
  2.     open(Wlog,">> $logfile") or die "Can't open $logfile : $!";
  3.     #write $str;
  4.     close Wlog;
  5. }
  6.  
This function opens the logfile and closes it again. Nothing else is done with this file as the middle line is commented out. What do you expect a commented out line to do, apart from nothing?

As for the $str, this isn't how you get parameters in perl, you have to get them from @_. This function does what you want (assuming it's not called from another object):
Expand|Select|Wrap|Line Numbers
  1. sub writelogfile { # assumes $logfile is already defined
  2.      my $str = shift; # get the first parameter from @_
  3.      open(WLOG,">> $logfile") or die "Can't open $logfile : $!";
  4.      print WLOG "$str\n"; # using print here which doesn't print a newline by default/
  5.      close WLOG;
  6. }
  7.  
Oct 1 '08 #2

P: 65
Thanks Stwange !!!!!

thanks for pointout my mistakes in script...hereafter i will try debug errors myself....

Thanks once again for your support....script worked fine as expected...

Regards,
Vijayarl
Oct 1 '08 #3

Post your reply

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