Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call subroutine within subroutine

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
0
0
I have a subroutine which will display the current time for a log file.

CURRENT_TIME STUFF,ETC,ETC
CURRENT_TIME MORE STUFF,ETC


During the course of the script, I call a number of subroutines for other information to be printed out to the log file. I would like to call the CURRENT_TIME subroutine from within another subroutine. But each time I try, it just ignores the entry and continues on with its normal processing. I have checked my PERL book, but I have come up with nothing on this, can this be done?

Thanks in advance.
 
Does not compute, how did you declare your subroutine, and how are you calling it

Paul
 
open(LOG, ">log_file");

sub CT {
$time = `date "+%T"`;
chomp $time;
print LOG "$time\t";
}

sub SOME_OTHER_ROUTINE {
CT; # This will not print the time in the log
print LOG "BLAH, BLAH, BLAH";
}

# Start Script here.
CT; # This does print the time.
print LOG "BLAH, BLAH";


Why can I not get the CT subroutine to work within another subroutine?
 
Well, it would help if you actually made a call to
SOME_OTHER_ROUTINE.

If you do that, you will see that the time will be printed twice.

Greetings,
Swamphen
 
Okay then, let me show some actual syntax from the script which I am running. Please do not patronize me with this "Wll, it would help if you actually made a call to SOME_OTHER_ROUTINE". I do not appreciate that in the least. I did exactly what you said and the time would not be printed out to the log when called from within another subroutine. So please look at the following and see if this helps:

#!/usr/bin/perl
# The script has been modified to make use of getopts. The script was modified
# so that with getopts you would be able to call certain functions or run the
# whole script. This will be useful for fixes, etc.

# SET RETURN CODES
#
# 0 - Script completed successfully.
# 1 - User requested usage info or was given usage info.
# 2 - User requested script version information.
#
# END RETURN CODES

# SET GETOPTS
#
# c == Convert Image
# h == Create HTML
# i == Input Directory
# l == List File
# o == Output Directory
# t == Image Type
# u == Give Usage Info
# v == Give Script Version Info
# ? == Give Usage Info
#
# END GETOPTS

# SET VARIABLES
#
$bdir = "/content";
$ldir = "/content/log";
#
$ea = "\.ann"; # Extension: ann
$eh = "\.htm"; # Extension: htm
$version = "2.0";
#
# END VARIABLES

# SET MINI-FUNCTIONS
#
sub DATE {
$date = `date "+%m/%d/%Y - %T"`;
chomp $date;
print LOG "$date\t";
}

sub IMGCONV {
print LOG "Create file: $oimg.....";
$image->Resize(width=>$nw, height=>$nh);
$image->Quantize(colors=>16, colorspace=>'RGB');
$image->Write(filename=>"$oimg", compression=>'LZW');
if (!($OPT{"h"})) {
undef $image;
}
print LOG "Done.\n"
}

sub IMGINFO {
print LOG "Processing file: $img.....";
$image = new Image::Magick;
$image->Read("$img");
($h, $w) = $image->Get('height', 'width');
if (($h || $w) == 0) {
print ERROR "Filename: $img\n";
print ERROR "Height: $h\n";
print ERROR "Width: $w\n\n";
undef $image;
undef $img;
undef $oimg;
} else {
$r = 640/$w;
$nw = 640; # New width
$nh = $h * $r; # New height
}
print LOG "Done.\n";
}

sub MKHTM {
MTIME; # This will not print to the log.
print "Create HTML: $oimg.....";
open(ANN, "$iann");
open(HTML, ">$ohtm");
$header = <ANN>;
print HTML $hd;
while(<ANN>) {
($f1, $f2, $alt, $x, $y, $ex, $ey, $f8, $f9, $f10, $f11, $f12, $f13, $f14, $f15, $id) = split/\x1/;
($v1, $v2, $var, $v4) = split/,/, $id;
printf HTML &quot;<AREA SHAPE=RECT COORDS=\&quot;%d,%d,%d,%d\&quot; HREF=\&quot;Javascript:parent.TabManager.hotspot('%s');\&quot; ALT=\&quot;%s\&quot;>\n&quot;, $x*$r, $y*$r, ($x+$ex)*$r, ($y+$ey)*$r, $var, $alt;
}
print HTML $fd;
close(HTML);
close(ANN);
if (exists($OPT{&quot;c&quot;})) {
undef $image;
}
}

