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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijayarl
    New Member
    • Sep 2008
    • 65

    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:
    Code:
    my $string;
    my $str="";
    my $datestamp = sprintf "%02d%02d%02d", 
        ((localtime)[5]%100, (localtime)[4]+1, (localtime)[3]);
    #print $datestamp;
    my $time =localtime();
    my $subtime = substr($time,11,18);
    my $time_sub = substr($subtime,0,8);
    #my $datetime=$datestamp."_".$time_sub;
    my $logfile ="c:\\Performance_svap\\logfile_$datestamp.txt";
    open (Wlog,"> $logfile") or die "Can't open $logfile : $!";
    &writelogfile("hi All");
    &wirtelogfile("Testing Log file creation");
    sub writelogfile($str){
    	open(Wlog,">> $logfile") or die "Can't open $logfile : $!";
    	#write $str;
    	close Wlog;
    }
    close Wlog;
    when i try to run this script i get this error message :
    Code:
    C:\Performance_svap\misc>chkempty.pl
    Undefined subroutine &main::wirtelogfile called at :\Performance_svap\misc\chke
    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
    Code:
    &writelogfile("hi All");
    in the output logfile i should get the string which is passed to this function.
    somethig like this :
    Code:
    in logfile: "hi All" should be written
    can anyone tell me how to acheive this ???

    Regards,
    Vijayarl
  • Stwange
    Recognized Expert New Member
    • Aug 2007
    • 126

    #2
    If you refuse to read the warning messages or the code, you will never be able to fix your "own" work.

    Firstly:
    Code:
    &wirtelogfile("Testing Log file creation");
    You see how this is spelt wrong? It should be &writelogfil e, as the warning tells you.

    Secondly:
    Code:
    sub writelogfile($str){
        open(Wlog,">> $logfile") or die "Can't open $logfile : $!";
        #write $str;
        close Wlog;
    }
    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):
    Code:
    sub writelogfile { # assumes $logfile is already defined
         my $str = shift; # get the first parameter from @_
         open(WLOG,">> $logfile") or die "Can't open $logfile : $!";
         print WLOG "$str\n"; # using print here which doesn't print a newline by default/
         close WLOG;
    }

    Comment

    • vijayarl
      New Member
      • Sep 2008
      • 65

      #3
      Thanks Stwange !!!!!

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

      Thanks once again for your support....scri pt worked fine as expected...

      Regards,
      Vijayarl

      Comment

      Working...