a problem about function returning point

H

havel.zhang

hi everyone:

Today I came cross a strange problem about function returning
point.

I have a function for replace chinses character to english character.

sub replace_par{
my $str = shift;
$str =~ s/\xA3\xA8/\(/;
$str =~ s/\xA3\xA9/\)/;
return $str;
}

Then I calling this function in a subroutine. as follows:
.... ...
### first we open a text file, and read line by line:
open(F,"<aaa.txt");
while(<F>){
.... ...
#according the context we have read, we named function name.
my $progName = $conf->{'feedname'} . '_slicecheck';
#calling the named function
my ($ret,$outline) = &$progName($conf,$data,$store);

if ($ret == 2){
... ...
};
print OUTPUT "$outline\n" if ($ret==0);
}
close(F);
close(OUTPUT);

exit(0);

#the calling function name is p4sup:
#
sub p4sup_slicecheck{
my $conf = shift;
my $data = shift;
my $store = shift;

#calling function replace_par,replace the chinese character
$data->{'english_name'} = replace_par($data->{'english_name'}) if
$data->{'english_name'} =~ /\xA3\xA8/
$l = length($data->{'english_name'});
$data->{'english_name'} .= ' ' x (100 - $l) if ($l < 100);

.... ...
}

then, after calling function replace_par, system should return to
subroutine:p4sup_slicecheck. But the system return to "close(OUTPUT)"
where the line before exit(0) !

Anyone hit this situation?

Thank u in advanced.

Havel Zhang
 
K

Klaus

Today I came cross a strange problem about function returning
point.

Like with every strange problem in Perl, first try the following, put
the following two lines at the beginning of your program:

use strict;
use warnings;

Correct all errors (if any) and all warnings (if any) and re-run your
program.

If the problem persists, then proceed as follows:

[ subroutine snipped ]
Then I calling this function in a subroutine. as follows:
... ...
### first we open a text file, and read line by line:
open(F,"<aaa.txt");
while(<F>){

This probably won't make any difference to your problem, but I mention
it anyway:
The open is better written as:

open my $F, '<', 'aaa.txt' or die "Error read 'aaa.txt', $!";
while ( said:
... ...
#according the context we have read, we named function name.
my $progName = $conf->{'feedname'} . '_slicecheck';
#calling the named function

You might have a good reason to call your function indirectly ("&
$progName()"), but if you do so you must accept the increased
complexity of your code.

With regards to your problem, this function call only makes sense if
$progName eq 'p4sup_slicecheck', therefore I stronly recommend to
print out the content of $progName before you actually call the
function, like so:

print STDERR "DEB-01: will call sub $progName\n";
my ($ret,$outline) = &$progName($conf,$data,$store);

Then give a life sign when you return from the function call, like so:

print STDERR "DEB-02: back from sub $progName\n";
if ($ret == 2){
... ...
};
print OUTPUT "$outline\n" if ($ret==0);}

close(F);

Better:
close $F;
close(OUTPUT);

exit(0);

#the calling function name is p4sup:
#
sub p4sup_slicecheck{
my $conf = shift;
my $data = shift;
my $store = shift;

#calling function replace_par,replace the chinese character
$data->{'english_name'} = replace_par($data->{'english_name'}) if
$data->{'english_name'} =~ /\xA3\xA8/

There is no semicolon at the end of the above line. I seriously ask
myself whether you ever compiled your program successfully ?

Anyway, the above function should be written differently to add some
prints, like so:

#calling function replace_par,replace the chinese character
print STDERR "DEB-03: inside p4sup_slicecheck()\n";
if ($data->{'english_name'} =~ /\xA3\xA8/) {
print STDERR "DEB-04: will call sub replace_par()\n";
$data->{'english_name'} = replace_par($data->{'english_name'});
print STDERR "DEB-05: back from sub replace_par()\n";
}
print STDERR "DEB-06: continue p4sup_slicecheck()\n";
$l = length($data->{'english_name'});
$data->{'english_name'} .= ' ' x (100 - $l) if ($l < 100);

... ...

}

then, after calling function replace_par, system should return to
subroutine:p4sup_slicecheck. But the system return to "close(OUTPUT)"
where the line before exit(0) !

Run the program and watch what STDERR reports, that might be useful.
 
H

havel.zhang

Thank u Klaus:

Thank u for your point out my program line by line, thank you:)
This program be wroten three years before when i first met
Perl. So, it's be designed so complex that not necessary. And without
using strict and warnings.
Today i add 'using strict' and 'using warnings' and reduced
complex, the problem solved.
Thank u.

Havel Zhang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top