pmqs / IO-Compress

IO-Compress - Perl5 module to read/write compressed data in multiple formats
14 stars 16 forks source link

Modifying whatIs() to allow for scalar or array objects #40

Open jackdeguest opened 2 years ago

jackdeguest commented 2 years ago

When using a scalar object, i.e. a blessed scalar reference, IO::Uncompress::Gunzip::gunzip and I suspect the other member of the IO::Compress family as well, rejects it with the error illegal input parameter.

It would be nice that it recognises those as scalar reference.

For example, consider the following code:

#!/usr/local/bin/perl
use v5.10;
use IO::Compress::Gzip;
use IO::Uncompress::Gunzip;
my $hello = 'Hello world';
my $compressed;
IO::Compress::Gzip::gzip( \$hello => \$compressed );
my $s = MyModule->new( $compressed );
my $hello;
eval
{
    IO::Uncompress::Gunzip::gunzip( $s => \$hello );
};
say $hello eq 'Hello world' ? 'ok' : 'not ok';
say $@ if( $@ );

package MyModule;
use strict;
use warnings;

sub new
{
    my $that = shift( @_ );
    my $str  = shift( @_ );
    return( bless( \$str => ( ref( $that ) || $that ) ) );
}

__END__

Before change, this would yield:

not ok
IO::Uncompress::Gunzip::gunzip: illegal input parameter

And after change: ok

pmqs commented 2 years ago

I avoided allowing any blessed references when I wrote this code because you have no idea what is lurking behind the reference.

Need to think about this proposal, plus it will need documented & the unit tests updated.