Operating System - Linux
1752753 Members
4346 Online
108789 Solutions
New Discussion юеВ

Re: Function calling another function

 
Mike Keys
Regular Advisor

Function calling another function

At the sake of simplifying code, I moved a transaction logging function it's own routine and am calling it through the actual transaction routines. However, the file is being created but nothing is being written. Here is a sample of a transaction function and the logging funciton.


#######################################################################
# Remove a user #
#######################################################################
sub userdel {

my $cmd = sprintf("/usr/sbin/userdel %s",$user);
my $status = system($cmd);
writetrans($cmd);
return ($status);
}

#######################################################################
# Write transactions to file #
#######################################################################
sub writetrans {

my $tmpfile = "/home/ops/passwd.fil";
my $trans = sprintf("%s \n",$cmd);
open (TMPFILE,">>$tmpfile"); # Write transactions to logfile
print TMPFILE "$trans";
close (TMPFILE) || die "\nCould not write to file: $tmpfile:$!";

}


I am passing the $cmd to the transaction logging function (writetrans) and have tried everything I could think of to get this to work. What am I missing?
10 REPLIES 10
David Child_1
Honored Contributor

Re: Function calling another function

It looks like your setting $cmd in one function and it will be locale to that function only. Two ways around it are;

1) create global $cmd
2) change writetrans to;

sub writetrans {

my $cmd = $_;

my $tmpfile = "/home/ops/passwd.fil";
my $trans = sprintf("%s \n",$cmd);
open (TMPFILE,">>$tmpfile"); # Write transactions to logfile
print TMPFILE "$trans";
close (TMPFILE) || die "\nCould not write to file: $tmpfile:$!";

}
Gopi Sekar
Honored Contributor

Re: Function calling another function


Which shell are you trying this, I believe it is not BASH, anyway Let us do a basic check:

move the writetrans function before userdel function and see whether it solves the problem

Regards,
Gopi
Never Never Never Giveup
Pat Lieberg
Valued Contributor

Re: Function calling another function

I had trouble with this before and it had to do with buffers not being flushed, so my log file was empty while the process was running. You can force it to not cache the writes to your logfile by doing:

$| = 1;

That's a pipe symbol by the way.

Hope this helps.

-Pat Lieberg
Unix Systems Admin.
3M IT Operations
Mike Keys
Regular Advisor

Re: Function calling another function

Pat,

Where do I insert your piece into the code?
Mike Keys
Regular Advisor

Re: Function calling another function

This is being done with Perl.
Mike Keys
Regular Advisor

Re: Function calling another function

So far suggestions mentioned do not work.
Gopi Sekar
Honored Contributor

Re: Function calling another function


oops i am sorry, i better get some specs :)

ok the problem looks like $cmd variable is local to the function userdel and is not reachable in writetrans function.

modify writetrans function to get $cmd as command line argument.

eg:

sub writetrans {

my ($cmd) = @_;


}

Regards,
Gopi
Never Never Never Giveup
Mike Keys
Regular Advisor

Re: Function calling another function

Hey, now i'm getting zeros written to the file. Hooray!
Mike Keys
Regular Advisor

Re: Function calling another function

what appears to be happening is that if I place the commands that are in writetrans(); into each transaction function, i get output just fine. yes, because the variable is local to that function.

However, by placeing the commands within their own function, I am getting 0's written to the file. Could it be that the 'sprint' command is doing something odd when passing the variable by reference?