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

Tim Maher/CONSULTIX tim at consultix-inc.com
Wed Jan 26 18:48:53 CST 2000


On Wed, Jan 26, 2000 at 03:26:46PM -0800, El JoPe Magnifico wrote:
> 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()...

Thanks for the *z*ub!

But you're missing my point; Dump() works perfectly well in a non-OO
program (although I'll admit perhaps it shouldn't), and in fact
examples of using Data::Dumper::Dump in this way appear in many of
the Perl books I've got on my shelf (e.g., Effective Perl, p. 148).

I'm very new to typeglobs, and figured I was just typing the wrong
stuff, but my original question was simply how do I make an alias so I
can type Dump rather than Data::Dumper::Dump, and still have my
program work.

What you're telling me is that it shouldn't have worked in the
first place, but it definitely did.

Now I'm really confused (but I'm used to that state! 8-})

Maybe the better question is why does it work at all in the form
shown in Effective Perl p. 148 (Item 37).

-Tim

> 
> 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.

Good explanation, but it doesn't fit the evidence that it actually
works perfectly well when invoked with an annonymous array ref as
the first argument (without aliasing being used).

> 
> 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-}
> 
-- 
*========================================================================*
| Tim Maher, PhD  Consultix &              (206) 781-UNIX/8649           |
|  Pacific Software Gurus, Inc             Email: tim at consultix-inc.com  |
|  UNIX/Linux & Perl Training              http://www.consultix-inc.com  |
| 2/22: UNIX  2/28: Perl Modules  2/29: Int. Perl  3/3: Pattern Matching |
*========================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    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