ARGV-OrDATA

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for ARGV-OrDATA

0.005   2024-11-27
        New subroutines: is_using_data and is_using_argv.

0.004   2019-01-05
        Use IO::Pty if available instead of skipping tests.

0.003   2018-12-28
        Skip tests when stdin is not a terminal.

0.002   2018-12-28
        Fix tests on MSWin and with non-standard Perl executable path.

lib/ARGV/OrDATA.pm  view on Meta::CPAN

ARGV::OrDATA - Let the diamond operator read from DATA if there's no ARGV

=head1 VERSION

Version 0.005

=cut

our $VERSION = '0.005';

sub import {
    my ($package) = $_[1] || caller;
    {   no strict 'refs';
        no warnings 'once';
        *ORIG = *ARGV;
        *ARGV = *{$package . '::DATA'} unless @ARGV || ! -t;
    }
}


sub unimport {
    my $package = shift;
    *ARGV = *ORIG;
    {   no strict 'refs';
        delete ${$package . '::'}{ORIG};
    }
    undef *ORIG;
}


sub is_using_argv {
    ! is_using_data()
}


sub is_using_data {
    my ($package) = caller;
    $package = caller 1 if 'ARGV::OrDATA' eq $package;
    return do {
        no strict 'refs';
        *ARGV eq *{$package . '::DATA' }
    }
}


=head1 SYNOPSIS

lib/ARGV/OrDATA.pm  view on Meta::CPAN

(see t/04-unimport.t).

=head2 Why?

I use this technique when solving programming contests. The sample
input is usually small and I don't want to waste time by saving it
into a file.

=head1 EXPORT

Nothing. There are 2 subroutines you can call via their fully qualified names,
though:

=over 4

=item ARGV::OrDATA::is_using_argv()

Returns 0 when ARGV reads from DATA, 0 otherwise.

=item ARGV::OrDATA::is_using_data()

t/05-is_funcs.t  view on Meta::CPAN

#!/usr/bin/perl
use warnings;
use strict;

use FindBin;
use Test::More tests => 4;

my %SCRIPT = (0 => 'is_using.pl',
              1 => 'is_using_package.pl');
sub run {
    my ($argument, $package, $expected) = @_;
    my $PIPE;
    if ('MSWin32' eq $^O && $] < 5.022) {
        open $PIPE, '-|',
                "$^X $FindBin::Bin/$SCRIPT{$package}"
                . ($argument ? "$FindBin::Bin/input.txt" : "")
            or die $!;
    } else {
        open $PIPE, '-|', $^X, "$FindBin::Bin/$SCRIPT{$package}",
                               $argument ? "$FindBin::Bin/input.txt" : ()

t/My.pm  view on Meta::CPAN

package
    My;

use warnings;
use strict;

sub imported { 1 }

__PACKAGE__

__DATA__
package 1
package 2



( run in 1.822 second using v1.01-cache-2.11-cpan-88abd93f124 )