On Sat, Jun 28, 2003 at 03:51:49PM +0200, Kevin Pfeiffer wrote:
> In article <[EMAIL PROTECTED]>, John W. Krahn wrote:
> [...]
> > Perhaps this article may help you understand.
> > 
> > http://perl.plover.com/FAQs/Namespaces.html
> 
> I've read this before (but it pays to reread it) and this time I also read the 
> "Seven Useful Uses of local". One thing MJD demonstrates is a clever wayt that one 
> can write one's one "map" and "grep"-like methods/functions. Only problem is that I 
> can't get it to work.
> 
> I get the error: 
> Use of uninitialized value in concatenation (.) or string at ./sub_print_hash line 
> 20.
> Can't call method "printhash" without a package or object reference at 
> ./sub_print_hash line 20.
> 
> Here is my code:
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
> 
> # sub_print_hash
> 
> # from MJD's "Seven Useful Uses of local" - http://perl.plover.com/local.html
> # very useful technique for building your own grep and map-like functions!
> 
> 
> my %capitals = (
>             Athens =>   "Greece",
>             Moscow =>   "Russia",
>             Helsinki => "Finland",
>            );
> foreach (keys %capitals) {
>    print "$_ => $capitals{$_}\n";
> }
> 
> printhash { "$a," } %capitals;
> 
> #printhash { "$a => $b\n" } %capitals;
> 
> sub printhash (&\%) {
> 
>    my $code = shift;
>    my $hash = shift;
>    local ($a, $b);
> 
>    while (($a, $b) = each %$hash) {
>       print &$code();
>    }
> }

The only problem is that you have used printhash before declaring it,
and in this case the declaration is important.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to