Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Import oddity

    Hello,

    Weird thing here:

    I get a variable from a module via @EXPORT_OK
    like so:

    use Foo::Monkey '$howdy'; # import the variable $howdy
    print $howdy;

    Works perfect.

    Now if I add strict->import; to my module's import function like so:

    package Foo::Monkey;;
    [ standard goodies cut]
    use base qw(Exporter);

    sub import { strict->import; }

    our $howdy = 'Howdy';

    our @EXPORT_OK = qw($howdy);

    [rest of goodies cut]

    Then the script gives me "Global symbol "$howdy" requires explicit
    package name..." since I'm not using 'my' (because the module should be
    Exporting it)


    I really really want to keep strict->import but need to be able to use
    @EXPORT_OK and friends like normal.

    Any ideas why /what is happening and what I can do to have my cake and
    eat it to? (IE Have my strict and Export it too)

    TIA

    Dan
    Dan Guest

  2. Similar Questions and Discussions

    1. Date oddity in Acrobat, default page size oddity
      Got two things driving me nuts. One, when I go to Save As a file, the file dats come up in the form like this: Saturday, July -2147483642, 41221,...
    2. pl/pgsql oddity
      Hello everyone, When writing some serverside code I ran into an oddity that I managed to boil down to this: ...
    3. Toolbox oddity
      Greetings All. Does anyone know why the toolbox icons and all the tool options along the top are solid black on some machines and not others, i've...
    4. CSS & Template Oddity
      I have created an entire website based on a template. In the beginning, I linked the CSS into the template and created pages. As I was building...
    5. OE oddity
      Greetings, I recently let a friend use my computer for a few minutes to send some e-mail and somehow they managed to mess things up somewhat. ...
  3. #2

    Default Re: Import oddity

    On Feb 17, 2004, at 7:56 AM, Dan Muey wrote:
     

    I think "add" is the wrong word here. You "replaced" the inherited
    import() method.
     

    sub import {
    my $class = shift;
    $class->SUPER::import(@_);
    strict->import;
    }

    I believe that will fix it. Not 100% sure though. Never tried it. ;)

    Hope that helps.

    James

    James Guest

  4. #3

    Default RE: Import oddity

    > On Feb 17, 2004, at 7:56 AM, Dan Muey wrote: 
    >
    > I think "add" is the wrong word here. You "replaced" the inherited
    > import() method.
    >  
    >
    > sub import {
    > my $class = shift;
    > $class->SUPER::import(@_);
    > strict->import;
    > }
    >
    > I believe that will fix it. Not 100% sure though. Never
    > tried it. ;)
    > [/ref]

    I just tried it and no go. Any other thoughts anyone?

    Simply put sub import { strict->import; } breaks Exporter's @EXPORT_OK functionality.

     

    It does help, we're getting there! Thanks :)
     
    Dan Guest

  5. #4

    Default Re: Import oddity

    Dan Muey wrote: 
    >
    > I just tried it and no go. Any other thoughts anyone?[/ref]

    The problem here is that Exporter::import() looks at the calling
    package, which in this case is Foo::Monkey *itself*, not the user
    of Foo::Monkey.

    Try this (a more general solution):

    sub import {
    my ($class) = @_;
    $class->export_to_level(2, @_);
    strict->import;
    }

    Or this (simpler):

    sub import {
    strict->import;
    goto &Exporter::import;
    }

    --
    Steve
    Steve Guest

  6. #5

    Default RE: Import oddity

     
    > >
    > > I just tried it and no go. Any other thoughts anyone?[/ref]
    >
    > The problem here is that Exporter::import() looks at the
    > calling package, which in this case is Foo::Monkey *itself*,
    > not the user of Foo::Monkey.
    > [/ref]

    Makes sense, but it works without strict->import; in the package's sub import { }
    Does that make sense? I'm stuck!
     

    I tried both and no go. All is well (IE the thigns specified
    are Exported to the script) if I do not have strict->import; (Which makes
    the script act as if they had 'use strict;' in the script)


    I think we're getting there but I am missing somethgin that is probably simple and obvious.

    Thanks a bunch

    Dan
     
    Dan Guest

  7. #6

    Default Re: Import oddity

    Dan Muey wrote: 

    Did you "use strict" or "require strict" anywhere?

    --
    Steve
    Steve Guest

  8. #7

    Default RE: Import oddity

    > Dan Muey wrote: 
    > strict->import; (Which makes  
    >
    > Did you "use strict" or "require strict" anywhere?
    > [/ref]

    package Foo::Monkey;

    use 5.006;
    use strict;
    use base qw(Exporter);

    sub import {
    strict->import; # makes script using package act as if it had done 'use strict;' itself

    # @EXPORT_OK symbols are Exported ok if strict->import; is not here, even if
    # I coment out use strict above and do not have use strict
    # in the script using the package (which I never did all along
    }

    Any thoughts?
     
    Dan Guest

Posting Permissions

  • You may not post new threads
  • You may 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