sub MTIME {
$time = `date &quot;+%T&quot;`;
chomp $time;
print LOG &quot;$time\t&quot;;
}

sub USAGE {
print &quot;\n\nUsage:\n&quot;;
print &quot;\t -b: <EXTENSION TYPE TO LOOK FOR>
print &quot;\t -c: Create Image file.\n&quot;;
print &quot;\t -h: Create HTML file.\n&quot;;
print &quot;\t -i: <INPUT DIRECTORY>\n&quot;;
print &quot;\t -l: <LIST NAME>\n&quot;;
print &quot;\t -o: <OUTPUT DIRECTORY>\n&quot;;
print &quot;\t -t: <DEFINE IMAGE TYPE>\n&quot;;
print &quot;\t -u: Gives usage information.\n&quot;;
print &quot;\t -v: Gives script version information.\n&quot;;
print &quot;\t -?: Gives usage information.\n\n\n&quot;;
exit 1;
}

sub VER {
print &quot;\n\nScript: graph2.pl.....Current Version: $version\n\n\n&quot;;
exit 2;
}
#
# END MINI-FUNCTIONS

# Start log files.
open(ERROR, &quot;>$ldir/graph2.err&quot;);
open(LOG, &quot;>$ldir/graph2.log&quot;);
DATE; # This works.
print LOG &quot;Script: graph2.pl.....Started.\n\n\n&quot;;

# Declare libraries.
MTIME; # This works.
print LOG &quot;Now loading libraries.....&quot;;
use DirHandle;
use Getopt::Std;
use Image::Magick;
print LOG &quot;Done.\n&quot;;

# Get the options.
MTIME;
print LOG &quot;Process command line options.....&quot;;
my %OPT;
getopts('b:chi:l:eek::t:uv?', \%OPT);

if (exists($OPT{&quot;b&quot;})) {
$breed = $OPT{&quot;b&quot;};
$breed =~ s:/+$::;
}

if (exists($OPT{&quot;c&quot;})) {
$COF = &quot;Enabled!!!&quot;;
} else {
$COF = &quot;Disabled!!!&quot;;
}

if (exists($OPT{&quot;h&quot;})) {
$HOF = &quot;Enabled!!!&quot;;
} else {
$HOF = &quot;Disabled!!!&quot;;
}

if (exists($OPT{&quot;i&quot;})) {
$idir = $OPT{&quot;i&quot;}; # Input Directory
$idir =~ s:/+$::;
opendir(DIR, &quot;$idir&quot;);
@fn = grep /.*\.$breed.*/, map &quot;$idir/$_&quot;, readdir DIR;
closedir(DIR);
} else {
$idir = &quot;N/A&quot;;
}

if (exists($OPT{&quot;l&quot;})) {
$list = $OPT{&quot;l&quot;};
$list =~ s:/+$::;
open(LIST, &quot;$list&quot;);
@fn = <LIST>;
close(LIST);
} else {
$list = &quot;N/A&quot;;
}

if (exists($OPT{&quot;o&quot;})) {
$odir = $OPT{&quot;o&quot;}; # Output Directory
$odir =~ s:/+$::;
$odir = &quot;$odir/&quot;; # Setup &quot;/&quot; at end of variable for later use.
} else {
$odir = &quot;$bdir/temp&quot;;
}

if (exists($OPT{&quot;t&quot;})) {
$it = $OPT{&quot;t&quot;}; # Image Type
$it =~ s:/+$::;
$et = &quot;\.$it&quot;; # Extension: type
} else {
$it = &quot;N/A&quot;;
}

if (exists($OPT{&quot;u&quot;}) || exists($OPT{&quot;?&quot;})) {
USAGE;
}

if (exists($OPT{&quot;v&quot;})) {
VER;
}

# Verify we have enough options for processing.
if ((exists($OPT{&quot;i&quot;}) && exists($OPT{&quot;l&quot;}))) {
print LOG &quot;Failed!!!\n&quot;;
MTIME;
print LOG &quot;You cannot specify OPTIONS: i and l at the same time.\n&quot;;
exit 3;
} elsif ((!($OPT{&quot;i&quot;}) && (!($OPT{&quot;l&quot;})))) {
print LOG &quot;Failed!!!\n&quot;;
MTIME;
print LOG &quot;You have to specify either OPTION: i or OPTION: l.\n&quot;;
exit 3;
} elsif ((exists($OPT{&quot;h&quot;}) && (exists($OPT{&quot;t&quot;}) && ( $et ne &quot;\.gif&quot; )))) {
print LOG &quot;Failed!!!\n&quot;;
MTIME;
print LOG &quot;This cannot be done, please try again.\n&quot;;
exit 3;
} else {
print LOG &quot;Done.\n\n&quot;;
}

