Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Problems with importing (and overriding) functions

by CheeseLord (Deacon)
on Aug 27, 2001 at 14:06 UTC ( [id://108087]=perlquestion: print w/replies, xml ) Need Help??

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

    Some built-ins may not be overridden (including map() and grep()) --- this node tells you how to know which can or can not be overridden.

(tye)Re: Problems with importing (and overriding) functions
by tye (Sage) on Aug 27, 2001 at 20:11 UTC

    Your "map" and "grep" require a single array rather than a list and so you should not call them simply "map" and "grep", even if Perl were to let you. (IMHO)

            - tye (but my friends call me "Tye")
Re: Problems with importing (and overriding) functions
by dragonchild (Archbishop) on Aug 27, 2001 at 17:17 UTC
    One possible solution here is to write MAP() and GREP(). Then, using your Cheesy package, have MAP() call map when necessary. I understand that this is going away from your stated goal of directly demonstrating the usefulness of map/grep in a void context, but, until Perl6 does this, it would make us Perl5'ers who would have to maintain your code a lot happier. :)

    You know, I might just grab your code idea there. It would be nice if you would post what you've written in Code Catacombs ... (Hint, hint!)

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://108087]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found