It's fine if you think it's fine, but it might get difficult to manage
in a large codebase. An alternative with more encapsulation would be
this:
{
my $interact = 0;
sub interact { $interact }
}
sub ask {
return 1 unless interact();
# otherwise do user-prompt stuff
}
-Ken
On Jan 3, 2006, at 6:16 AM, James Harvard wrote:
I'm building a script that uses a sub-routine for user-interaction:
if ( &ask('Do you want to do this?') ) {
# do stuff
}
I'd like to have a switch variable that allows me to toggle the
user-interaction mode. In other words, if $interact is false then
ask() will not prompt the user and will return true.
I see that Perl does not complain if I do this ...
my $interact = 0;
sub ask {
return 1 unless $interact;
# otherwise do user-prompt stuff
}
... but is that considered bad programming practice? It seems like
unnecessary extra typing to pass $interact as an argument to the
sub-routine every time, or to always do "if ( (! $interact) || &ask
)".
Many TIA,
James Harvard