Skip to content

Instantly share code, notes, and snippets.

@EvanCarroll
Created May 12, 2009 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvanCarroll/110655 to your computer and use it in GitHub Desktop.
Save EvanCarroll/110655 to your computer and use it in GitHub Desktop.
CORE::Open vs IO::File
#!/usr/bin/env perl
##
## These tests to help illustrate some of the difference between IO::File and CORE::open()
##
use feature ':5.10';
use Test::More tests => 18;
use_ok ( 'IO::File' );
my $oofh = IO::File->new('foobar', 'w');
CORE::open( my $fh, '>', 'foobar' );
{
ok ( ref $oofh , 'oofh is a reference' );
ok ( ref $fh , 'fh is a reference' );
is ( ref $oofh, ref $fh, 'they are the same reference' );
}
{
ok ( $oofh->new , 'oofh can call new()' );
ok ( $fh->new , 'fh can call new()' );
}
{
eval { $oofh->new->new };
ok ( ! $@, 'oofh can call new()->new()' );
eval { $fh->new->new };
ok ( ! $@ , 'fh can call new()->new()' );
}
{
eval { $oofh->autoflush };
ok ( ! $@ , 'oofh autoflushed' );
eval { $fh->autoflush };
ok ( ! $@ , 'fh autoflushed' );
}
{
ok ( $oofh->can( 'autoflush' ), 'oofh can autoflush' );
ok ( $fh->can( 'autoflush' ), 'fh can autoflush' );
}
SKIP: {
eval { require Scalar::Util };
skip "Scalar::Util not installed", 4 if $@;
ok ( Scalar::Util::blessed($oofh), 'oofh is an object per S:U:blessed()' );
ok ( Scalar::Util::blessed($fh), 'fh is an object per S:U:blessed()' );
ok ( Scalar::Util::openhandle($oofh), 'oofh is an filehandle per S:U:openhandle()' );
ok ( Scalar::Util::openhandle($fh), 'fh is an filehandle per S:U:openhandle()' );
}
SKIP: {
eval { require Symbol };
skip "Symbol not installed", 1 if $@;
Symbol::delete_package( 'IO::Handle' );
eval { $fh->autoflush };
ok ( ! $@ , 'fh can call autoflush wo/ IO::Handle loaded' );
eval { $fh->new };
ok ( ! $@ , 'fh can call new wo/ IO::Handle loaded' );
## oofh not needed because using it implies it is loaded, this is a confusing error for fh though
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment