a problem about function returning point

Discussion in 'Perl Misc' started by havel.zhang, Apr 29, 2007.

  1. havel.zhang

    havel.zhang Guest

    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
    havel.zhang, Apr 29, 2007
    #1
    1. Advertising

  2. havel.zhang

    Klaus Guest

    Re: a problem about function returning point

    On Apr 29, 4:00 pm, "havel.zhang" <> 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
    > 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.

    --
    Klaus
    Klaus, Apr 29, 2007
    #2
    1. Advertising

  3. havel.zhang

    havel.zhang Guest

    Re: a problem about function returning point

    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 <> wrote:
    > On Apr 29, 4:00 pm, "havel.zhang" <> 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;
    havel.zhang, Apr 30, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gopi

    point to point protocol

    Gopi, Jul 13, 2004, in forum: VHDL
    Replies:
    1
    Views:
    462
    Mike Treseler
    Jul 13, 2004
  2. H aka N
    Replies:
    15
    Views:
    15,616
    Ben Jones
    Mar 2, 2006
  3. Joe Wright

    returning a floating point value from a function

    Joe Wright, Jun 5, 2004, in forum: C Programming
    Replies:
    12
    Views:
    564
    geowar
    Dec 10, 2004
  4. Peter

    problem returning floating point

    Peter, Nov 6, 2008, in forum: C Programming
    Replies:
    10
    Views:
    491
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,290
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page