Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Though I don't understand how tie() works, too, I know how to use it ;) The following class represents a hash whose keys are case-insensitive:
package Insensitive::Hash; use strict; use warnings; sub new { my ( $class, @args ) = @_; my %self; while ( my ($key, $value) = splice @args, 0, 2 ) { $self{ lc $key } = $value; } bless \%self, $class; } sub get { my ( $self, $key ) = @_; $self->{ lc $key }; } sub set { my ( $self, $key, $value ) = @_; $self->{ lc $key } = $value; } 1;
How can we use Insensitive::Hash?
use strict; use warnings; use Insensitive::Hash; my $hash = Insensitive::Hash->new( 'Content-Type' => 'text/plain', ); # $key is case-insensitive $hash->get( 'Content-Type' ); # => "text/plain" $hash->get( 'content-type' ); # => "text/plain" $hash->set( 'CONTENT-TYPE' => 'text/html' );
To implement tie() interface, rename the method names of Insensitive::Hash as follows:
package Insensitive::Hash; use strict; use warnings; # new -> TIEHASH # get -> FETCH # set -> STORE sub TIEHASH { my ( $class, @args ) = @_; my %self; while ( my ($key, $value) = splice @args, 0, 2 ) { $self{ lc $key } = $value; } bless \%self, $class; } sub FETCH { my ( $self, $key ) = @_; $self->{ lc $key }; } sub STORE { my ( $self, $key, $value ) = @_; $self->{ lc $key } = $value; } 1;
How tie() works?
use strict; use warnings; use Insensitive::Hash; tie my %hash, 'Insensitive::Hash', ( 'Content-Type' => 'text/plain' ); # <=> my $hash = Insensitive::Hash->TIEHASH( ... ) $hash{'Content-Type'}; # <=> $hash->FETCH( 'Content-Type' ); $hash{'content-type'}; # <=> $hash->FETCH( 'content-type' ); $hash{'CONTENT-TYPE'} = 'text/html'; # <=> $hash->STORE( 'CONTENT-TYPE' => 'text/html' );
Insensitive::Hash was taken from "Object-oriented Perl" written by D. Conway. See also HTTP::Headers (field names are case-insensitive).

In reply to Re^3: Tieing and Blessing by anazawa
in thread Tieing and Blessing by McA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-03-19 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found