CheeseLord has asked for the wisdom of the Perl Monks concerning the following question:
As I talked about here, I wrote a quick module to accomplish my desired function of having operators work on the variable itself if called in void context. And it worked well enough for the unary operators, but then damian1301 gave me the idea to extend the module's capabilities to working with map and grep. And I've actually got those working fairly well, too, but with a small problem: I can't import them properly.
I imagine it may have to do with the nature of the functions I'm messing with, or my inexperience with the things I'm playing with, but whatever it is, I don't understand it. Here's a boiled-down version of the module that still suffers from the same problem:
package Cheesy; use strict; use warnings; use Exporter; our (@ISA, @EXPORT_OK); @ISA = qw(Exporter); @EXPORT_OK = qw( grep map ); sub grep (&\@) { my $code = shift; my $ref = shift; return CORE::grep &$code, @$ref if defined wantarray; @$ref = CORE::grep &$code, @$ref; } sub map (&\@) { my $code = shift; my $ref = shift; return CORE::map &$code, @$ref if defined wantarray; @$ref = CORE::map &$code, @$ref; } 1;
And here's my test program:
#!/usr/bin/perl -w use strict; use Cheesy qw(map grep); my (@foo, # tested @bar); # desired @foo = (1 .. 5); map {$_ * 10} @foo; @bar = (10, 20, 30, 40, 50); print "@foo\n"; print "Should be: @bar\n"; @foo = (1 .. 5); Cheesy::map {$_ * 10} @foo; @bar = (10, 20, 30, 40, 50); print "@foo\n"; print "Should be: @bar\n"; @foo = qw(fish chicken cheese); grep {$_ eq 'cheese'} @foo; @bar = ('cheese'); print "@foo\n"; print "Should be: @bar\n"; @foo = qw(fish chicken cheese); Cheesy::grep {$_ eq 'cheese'} @foo; @bar = ('cheese'); print "@foo\n"; print "Should be: @bar\n";
And my output:
1 2 3 4 5 Should be: 10 20 30 40 50 10 20 30 40 50 Should be: 10 20 30 40 50 fish chicken cheese Should be: cheese cheese Should be: cheese
Without specifying the package name, it doesn't work. Were I to add one of the unary ops, though (say, uc), it would. This makes no sense to me, but then again, many things in life don't. So I ask you all - what's going on here? And how can I get this to work the way I want to? Or can I?
His Royal Cheeziness
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems with importing (and overriding) functions
by danger (Priest) on Aug 27, 2001 at 15:11 UTC | |
|
(tye)Re: Problems with importing (and overriding) functions
by tye (Sage) on Aug 27, 2001 at 20:11 UTC | |
|
Re: Problems with importing (and overriding) functions
by dragonchild (Archbishop) on Aug 27, 2001 at 17:17 UTC |