leriksen has asked for the wisdom of the Perl Monks concerning the following question:
I am processing different types of files, some compressed and some uncompressed. The uncompressed ones I can open with just
open(HANDLE, $filename) or die ...
and the compressed ones with
open(HANDLE, "$unzip $unzip_opts $filename |") or die ...
These two different ways of opening a file are in two different sub's - _open_unzipped and _open_zipped
I also have a sub called _detect_type that uses some logic to work out which open routine is required. And it returns a reference to the required sub
sub _detect_type { ... # logic to determine type return \&_open_unzipped if $rule1; return \&_open_zipped if $rule2; return undef; }
All of this is wrapped up in a module Import.pm (shocking name I know ...)
package Import; use Exporter; @ISA = qw(Exporter); @EXPORT = qw(load_file); @EXPORT_OK = qw(_detect_type _open_zipped _open_unzipped ); %EXPORT_TAGS = {STD => \@EXPORT, TEST => \@EXPORT_OK }; sub load_file { my $filename = shift; my $openner = _detect_type($filename); &$openner($filename); while (my $line = <HANDLE>) { ... # do stuff with lines } } sub _detect_type {...} sub _open_zipped {...} sub _open_unzipped {...} 1;
Furthermore, I am a good little vegemite and I have a test harness for all this in t/Import.t
use Test::More qw(no_plan); use Import qw(:TEST); ... is($test1, $result1); is($test2, $result2); ...
Now to my problem - how do I check which code reference I got back from _detect_type.
This doesn't always work - sometimes the code reference matches and sometimes it doesn't.
is(_detect_type('unzipped.txt'), \&open_unzipped); is(_detect_type('zipped.zip'), \&open_zipped);
Is what I am doing unreasonable - or fraught with issues?
I am using Perl 5.6.0 on RH 6.2 Linux.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Comparing references to sub's
by pg (Canon) on Mar 20, 2003 at 04:24 UTC | |
by leriksen (Curate) on Mar 21, 2003 at 02:23 UTC | |
|
Re: Comparing references to sub's
by graff (Chancellor) on Mar 20, 2003 at 01:40 UTC | |
by leriksen (Curate) on Mar 20, 2003 at 03:36 UTC | |
by graff (Chancellor) on Mar 20, 2003 at 03:50 UTC | |
by rob_au (Abbot) on Mar 20, 2003 at 10:57 UTC | |
by jonadab (Parson) on Mar 20, 2003 at 06:30 UTC | |
|
Re: Comparing references to sub's
by leriksen (Curate) on Mar 20, 2003 at 01:36 UTC | |
by diotalevi (Canon) on Mar 20, 2003 at 01:51 UTC | |
by graff (Chancellor) on Mar 20, 2003 at 02:00 UTC | |
by adrianh (Chancellor) on Mar 20, 2003 at 08:49 UTC | |
|
Re: Comparing references to sub's
by jaa (Friar) on Mar 21, 2003 at 11:39 UTC | |
by leriksen (Curate) on Mar 24, 2003 at 03:41 UTC |