Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > a problem about function returning point

Reply
Thread Tools

a problem about function returning point

 
 
havel.zhang
Guest
Posts: n/a
 
      04-29-2007
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
subroutine4sup_slicecheck. But the system return to "close(OUTPUT)"
where the line before exit(0) !

Anyone hit this situation?

Thank u in advanced.

Havel Zhang

 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      04-29-2007
On Apr 29, 4:00 pm, "havel.zhang" <(E-Mail Removed)> wrote:
> 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 (<$F>) {

> ... ...
> #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
> subroutine4sup_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.

--
Klaus

 
Reply With Quote
 
 
 
 
havel.zhang
Guest
Posts: n/a
 
      04-30-2007
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

On Apr 29, 11:40 pm, Klaus <(E-Mail Removed)> wrote:
> On Apr 29, 4:00 pm, "havel.zhang" <(E-Mail Removed)> wrote:
>
> > 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;


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
problem returning floating point Peter C Programming 10 11-07-2008 05:19 PM
Point to Point T1 connection problem Navneet Cisco 3 10-14-2008 03:28 PM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
returning a floating point value from a function Joe Wright C Programming 12 12-10-2004 10:18 PM



Advertisments