Hi all, I'm trying to create my first module with a function which build a hash cointaining some words but I'm getting some error whose solution I can't find. This is the module:
package MyTools; use strict; BEGIN{ use Exporter (); use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 0.01; @ISA = qw (Exporter); @EXPORT = qw (); @EXPORT_OK = qw (loadwords); %EXPORT_TAGS = (); } sub loadwords { my @words = qw(a able about above across actually after again all) +; foreach (@words) { my $some_words{$_} = "1"; } } 1; __END__
When I code the same function into a Perl script, I get no errors and it can be used correctly. However, when I "use" the previous module and this script:
#!/usr/bin/perl -w use strict; use MyTools qw(loadwords); my %somewords2; # load the words from the module &loadwords; # load the words using the function &loadwords2; print "'all' is one of my words\n" if $some_words{'all'}; print "'all' is one of my words\n" if $somewords2{'all'}; sub loadwords2 { my @words2 = qw(a able about above across actually after again all +); foreach (@words2) { $somewords2{$_} = "1"; } }
I get a error message:
syntax error at MyTools.pm line 16, near "$some_words{" Compilation failed in require at ./cosa.pl line 3. BEGIN failed--compilation aborted at ./cosa.pl line 3.
What am I doing wrong? I guess I'm not exporting any variable. Any help? Please, open my eyes ;-) Thanks a lot in advance.
Res publica non dominetur.

In reply to Creating hashes into a module. by vitojph

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.