Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

delete on tied variables

by cmac (Monk)
on Jan 12, 2009 at 03:33 UTC ( [id://735595]=perlquestion: print w/replies, xml ) Need Help??

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

I'm writing a module that implements tied interfaces. The description of the tied interface describes the DELETE function for both arrays and hashes as applying to only one entry/element.

The description of the delete function says that it can be used on a "hash element, array element, hash slice, or array slice".

Q1: What is a hash slice and how is one written in Perl?

Q2: if a delete function in a Perl program specifies a slice, will the interpreter break it up into individual calls to DELETE?

Q3: if not, what are the operands to DELETE that express a slice, for both an array and a hash?

If there are other pages that cover this subject, please tell me.

Thanks be to Perl Monks,
cmac
www.animalhead.com

Replies are listed 'Best First'.
Re: delete on tied variables
by ikegami (Patriarch) on Jan 12, 2009 at 03:38 UTC

    What is a hash slice and how is one written in Perl?

    A means of selecting multiple elements for fetching or setting. An example would be @hash{ 'key1', 'key2', ... }. See perldata for details.

    if a delete function in a Perl program specifies a slice, will the interpreter break it up into individual calls to DELETE?

    Yes.

    { package Tester; use Tie::Hash qw(); our @ISA = 'Tie::StdHash'; sub DELETE { my $self = shift; print("DELETE @_\n"); $self->SUPER::DELETE(@_) } } { tie my %hash, 'Tester'; %hash = ( a => 1, b => 2, c => 3, d => 4, ); delete @hash{qw( b c )}; print(join(' ', keys %hash), "\n"); }
    DELETE b DELETE c a d

    If there are other pages that cover this subject, please tell me.

    Update: Formatting changes. Switched from using array to using hash in DELETE test.

Re: delete on tied variables
by dragonchild (Archbishop) on Jan 12, 2009 at 16:45 UTC
    Perl is opensource. As such, there are dozens of modules on CPAN that use tying. One example is DBM::Deep. It implements tying for both arrays and hashes and it implements (nearly) every single method that the tie interface provides for arrays and hashes. If DBM::Deep doesn't do it or worry about it, then it's probably not something you need to worry about. (If I'm mistaken, I need to know cause it's a bug in DBM::Deep!)

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found