Function calling another function (108 Views)
Reply
Regular Advisor
Mike Keys
Posts: 170
Registered: ‎10-17-2002
Message 1 of 11 (108 Views)

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?
Honored Contributor
David Child_1
Posts: 394
Registered: ‎01-10-2001
Message 2 of 11 (108 Views)

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:$!";

}
Honored Contributor
Gopi Sekar
Posts: 877
Registered: ‎04-25-2005
Message 3 of 11 (108 Views)

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
Valued Contributor
Pat Lieberg
Posts: 107
Registered: ‎06-23-2005
Message 4 of 11 (108 Views)

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
Regular Advisor
Mike Keys
Posts: 170
Registered: ‎10-17-2002
Message 5 of 11 (108 Views)

Re: Function calling another function

Pat,

Where do I insert your piece into the code?
Regular Advisor
Mike Keys
Posts: 170
Registered: ‎10-17-2002
Message 6 of 11 (108 Views)

Re: Function calling another function

This is being done with Perl.
Regular Advisor
Mike Keys
Posts: 170
Registered: ‎10-17-2002
Message 7 of 11 (108 Views)

Re: Function calling another function

So far suggestions mentioned do not work.
Honored Contributor
Gopi Sekar
Posts: 877
Registered: ‎04-25-2005
Message 8 of 11 (108 Views)

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
Regular Advisor
Mike Keys
Posts: 170
Registered: ‎10-17-2002
Message 9 of 11 (108 Views)

Re: Function calling another function

Hey, now i'm getting zeros written to the file. Hooray!
Regular Advisor
Mike Keys
Posts: 170
Registered: ‎10-17-2002
Message 10 of 11 (108 Views)

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?
Honored Contributor
Gopi Sekar
Posts: 877
Registered: ‎04-25-2005
Message 11 of 11 (108 Views)

Re: Function calling another function

I tested the subroutines in my system and it is working as expected. Below are my changes to the function (note I modified program to make sure I dont delete any user in my system :)

My system is Linux (Fedora Core 4), if this is not working on your system then it is more likely that the perl compiler you have is not working as expected

Hope this helps,
Gopi

sub userdel {
$user='/tmp';
my $cmd = sprintf("/bin/ls %s",$user);
my $status = system($cmd);
writetrans($cmd);
return ($status);
}

sub writetrans {
my ($cmd) = @_;

my $tmpfile = "/tmp/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:$!";
}


Never Never Never Giveup
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.