Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Perl (https://www.askmehelpdesk.com/forumdisplay.php?f=453)
-   -   Trying to return a value from a subroutine (https://www.askmehelpdesk.com/showthread.php?t=84033)

  • Apr 18, 2007, 02:38 PM
    gerdesk
    trying to return a value from a subroutine
    I have some Perl code where I am trying to return a value from a subroutine, but I'm getting all kinds of errors. Here is the code I have:

    while($sth->fetch())
    {
    # if($leventid == 2||3||4||11||21) {
    # $pagecount++;
    # }
    $page = get_timing($leventid, $tid, $tname);
    if($page == 1) {
    return 2;
    } else {
    return 0;
    }
    }

    #if($page == 1) {
    # return 2;
    #} else {
    # return 0;
    #}

    #print "Total # of pages: $pagecount\n\n";

    ################################################## ###################
    #################### Subroutines ####################################
    ################################################## ###################
    sub get_timing($$$) {
    my $event_id = shift;
    my $task_id = shift;
    my $task_name = shift;

    $event_id = int($event_id);

    if($event_id == 2) { #missed start date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 3) { #missed end date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 4) { #missed due date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 11) { #missed due date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 21) { #missed due date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    return $p;

    } #end get_timing

    These are the errors I'm getting:

    main::get_timing() called too early to check prototype at exportmonitor.pl line 46.
    Can't return outside a subroutine at exportmonitor.pl line 48.

    What am I doing wrong, and why isn't the value of $p being stored in $page above?
  • Apr 18, 2007, 03:20 PM
    l99057j
    Quote:

    Originally Posted by gerdesk
    I have some Perl code where I am trying to return a value from a subroutine, but I'm getting all kinds of errors. Here is the code I have:

    while($sth->fetch())
    {
    # if($leventid == 2||3||4||11||21) {
    # $pagecount++;
    # }
    $page = get_timing($leventid, $tid, $tname);
    if($page == 1) {
    return 2;
    } else {
    return 0;
    }
    }

    #if($page == 1) {
    # return 2;
    #} else {
    # return 0;
    #}

    #print "Total # of pages: $pagecount\n\n";

    ################################################## ###################
    #################### Subroutines ####################################
    ################################################## ###################
    sub get_timing($$$) {
    my $event_id = shift;
    my $task_id = shift;
    my $task_name = shift;

    $event_id = int($event_id);

    if($event_id == 2) { #missed start date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 3) { #missed end date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 4) { #missed due date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 11) { #missed due date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    if($event_id == 21) { #missed due date
    print "$task_name returned an error code of $event_id.\n";
    $p = 1;
    }
    return $p;

    } #end get_timing

    These are the errors I'm getting:

    main::get_timing() called too early to check prototype at exportmonitor.pl line 46.
    Can't return outside a subroutine at exportmonitor.pl line 48.

    What am I doing wrong, and why isn't the value of $p being stored in $page above?

    Got this from perldiag - various Perl diagnostics

    I'm not a Perl expert, but it sounds like you need to move the function up higher in the code file.

    You've called a function that has a prototype before the parser saw a definition or declaration for it, and Perl could not check that the call conforms to the prototype. You need to either add an early prototype declaration for the subroutine in question, or move the subroutine definition ahead of the call to get proper prototype checking. Alternatively, if you are certain that you're calling the function correctly, you may put an ampersand before the name to avoid the warning. See the perlsub manpage.
  • Apr 20, 2007, 08:19 AM
    asterisk_man
    Also, your "return 2" and "return 0" can not be used inside the main portion of your code, only from inside a subroutine. Maybe you want to use exit instead?
    exit - perldoc.perl.org

  • All times are GMT -7. The time now is 01:48 AM.