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


In reply to Problems with importing (and overriding) functions by CheeseLord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.