Ask Experts Questions for FREE Help !
Ask
    gerdesk's Avatar
    gerdesk Posts: 1, Reputation: 1
    New Member
     
    #1

    Apr 18, 2007, 02:38 PM
    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?
    l99057j's Avatar
    l99057j Posts: 57, Reputation: 18
    Junior Member
     
    #2

    Apr 18, 2007, 03:20 PM
    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.
    asterisk_man's Avatar
    asterisk_man Posts: 476, Reputation: 32
    Full Member
     
    #3

    Apr 20, 2007, 08:19 AM
    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

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Filing status different for Federal return and State return [ 1 Answers ]

Can I use a different filing status for Federal and State? I got married in October last year, but I and my wife were residing in separate states till early 2007. So, on Dec 31st we were married but did not stay together. For the state I guess we cannot file JOINT because of different states....

Dual Status Return or NR Return [ 1 Answers ]

Hi, I was in US 2006 on H1B since 1st May '06 to 8th May '07 along with Wife and son on H4 My earlier visits to US were as below 2002(H1b) - 92 Days 2003(H1b) - 83 Days 2004 (H1b) - 216 Days (Tax return filed as Dual status) 2005(H1b) - 0 Days 2006(H1b) - 147 Days

Return on Assets & Return on Equity [ 1 Answers ]

Question... Lily Cosmetics has annual sales of $500,000,000. They maintain a net after tax profit margin of 5% and they have a sales-to-assets ratio of 4. A) What is the Return on assets? B) If the debt/equity ratio is 0.5, what is the Return on Equity?


View more questions Search