You cannot accomplish this by setting a character variable, but you can do this with procedure pointers. I've modified your example slightly to implement that. See:
program test
implicit none
abstract interface
subroutine no_args
end subroutine
end interface
procedure(no_args), pointer :: sub => null()
sub => A
call sub
sub => B
call sub
contains
subroutine A
implicit none
print *,"OK! A"
end subroutine
subroutine B
implicit none
print *,"OK! B"
end subroutine
end program
The changes are:
- Define an interface for the procedure pointer
- Declare
sub
as a procedure pointer to that abstract interface
You can then assign sub
to subroutines that take no arguments (since that is the what the interface says) and call them through sub
the way you envision.