Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading from @ARGV and/or __DATA__ #57

Open
grtodd opened this issue Nov 13, 2014 · 1 comment
Open

Reading from @ARGV and/or __DATA__ #57

grtodd opened this issue Nov 13, 2014 · 1 comment

Comments

@grtodd
Copy link
Contributor

grtodd commented Nov 13, 2014

While trying to do this I came across a Stack Overflow thread that refers to ARGV/__DATA__ bug/s that still seem to be there.

If there is a work around or update the POD (and the SO page) could be updated. My apologies if this is a duplicate.

cheers
http://stackoverflow.com/questions/12443549/ioall-how-do-i-read-from-argv-and-data

@357r4bd
Copy link

357r4bd commented Mar 25, 2015

Hello, while this is not a general solution yet, these changes allow the file handle portion to work for the following 2 scripts.

The major question I have from what I've found here is, what is the difference between _handle and io_handle?

bash-3.2$ git diff
diff --git a/lib/IO/All.pm b/lib/IO/All.pm
index 1ea3700..83bf63f 100755
--- a/lib/IO/All.pm
+++ b/lib/IO/All.pm
@@ -512,9 +512,10 @@ sub absolute {

 sub all {
     my $self = shift;
+
     $self->_assert_open('<');
     local $/;
-    my $all = $self->io_handle->getline;
+    my $all = $self->_handle->getline;
     $self->_error_check;
     $self->_autoclose && $self->close;
     return $all;
@@ -796,8 +797,8 @@ sub _assert_open {

 sub _error_check {
     my $self = shift;
-    return unless $self->io_handle->can('error');
-    return unless $self->io_handle->error;
+    return unless $self->_handle->can('error');
+    return unless $self->_handle->error;
     $self->throw($!);
 }

diff --git a/lib/IO/All/File.pm b/lib/IO/All/File.pm
index 917bb53..f6eeeb0 100644
--- a/lib/IO/All/File.pm
+++ b/lib/IO/All/File.pm
@@ -90,7 +90,7 @@ sub open {
         $self->io_handle->open($self->pathname, @args)
           or $self->throw($self->open_msg);
     }
-    elsif (defined $self->_handle and
+    elsif (defined $self->_handle and defined $self->io_handle and
            not $self->io_handle->opened
           ) {
         # XXX Not tested

The above diff allows the following 2 scripts to work (one from the Stack Overflow link and one I created myself from that):

File 1 (Stack Overflow):

use strict;
use warnings;

use lib './lib';
use IO::All;

open my $fh, "<", "/etc/passwd" or die $!;

# Either of these should work according to the docs.
# my $io = IO::All->new($fh);
my $io = io->file->handle($fh);
print $io->all;

File 2 (mine):

use warnings;

use lib './lib';

use Data::Dumper ();
use IO::All;

my $io;

# Either of these should work according to the docs.
#$io = IO::All->new('/Users/brett/git/io-all-pm/test.pl');

open my $fh, "<", '/Users/brett/git/io-all-pm/test.pl' or die $!;

$io = IO::All->new($fh);

#$io = io->file->handle($fh);

my $fx = $io->_handle;

print $fx->getline;

print $io->all;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants