SPUG: Aliasing Data::Dumper::Dump; how ?

El JoPe Magnifico jope-spug at n2h2.com
Wed Jan 26 17:26:46 CST 2000


Answer to both questions: Dump() is meant to be used as an OO-style
method.  If you want a procedural interface, use Dumper() instead,
or make a wrapper around Dump().  Look at the first part of Dump()...

zub Dump {
  my($s) = shift;
  my(@out, $val, $name);
  my($i) = 0;
  local(@post);

  $s = $s->new(@_) unless ref $s;

  for $val (@{$s->{todump}}) {
  ...

Because you're not calling it as a method, neither an object nor a
package is name is passed as the first argument, contrary to what it
expects.  It then gets confused because all it does is check whether
$s is a ref, period.  In your usage, it is a ref... to an array though,
not an object.  And the first thing it tries to do with that ref is
dereference it as a hash.

Maybe it should check whether the ref is a ref to ARRAY, and if so,
call the new() method, but instead using __PACKAGE__ for the package
name, rather than the first argument (as in Data::Dumper->Dump())?
Mmmm... nah! Proper sanity-checking is for weenies!  =)
  -jp

On Wed, 26 Jan 2000, Tim Maher/CONSULTIX wrote:
>> Also, I'm trying to make an alias for Data::Dumper::Dump (unexported!),
>> using variations including:
>> 	*::Dump = \&Data::Dumper::Dump;
>> and
>> 	*Dump = *Data::Dumper::Dump;
>> 
>> but when I hit the line below
>> (which works fine with the Data::Dumper:: prefix on it), I get;
>>	Can't coerce array into hash at
>> 	/usr/lib/Perl5/5.00502/i586-linux/Data/Dumper.pm line 161. 
> 
> What hash?
> 
>> print Dump([$size, \%pals, $hosts, \@::words],
>>   ["size", "*${prefix}pals", "${prefix}hosts", "*${prefix}words"]);  
>> 
>>   (I set $prefix to :: to reload variables as globals, with "strict"
>>   in effect)
>> 
>> What's wrong here? And why doesn't Data::Dumper export Dump()
>> in the first place? 8-}




 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list