467,202 Members | 1,075 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,202 developers. It's quick & easy.

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

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;
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.
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");
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
can anyone tell me how to acheive this ???

Oct 1 '08 #1
  • viewed: 1332
2 Replies
Expert 100+
If you refuse to read the warning messages or the code, you will never be able to fix your "own" work.

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

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. }
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. }
Oct 1 '08 #2
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...

Oct 1 '08 #3

Post your reply

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

Similar topics

4 posts views Thread by Neil Zanella | last post: by
3 posts views Thread by franco ziade | last post: by
5 posts views Thread by Confused User | last post: by
11 posts views Thread by Brad Pears | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.