Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Tieing and Blessing

by anazawa (Scribe)
on Oct 05, 2012 at 13:38 UTC ( #997477=note: print w/replies, xml ) Need Help??

in reply to Tieing and Blessing

I posted similar question before. See also "Blessing tied hash".

By the way, tie() is one of my favorite functions. Tie::ToObject interests me specially.

Replies are listed 'Best First'.
Re^2: Tieing and Blessing
by anaconda_wly (Scribe) on Jan 07, 2013 at 09:24 UTC
    I have the question is that firstly I didn't use DBM file yet and can't understand tie from its manual since lack the experience of dbm file. I think blessing is tieing a reference to a package actually.
      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).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997477]
Corion watches from the sidelines. Or rather, from behind, as my system only gets output from that process and my programs adhere strictly to the GIGO design principle.
[erix]: ah, that's nice to hear Corion :)
[Corion]: erix: Yeah, the sad thing is that all I can do is document things, so I can point fingers when the auditors come :-/
[Corion]: "I'm here to open tickets and point fingers. And I'm all out of tickets."
[erix]: didn't Sybase have pretty good auditing? :) (this is a vague memory)
[erix]: (culprits often are upstream of db of course)
[Corion]: Ah, how I missed it. After some years, I revisit slashdot on a click-bait link, and it provides the usual humor instantly: "I didn't know Drupal had rules for sex. It must be a plug-in"
[Corion]: erix: This is not for sybase, but for the input data files, resp. their contents.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-03-28 08:59 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (328 votes). Check out past polls.