Professional Web Applications Themes

symbolic references in perl module help - PERL Beginners

Hi I have a problem, I have a PM i have this A.pm : package A; my %b; $b = { apple => \&foo1, oranges => \&foo2, open => \&foo3 }; sub foo1 { print "apples\n" } sub foo2 { print "oranges\n" } sub foo3 { my ($item) = _; print $item."\n" } 1; ## End Module MAIN : !#/bin/perl lib "$ENV/"; use MOD::A; $MOD::A::b{foo1}->(); $MOD::A::b{foo2}->(); $MOD::A::b{foo3}->("pairs"); exit(); Thats it, I keep getting a function undefined, I used require and exporter but I kept getting subruotine undefined...at times in the main:: and in the perl modules... I went around this ...

  1. #1

    Default symbolic references in perl module help

    Hi I have a problem,

    I have a PM

    i have this
    A.pm :

    package A;

    my %b;

    $b = {
    apple => \&foo1,
    oranges => \&foo2,
    open => \&foo3
    };

    sub foo1 {
    print "apples\n"
    }

    sub foo2 {
    print "oranges\n"
    }

    sub foo3 {
    my ($item) = _;
    print $item."\n"
    }

    1;

    ## End Module

    MAIN :

    !#/bin/perl

    lib "$ENV/";
    use MOD::A;

    $MOD::A::b{foo1}->();
    $MOD::A::b{foo2}->();
    $MOD::A::b{foo3}->("pairs");

    exit();


    Thats it, I keep getting a function undefined, I used require and exporter
    but I kept getting subruotine undefined...at times in the main:: and in the
    perl modules... I went around this by having the Hash of symbolic references
    in the main namespace so it works...I just need to know if PERL allows
    symbolic reference variables or hashs with symbolic references to be access
    in the Perl modules or just in the main.....

    THank you in advance....



    PERL RULEZ

    __________________________________________________ _______________
    Check out the latest news, polls and tools in the MSN 2004 Election Guide!
    http://special.msn.com/msn/election2004.armx

    Luis Guest

  2. #2

    Default Re: symbolic references in perl module help

    Luis Pachas wrote: 

    I noticed several problems.
     

    Must be global. And you need to decide if you want a hash or a hash
    reference. I'm assuming a hash reference.

    our $b = {
     

    Still assuming hash references, do you mean:

    $MOD::A::b->{apple}();
    $MOD::A::b->{oranges}();
    $MOD::A::b->{open}("pairs");
     

    Which hash of symbolic references? They are hard references, not symbolic.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default Re: symbolic references in perl module help

    On Jul 16, 2004, at 7:54 AM, Luis Pachas wrote:
     

    You're using a boat load of tricks to bypass the real problem. Why
    don't you show us the code that is producing the "function undefined"
    errors and let us fix that for you.

    James

    James Guest

  4. #4

    Default RE: symbolic references in perl module help

    Luis Pachas <com> wrote:

    : I keep getting a function undefined, I used require
    : and exporter but I kept getting subroutine undefined.
    : at times in the main:: and in the perl modules.

    Exporter works fine. Perhaps you could show us what
    you did?


    : I went around this by having the Hash of symbolic
    : references in the main namespace so it works.

    You are not using a hash of symbolic references
    in A.pm. You are using a reference to a hash of code
    references.


    : I just need to know if PERL

    Perl or perl -- never PERL!

    : allows symbolic reference variables or hashes with
    : symbolic references to be accessed in the Perl modules
    : or just in the main.

    It probably does allow that, but very few people here
    will help you with symbolic references. They are an
    advanced topic and generally considered off-topic on
    this list.


    : package A;

    use strict;
    use warnings; # assumes perl 5.6 and above


    : my %b;

    You don't use this hash in this module.


    : $b = {

    Never us $a or $b as variables. They have a special
    meaning in perl and are already in the symbol table.
    Use descriptive names!

    $b is not the same as %b.


    : apple => \&foo1,
    : oranges => \&foo2,
    : open => \&foo3
    : };

    I think you meant this. We use 'our' because we want
    to access this variable from another module. Note the
    hash keys are apple, orange, and open.

    our %fruit;

    %fruit = (
    apple => \&foo1,
    oranges => \&foo2,
    open => \&foo3,
    );

    I really wouldn't recommend that hash. It forces
    you to jump through too many hoops to get things
    working. The working example below doesn't use a hash.


    : sub foo1 {
    : print "apples\n"
    : }
    :
    : sub foo2 {
    : print "oranges\n"
    : }
    :
    : sub foo3 {
    : my ($item) = _;
    : print $item."\n"
    : }
    :
    : 1;
    :
    : ## End Module
    :


    : MAIN :
    :
    : !#/bin/perl

    use strict;
    use warnings; # assumes perl 5.6 and above


    : lib "$ENV/";

    I think you meant the following. It assumes there
    is something in $ENV. I doubt there is.

    use lib "$ENV/";


    Perhaps this is better. It assumes you are placing
    your module in a directory named MOD found in the
    current directory.

    use lib '.';


    : use MOD::A;
    :
    : $MOD::A::b{foo1}->();
    : $MOD::A::b{foo2}->();
    : $MOD::A::b{foo3}->("pairs");

    I think you mean 'pears'. It would go better with
    the fruit theme.

    $MOD::A::b{foo1} does not exist. A.pm never set %b
    equal to anything. Therefore, $b{foo1} was never set to
    a value.

    $A::b is equal to a hash reference, but foo1, foo2,
    and foo3 are not it's keys. This will probably fill
    your needs.

    package A;

    use strict;
    use warnings;

    sub foo1 {
    print "apples\n"
    }

    sub foo2 {
    return "oranges\n"
    }

    sub foo3 {
    my $fruit = shift;
    print "$fruit\n";
    }

    1;


    #!/usr/bin/perl

    use strict;
    use warnings;

    use lib './MOD';
    use A;

    A::foo1();
    A::foo2();
    A::foo3('pears');

    __END__


    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328




























    Charles Guest

Similar Threads

  1. Perl Module using XS
    By Rajan in forum PERL Modules
    Replies: 2
    Last Post: May 6th, 03:20 PM
  2. symbolic references
    By David Byrne in forum PERL Beginners
    Replies: 10
    Last Post: May 11th, 02:39 PM
  3. symbolic references and hashes
    By Gary Stainburn in forum PERL Beginners
    Replies: 2
    Last Post: January 7th, 08:08 PM
  4. Perl Module DBD::ORACLE
    By polarbear in forum PERL Modules
    Replies: 2
    Last Post: November 7th, 11:13 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