ARGV-OrDATA

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for ARGV-OrDATA

0.006   2025-12-08
        Fix the documentation.

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.

MANIFEST  view on Meta::CPAN

Changes
lib/ARGV/OrDATA.pm
Makefile.PL
MANIFEST			This list of files
README
t/00-data.t
t/01-file.t
t/02-stdin.t
t/03-package.t
t/04-unimport.t
t/05-is_funcs.t
t/input.txt

META.json  view on Meta::CPAN

{
   "abstract" : "Let the diamond operator read from DATA if there's no ARGV",
   "author" : [
      "E. Choroba <choroba@cpan.org>"
   ],
   "dynamic_config" : 1,
   "generated_by" : "ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010",
   "license" : [
      "artistic_2"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
      "version" : "2"
   },
   "name" : "ARGV-OrDATA",
   "no_index" : {
      "directory" : [
         "t",
         "inc"
      ]
   },
   "prereqs" : {
      "build" : {
         "requires" : {
            "FindBin" : "0",

META.yml  view on Meta::CPAN

---
abstract: "Let the diamond operator read from DATA if there's no ARGV"
author:
  - 'E. Choroba <choroba@cpan.org>'
build_requires:
  FindBin: '0'
  Test::More: '0'
configure_requires:
  ExtUtils::MakeMaker: '0'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010'
license: artistic_2
meta-spec:
  url: http://module-build.sourceforge.net/META-spec-v1.4.html
  version: '1.4'
name: ARGV-OrDATA
no_index:
  directory:
    - t
    - inc
requires:
  perl: '5.008'
resources:
  repository: https://github.com/choroba/argv-ordata
version: '0.006'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'

Makefile.PL  view on Meta::CPAN

use 5.006;
use strict;
use warnings;
use ExtUtils::MakeMaker;

WriteMakefile(
    NAME             => 'ARGV::OrDATA',
    AUTHOR           => q{E. Choroba <choroba@cpan.org>},
    VERSION_FROM     => 'lib/ARGV/OrDATA.pm',
    ABSTRACT_FROM    => 'lib/ARGV/OrDATA.pm',
    LICENSE          => 'artistic_2',
    PL_FILES         => {},
    MIN_PERL_VERSION => '5.008',
    CONFIGURE_REQUIRES => {
        'ExtUtils::MakeMaker' => '0',
    },
    BUILD_REQUIRES => {
        'FindBin'    => '0',
        'Test::More' => '0',
    },
    PREREQ_PM => {
        #'ABC'              => '1.6',
        #'Foo::Bar::Module' => '5.0401',
    },
    macro => { TARFLAGS => '--format=ustar -cvf' },
    dist  => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
    clean => { FILES => 'ARGV-OrDATA-*' },
    META_MERGE => {
        resources => { repository => 'https://github.com/choroba/argv-ordata' },
    },
);

README  view on Meta::CPAN

ARGV-OrDATA

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

SYNOPSIS

    use ARGV::OrDATA;

    while (<>) {
        print;
    }

    __DATA__
    You'll see this if you don't redirect something to the script's
    STDIN or you don't specify a filename on the command line.


INSTALLATION

To install this module, run the following commands:

	perl Makefile.PL
	make
	make test
	make install

SUPPORT AND DOCUMENTATION

After installing, you can find documentation for this module with the
perldoc command.

    perldoc ARGV::OrDATA

You can also look for information at:

    MetaCPAN
        https://metacpan.org/pod/ARGV::OrDATA

    GitHub, the distribution's git repository (inludes issues)
        http://github.com/choroba/argv-ordata


LICENSE AND COPYRIGHT

Copyright (C) 2017 E. Choroba

This program is free software; you can redistribute it and/or modify it

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

package ARGV::OrDATA;

use 5.006;
use strict;
use warnings;

=head1 NAME

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

=head1 VERSION

Version 0.006

=cut

our $VERSION = '0.006';

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};
    }

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

}


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

    use ARGV::OrDATA;

    while (<>) {
        print;
    }

    __DATA__
    You'll see this if you don't redirect something to the script's
    STDIN or you don't specify a filename on the command line.

=head1 DESCRIPTION

Tell your script it should use the DATA section if there's no input
coming from STDIN and there are no arguments.

You can also specify which package's DATA should be read instead of
the caller's:

    use My::Module;
    use ARGV::OrDATA 'My::Module';

    while (<>) {  # This reads from My/Module.pm's DATA section.
        print;
    }

To restore the old behaviour, you can call the C<unimport> method.

    use ARGV::OrDATA;

    my $from_data = <>;

    @ARGV = 'file1.txt';  # Ignored.

    'ARGV::OrDATA'->unimport;

    @ARGV = 'file2.txt';  # Works.

    my $from_file2 = <>;

Calling C<import> after C<unimport> would restore the DATA handle, but
B<wouldn't rewind it>, i.e. it would continue from where you stopped
(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, 1 otherwise.

=item ARGV::OrDATA::is_using_data()

Returns 1 when ARGV reads from DATA, 0 otherwise.

=back

=head1 AUTHOR

E. Choroba, C<< <choroba at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to the GitHub repository at
L<https://github.com/choroba/argv-ordata>.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc ARGV::OrDATA


You can also look for information at:

=over 4

=item * MetaCPAN

L<http://mcpan.org/pod/ARGV-OrDATA>

=item * GitHub

L<https://github.com/choroba/argv-ordata>

=back


=head1 ACKNOWLEDGEMENTS

t/00-data.t  view on Meta::CPAN

    skip "Can't run the test when stdin is not the terminal", 3
        unless -t;

    my $PIPE;
    if ('MSWin32' eq $^O && $] < 5.022) {
        open $PIPE, '-|', "$^X $FindBin::Bin/script.pl" or die $!;
    } else {
        open $PIPE, '-|', $^X, "$FindBin::Bin/script.pl" or die $!;
    }

    is $_, "data $.\n", "Read line $. from DATA" while <$PIPE>;

    ok eof $PIPE, 'closed DATA';
}

