[VPM] testing two refs for same target

Darren Duncan darren at DarrenDuncan.net
Fri Jul 4 18:48:17 CDT 2003


Hello.  I have one or more questions of the Perl gurus that I'm unsure of myself.

I wanted to know what the standard or best practice is for comparing two references to see if they point to the same item/object.  

What I had thought of was simply doing "$ref1 eq $ref2", but I wanted to know whether there was a better way.  

The context is that I have an array of references to objects and also a scalar having a reference to one of the same objects in the array; I want to remove the identified object from the list, so I need to know which one it is.

On a related note, do you know if Perl allows you to remove elements from an array while you are running a foreach loop on it; for example:

foreach my $item (@items) {
	if( $item eq $wanted_item ) {
		remove_current_item(@items);
		last;
	}
}

In other words, does Perl have a built-in function like "remove_current_item"?  Currently I am doing something like this:

foreach my $i (0..$#items) {
	my $i_to_remove;
	if( $items[$i] eq $wanted_item ) {
		$i_to_remove = $i;
		last;
	}
}
splice( @items, $i_to_remove, 1 );

But that looks messier than I would like.

On a separate but related question, is it reasonable to use object references as keys in a hash?  So I could for example, do this:

delete( $items{$wanted_item} );

Finally, would any of the above actions cause the object references to stringify and no longer be references?

Essentially, I'm sort of exploring new territory here.

Thanks for any answers.

BTW, I plan to upload tonight the first version of SQL::ObjectModel (name registration pending; used by Rosetta) which one can actually use for something (not just documentation), although it will be at alpha development status.  This class is an unserialized representation of SQL, and would be the standard way of formatting database commands for Rosetta to execute.  My main question above relates to deleting the link from a node's parent to itself.

Have a good day. -- Darren Duncan



More information about the Victoria-pm mailing list