IO-Interactive

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

examples/memory_test.pl
INSTALL.SKIP
lib/IO/Interactive.pm
LICENSE
Makefile.PL
MANIFEST			This list of files
MANIFEST.SKIP
README.pod
SECURITY.md
t/00.load.t
t/busy.t
t/interactive.t
t/is_interactive.t
t/pod-coverage.t
t/pod.t
xt/changes.t
META.yml                                 Module YAML meta-data (added by MakeMaker)
META.json                                Module JSON meta-data (added by MakeMaker)

lib/IO/Interactive.pm  view on Meta::CPAN

sub _input_pending_on {
    my ($fh) = @_;
    my $read_bits = "";
    my $bit = fileno($fh);
    return if $bit < 0;
    vec($read_bits, fileno($fh), 1) = 1;
    select $read_bits, undef, undef, 0.1;
    return $read_bits;
}

sub busy (&) {
    my ($block_ref) = @_;

    # Non-interactive busy-ness is easy...just do it
    if (!is_interactive()) {
        $block_ref->();
        open my $fh, '<', \ "";
        return $fh;
    }

    # Otherwise fork off an interceptor process...
    my ($read, $write);
    pipe $read, $write;
    my $child = fork;

lib/IO/Interactive.pm  view on Meta::CPAN

=head1 NAME

IO::Interactive - Utilities for interactive I/O

=head1 VERSION

This document describes IO::Interactive version 1.02

=head1 SYNOPSIS

    use IO::Interactive qw(is_interactive interactive busy);

    if ( is_interactive() ) {
        print "Running interactively\n";
    }

    # or...

    print {interactive} "Running interactively\n";


    $fh = busy {
        do_noninteractive_stuff();
    }


=head1 DESCRIPTION

This module provides three utility subroutines that make it easier to
develop interactive applications.

The C<ARGV> filehandle, the one that C<< <> >> or an empty

lib/IO/Interactive.pm  view on Meta::CPAN


    print {interactive} "Please enter a value: ";
    my $value = <>;

You can also pass C<interactive> a writable filehandle, in which case it
writes to that filehandle if it is connected to a terminal (instead of
writing to C<*STDOUT>). Once again, the usual suspect is C<*STDERR>:

    print {interactive(*STDERR)} $warning;

=item C<busy {...}>

This subroutine takes a block as its single argument and executes that block.
Whilst the block is executed, C<*ARGV> is temporarily replaced by a closed
filehandle. That is, no input from C<*ARGV> is possible in a C<busy> block.
Furthermore, any attempts to send input into the C<busy> block through
C<*ARGV> is intercepted and a warning message is printed to C<*STDERR>.
The C<busy> call returns a filehandle that contains the intercepted input.

A C<busy> block is therefore useful to prevent attempts at input when the
program is busy at some non-interactive task.

=back

=head1 DIAGNOSTICS

=over

=item Unknown subroutine (%s) requested

This module only exports the three subroutines described above.

t/busy.t  view on Meta::CPAN

use IO::Interactive qw( busy );

print "1..2\n";

*ARGV = *DATA;

my $fh = busy {
    print "ok 1\n";
    sleep 3;
};

print <$fh>;
print <ARGV>;

__DATA__
ok 2



( run in 0.248 second using v1.01-cache-2.11-cpan-87723dcf8b7 )