t/03-package.t  view on Meta::CPAN

use FindBin;
use lib $FindBin::Bin;

use My;

BEGIN {
    open *STDIN, '<&', IO::Pty->new
        if ! -t && eval { require IO::Pty };
}

use ARGV::OrDATA qw{ My };

SKIP: {
    skip "Can't run the test when stdin is not the terminal", 3
        unless -t;

    is $_, "package $.\n", "Read line $. from package" while <>;

    ok eof, 'end of package';
}

__DATA__
data 1
data 2

t/04-unimport.t  view on Meta::CPAN


use Test::More tests => 4;

use FindBin;

BEGIN {
    open *STDIN, '<&', IO::Pty->new
        if ! -t && eval { require IO::Pty };
}

use ARGV::OrDATA;

SKIP: {
    skip "Can't run the test when stdin is not the terminal", 4
        unless -t;

    my $file = "$FindBin::Bin/input.txt";

    is scalar <>, "data 1\n", 'read line 1 from data';

    @ARGV = $file;
    is scalar <>, "data 2\n", 'changes to @ARGV ignored';

    'ARGV::OrDATA'->unimport;
    @ARGV = $file;
    is scalar <>, "file 1\n", 'unimport works';

    'ARGV::OrDATA'->import;
    is scalar <>, "data 3\n", 'switching back to data';
}

__DATA__
data 1
data 2
data 3

t/My.pm  view on Meta::CPAN

package
    My;

use warnings;
use strict;

sub imported { 1 }

__PACKAGE__

__DATA__
package 1
package 2

t/is_using.pl  view on Meta::CPAN

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

use ARGV::OrDATA;

print ARGV::OrDATA::is_using_argv() ? 1 : 0;
print ARGV::OrDATA::is_using_data() ? 1 : 0;
print "\n";

__DATA__
1

t/is_using_package.pl  view on Meta::CPAN

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

package
    My;

use ARGV::OrDATA;

print ARGV::OrDATA::is_using_argv() ? 1 : 0;
print ARGV::OrDATA::is_using_data() ? 1 : 0;
print "\n";

__DATA__
1

t/pipe.pl  view on Meta::CPAN

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

use Test::More tests => 3;

use ARGV::OrDATA;

while (<>) {
    is $_, "pipe $.\n", "read line $. from stdin";
}
ok eof *STDIN, 'end of stdin';

__DATA__
data 1
data 2

t/script.pl  view on Meta::CPAN

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

use ARGV::OrDATA;

while (<>) {
    print;
}

__DATA__
data 1
data 2

xt/changes.t  view on Meta::CPAN

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

use Test::More tests => 1;
use ARGV::OrDATA;

use FindBin;

my $module_version = $ARGV::OrDATA::VERSION;

my $dir = $FindBin::Bin . '/..';
open my $changes, '<', "$dir/Changes" or die 'Changes not found';

my $found;
while (<$changes>) {
    $found = 1, last if /\Q$module_version\E\s/;
}

ok $found, "$module_version found in Changes";



( run in 0.993 second using v1.01-cache-2.11-cpan-140bd7fdf52 )