Professional Web Applications Themes

Unusual Exporting - PERL Beginners

I have a module that needs to export a hash and a subroutine. I used "Exporter" and that's now happening, but there's a catch... I need the subroutine to make changes to that hash, when called. (I'm aware this is an unusual interface and I do have good reasons for wanting to do it.) I can't seem to get this part right. I'm using something like: #!/usr/bin/perl package MyExporter; use strict; use warnings; use Exporter; our ISA = 'Exporter'; our EXPORT = qw/ %hash routine /; our %hash = (Test => 'Works!'); sub routine { my($caller) = caller; $$caller::hash{Another_Test} = ...

  1. #1

    Default Unusual Exporting

    I have a module that needs to export a hash and a subroutine. I used
    "Exporter" and that's now happening, but there's a catch...

    I need the subroutine to make changes to that hash, when called. (I'm
    aware this is an unusual interface and I do have good reasons for
    wanting to do it.) I can't seem to get this part right.

    I'm using something like:

    #!/usr/bin/perl

    package MyExporter;

    use strict;
    use warnings;

    use Exporter;
    our ISA = 'Exporter';
    our EXPORT = qw/ %hash routine /;

    our %hash = (Test => 'Works!');

    sub routine {
    my($caller) = caller;
    $$caller::hash{Another_Test} = 'Doesn\'t work!';
    }

    1;

    __END__

    When I use Data::Dumper from the module using the example above, I get:

    $VAR1 = {
    'Test' => 'Works!'
    };

    I'm confused about why the above doesn't work. Exporter isn't somehow
    creating a lexical variable out of that hash, is it? I guess this is
    my first question: What am I not understanding about the above?

    Second question: What is the correct way to do what I'm trying to do?

    Thanks for your time and any tips you can pass on.

    James

    James Guest

  2. #2

    Default Re: Unusual Exporting

    Sorry to answer my own question but...

    On Aug 2, 2004, at 1:37 PM, James Edward Gray II wrote:
     

    Changing that to:

    $hash{Another_Test} = 'Now works!';

    Does the trick.
     

    I'm still a little confused about why that works. Is it because after
    the subroutine is "exported" it's called from inside the same namespace
    as the hash and can manipulate it at will? Or am I just lost (quite
    possible)?

    Thanks again.

    James

    James Guest

  3. #3

    Default Re: Unusual Exporting

    On Aug 2, James Edward Gray II said:
     

    When you export a hash, you're not copying it, you're aliasing it.
    %main::hash is not a COPY of %MyExporter::hash, but an alias to it. The
    exporting procedure is:

    my $pkg = caller; # who is exporting us?
    *{ $pkg . '::hash' } = \%hash;

    That's basically what happens.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    http://japhy.perlmonk.org/ % have long ago been overpaid?
    http://www.perlmonks.org/ % -- Meister Eckhart

    Jeff Guest

Similar Threads

  1. unusual cfquery problem
    By feral99 in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: May 9th, 12:13 AM
  2. most unusual os x system problem 10.2.x
    By Pell in forum Mac Applications & Software
    Replies: 2
    Last Post: July 27th, 11:31 PM
  3. Getting unusual error - I think.
    By Dave in forum ASP Database
    Replies: 6
    Last Post: July 7th, 08:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139