Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Problem with Tie::RegexpHash

by sch (Pilgrim)
on Oct 10, 2002 at 16:53 UTC ( [id://204227]=perlquestion: print w/replies, xml ) Need Help??

sch has asked for the wisdom of the Perl Monks concerning the following question:

If I try and use the following piece of code:

#!/usr/bin/perl -w use strict; use diagnostics; use Tie::RegexpHash; my $rehash = Tie::RegexpHash->new(); $rehash->add( qr/abc/, "abc" ); $rehash->add( qr/def/, "def" );

I get the following warning:

Use of uninitialized value in string ne at /usr/local/lib/perl5/site_perl/5.8.0/Tie/RegexpHash.pm line 58 + (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl tells you what ope +ration you used the undefined value in. Note, however, that perl optimiz +es your program and the operation displayed in the warning may not necessa +rily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer +to the concatenation (.) operator, even though there is no . in your program.

Can anyone suggest what might be happening?
Thanks

Edit by tye to remove "cpan://" as noted by author

Replies are listed 'Best First'.
Re: Problem with Tie::RegexpHash
by jsprat (Curate) on Oct 10, 2002 at 18:14 UTC
    Change lines 58 and 59 of Tie::RegexpHash as follows:

    # while ( ($key ne $self->{KEYS}->[ $i ]) and # ($i < $self->{COUNT}) ) { while ( ($i < $self->{COUNT}) and ($key ne $self->{KEYS}->[ $i ]) ) {

    and evaluates left to right. I think the original author meant to short-circuit, but somehow reversed the comparisons.

      jsprat is absolutely right. The RegexpHash.pm code as is overruns the end of the array referenced in KEYS. The warning the POD talks about is a bit further down in the module in the add method. That warning is generated by the module itself, not by Perl as it was in this case.

      CU
      Robartes-

Re: Problem with Tie::RegexpHash
by Mr. Muskrat (Canon) on Oct 10, 2002 at 16:58 UTC

    When I run it I get this:

    syntax error at test.pl line 6, near "use cpan:" Execution of test.pl aborted due to compilation errors (#1) Uncaught exception from user code: syntax error at test.pl line 6, near "use cpan:" Execution of test.pl aborted due to compilation errors.

    Update: The parent node has been edited making this node irrelevent.

      Oops, I was trying to link the text in the example to the CPAN search engine which didn't work and I thought I'd removed all trace of the tags - obviously not. The code example should read:

      #!/usr/bin/perl -w use strict; use diagnostics; use Tie::RegexpHash; my $rehash = Tie::RegexpHash->new(); $rehash->add( qr/abc/, "abc" ); $rehash->add( qr/def/, "def" );

Re: Problem with Tie::RegexpHash
by Mr. Muskrat (Canon) on Oct 10, 2002 at 17:12 UTC

    Having removed the cpan:// portion of the offending line, I too get that error warning. Commenting out use diagnostics; results this error warning: Use of uninitialized value in string ne at C:/Perl/site/lib/Tie/RegexpHash.pm line 58.. Removing -w results in no errors or warnings.

    Added: Does this module not like warnings? Or is there a way to get it to work?

      Yup, thought of that :) - but then when I have problems later on (as I always do), the first things the monks would say is use -w (well, after use strict).

      Looking at the documentation for the module, it says of the add method:

      $obj->add( $key, $value );
      Adds a new key/value pair to the hash. $key can be a Regexp or a string (which is compiled into a Regexp).
      If $key is already defined, the value will be changed. If $key matches an existing key (but is not the same), a warning will be shown if warnings are enabled.

      and I suspect that the final paragraph is coming into play, but I can't see why.

        After having read the docs for Tie::RegexpHash, I still haven't found a solution. I'd say that your best bet would be to contact the author.
Re: Problem with Tie::RegexpHash
by jryan (Vicar) on Oct 10, 2002 at 17:23 UTC

    The problem is that Tie::RegexHash is a tied module, meaning that you need to use the tied interface. Try:

    use Tie::RegexpHash; my $rehash = tie my %rgxhash, 'Tie::RegexpHash'; $rehash->add( qr/abc/, "abc" ); $rehash->add( qr/def/, "def" );

      I hate to tell you this but that's not the problem at all. Your code still results in the same warning when used in conjunction with use diagnostics and -w.

      Tried using the tie method as well, but that also failed

      According to the POD, you can either use the tie method, or the OO interface - unfortunately neither of them seem to work!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://204227]
Approved by mr2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-07-14 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.