Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested functions with explicit returns

Status
Not open for further replies.

Deleted

Technical User
Jul 17, 2003
470
US
Haven't seen much info on this. Basically I am nested several functions that will return a final result. Any consequences of doing it this way? Performance?

I have not see any examples like so. I understand perl is flexible. But in many cases 'flexible' doesn't mean correct.

Example:

my $ord = "asc"
my $col = "name";

my ( $pages, $links, $new_data ) = generate_results( sort_data( $ord, $col, get_db_data($id) ) );

sub get_db_data {
my $id = shift;
my $data;

....... query data ......

return $data;
}

sub sort_data {
my ( $ord, $col, $data ) = @_;

....... sort action ......

return $data;
}

sub generate_results {
my $data = shift;
my ( $pages, $links, $new_data );

....... generate per page info ......

return ( $pages, $links, $new_data );
}
 
Yeah, you can nest functions like that. Perl keeps an internal stack of functions. By default the stack will tolerate going 100 levels deep into subroutines before Perl will warn about "deep recursion"

So, even something like this would work:
Code:
&recurse();

sub recurse {
   &recurse();
}

That would recurse back through itself infinitely, and Perl will warn about the deep recursion when it gets 100 levels deep. But, in most cases, your program usually wouldn't get 100 levels deep in the stack in the first place.

So, yes, Perl is built to handle nested subroutine calls like that.

(also, that deep recursion code, don't run that, it'll eat up a lot of memory ^^)
 
by nested I am guessing you mean how you are calling the functions here:

generate_results( sort_data( $ord, $col, get_db_data($id) ) );

that is perfectly fine and should have no affect on performance that I am aware of.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top