# State in the log what options are being processed.
MTIME;
print LOG &quot;Create Image: $COF\n&quot;;
MTIME;
print LOG &quot;Create HTML: $HOF\n&quot;;
MTIME;
print LOG &quot;Input Directory: $idir\n&quot;;
MTIME;
print LOG &quot;List File: $list\n&quot;;
MTIME;
print LOG &quot;Output Directory: $odir\n&quot;;
MTIME;
print LOG &quot;Image type: $it\n\n&quot;;

# Setup transitional variables and run main program.
while($file = shift(@fn)) {
$file =~ /(.*\/)(.*)(\.$breed)(.*)/; # Tif File
$ipath = $1; # Input Path
$filename = $2; # Name of file
$ext = $3; # Extension
if ( $ext eq &quot;\.tif&quot; ) {
$iann = &quot;$ipath$filename.ann&quot;; # Input annotation
} else {
$iann = $ipath . $filename . $ext;
}
if ( $ext eq &quot;\.ann&quot; ) {
$img = &quot;$ipath$filename.tif&quot;; # Input tif
} else {
$img = $ipath . $filename . $ext; # Tif by default.
}
$ohtm = $odir . $filename . $eh; # Output HTML
$oimg = $odir . $filename . $et; # Output Image

# Process Img first.
if (!(-e $img)) {
MTIME;
print ERROR &quot;File Missing: $img\n&quot;;
} else {
MTIME;
IMGINFO;
}
if (exists($OPT{&quot;c&quot;})) {
MTIME;
IMGCONV;
} else {
undef $image;
}

# Process HTML second.
if (-e $ohtm) {
MTIME;
print LOG &quot;Processing file: $ohtm.....Skipping!!!\n&quot;;
next;
}

$hd = &quot;<HTML>\n&quot; . &quot;<IMG SRC=\&quot;$filename$et\&quot; ALT=\&quot;$filename\&quot; USEMAP=\&quot;#mainmap\&quot; BORDER=0>\n&quot; . &quot;<MAP NAME=\&quot;mainmap\&quot;>\n&quot;;
$fd = &quot;<\/MAP>\n&quot; . &quot;<\/HTML>\n&quot;;
if (exists($OPT{&quot;h&quot;})) {
# The following MTIME (Current Time) statements had to be put here.
MTIME;
print LOG &quot;Processing file: $iann.....Done.\n&quot;;
MTIME;
print LOG &quot;Create file: $ohtm.....Done.\n\n&quot;;
MKHTM;
}
}

close(LOG);
close(ERROR);
exit 0;
#EOS



Subroutine &quot;MTIME&quot; will not work when called from subroutine: MKHTM, IMGCONV, IMGINFO, etc. So could someone please enlighten me as to why this is occuring?

Swamphen,

It does not print twice, it will not print at all when called from another subroutine, it will only print when called from outside a subroutine, when called from the main part of the script. My apologies for not making sure that my PERL script example was complete.


Again, thanks in advance for whatever help can be offered in solving this problem.

PERL Version: 5.8
 
Code:
use strict;
use warnings;

sub CT {
	my $time = `time /T`;
	chomp $time;
	print &quot;$time\n&quot;;
}

sub SOME_OTHER_ROUTINE {
	CT;
	print &quot;in some other routine\n&quot;;
}

SOME_OTHER_ROUTINE;
CT;
print &quot;end of stuff\n&quot;;
outputs
Code:
 1:23p
in some other routine
 1:23p
end of stuff
So it works just like I'd expect. Does it not for you? (and being in windows, the date command is different)

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
icrf,

In the script above, I cannot get this to work, but when I just made a test script without loading any libraries or doing anything else, this worked without a problem. Thank you for your reply, it would seem that I have missed something and need to find what that is.
 
Try running icrf's sample code but switch the order of the subroutine definitions. I think this will enlighten you (make sure you use warnings and strict).

Then call the subroutine as CT() or &CT and run it again. You should be able to figure out what is going on (the error message from the first run will be the most enlightening).

jaa
 
BTW, if you can't figure it out, read the perlsub perldoc.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top