[tpm] Subroutine arcana

Jim Graham james.a.graham at gmail.com
Mon May 14 07:35:38 PDT 2007


Hi 

 Without seeing your code, it's hard to say. But I can cook up a quick example to show that you can nest subroutines.

Hope this helps, Jim

File:subs.pl

#-- define subroutines
sub named_sub
{
 #-- define a named and anon sub
 sub named_internal_sub
 {
  print "\t\tIn named_internal_sub with args: @_\n";
 }
 #-- can make anon_sub private if we declare it "my". Otherwise
 #   it is globally visible
 $anon_sub = sub { print "\t\tIn internal anon sub: @_ \n"; };

 #-- Sub execution
 print "\tIn named_sub with args: @_\n";
 
 print "\tCalling internal sub from named_sub\n";
 named_internal_sub(@_);
 
 print "\tCalling anon sub from named_sub\n";
 $anon_sub->(@_);
 
}

#-- MAIN
print "In ", __FILE__, "\n";
print "Calling named sub from Main\n";

named_sub( "Some", "Set", "Arguments", 3);

#-- Named internal sub is in the symbol table
print "Calling named internal sub from Main\n";
named_internal_sub( "Some", "Set", "Arguments", 3);

#-- this can be made private if $anon_sub is declared "my" in the "named_sub"
print "Calling anon sub from Main\n";
$anon_sub->( "Some", "Set", "Arguments", 3);

__DATA__

In subs.pl
Calling named sub from Main
	In named_sub with args: Some Set Arguments 3
	Calling internal sub from named_sub
		In named_internal_sub with args: Some Set Arguments 3
	Calling anon sub from named_sub
		In internal anon sub: Some Set Arguments 3 
Calling named internal sub from Main
		In named_internal_sub with args: Some Set Arguments 3
Calling anon sub from Main
		In internal anon sub: Some Set Arguments 3 

 

-----Original Message-----
From: toronto-pm-bounces+james.a.graham=gmail.com at pm.org [mailto:toronto-pm-bounces+james.a.graham=gmail.com at pm.org] On Behalf Of
arocker at vex.net
Sent: May 14, 2007 10:04 AM
To: tpm at to.pm.org
Subject: [tpm] Subroutine arcana


Does anyone know if it is legitimate to use an anonymous subroutine in a
named one? I've run across a case in someone else's code, and the 
expression using the anonymous routine simply seems to be ignored, neither
executing nor generating a complaint.

I believe, (and taught), that named subroutines cannot be nested, but when
I sought guidance in the canon, it was strangely silent about the nesting
of either type.

_______________________________________________
toronto-pm mailing list
toronto-pm at pm.org
http://mail.pm.org/mailman/listinfo/toronto-pm



More information about the toronto-pm mailing list