tie

How can I hook into Perl's print?

Here's a scenario. You have a large amount of legacy scripts, all using a common library. Said scripts use the 'print' statement for diagnostic output. No changes are allowed to the scripts - they range far and wide, have their approvals, and have long since left the fruitful valleys of oversight and control. Now a new need has arriv...

How can I call methods on a tied variable?

I've just started to learn about tie. I have a class named Link which I would like to do the following thing: if fetched, return the link's address if stored, store the new address be able to call methods on it So far, my code is : package Link; sub FETCH { my $this = shift; return $this->{"site"}; } sub STORE { my ($...

Deferring code on scope change in Perl

I often find it useful to be able to schedule code to be executed upon leaving the current scope. In my previous life in TCL, a friend created a function we called defer. It enabled code like: set fp [open "x"] defer("close $fp"); which was invoked when the current scope exited. The main benefit is that it's always invoked no m...

Why can't Win32::TieRegistry list subkeys?

Using Cygwin Perl v5.8.8 and Win32::TieRegistry 0.26. We can get a tied hash object thing for HKEY_CURRENT_USER: $ perl -e ' my %RegHash; use Win32::TieRegistry( TiedHash => \%RegHash ); use Data::Dumper; my $Key = $RegHash{"HKEY_CURRENT_USER"}; print Dumper $Key;' $VAR1 = bless( {}, 'Win32::TieRegistry' ); And this works for sub key...

Can I overload Perl's =? (And a problem while use Tie)

I choose to use tie and find this: package Galaxy::IO::INI; sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = {']' => []}; # ini section can never be ']' tie %{$self},'INIHash'; return bless $self, $class; } package INIHash; use Carp; require Tie::Hash; @INIHash::ISA = qw(Tie::StdH...

Is Tie::File lazily loading a file?

I'm planning on writing a simple text viewer, which I'd expect to be able to deal with very large sized files. I was thinking of using Tie::File for this, and kind of paginate the lines. Is this loading the lines lazily, or all of them at once? ...

How can I prevent perl from reading past the end of a tied array that shrinks when accessed?

Is there any way to force Perl to call FETCHSIZE on a tied array before each call to FETCH? My tied array knows its maximum size, but could shrink from this size depending on the results of earlier FETCH calls. here is a contrived example that filters a list to only the even elements with lazy evaluation: use warnings; use strict; pa...

Attach a bitmap on another bitmap

I have a class Building. It contains a _bitmap object, referenced to a drawable. I can draw it on an external View, where its canvas calls myBuilding.getBitmap(). Now I want to draw some windows on the building, but I need them to be "tied" to the building, so that translating the x and y of myBuilding, the windows move together. Is th...

Update the rank in a MySQL Table

I have the following table structure for a table Player Table Player { Long playerID; Long points; Long rank; } Assuming that the playerID and the points have valid values, can I update the rank for all the players based on the number of points in a single query? If two people have the same number of points, they should tie...

returning a lazily-computed scalar, in perl

I'm trying to add some functionality to our code base by using tied scalars. We have a function which is specified to return scalars. I thought I could add some features to the system by tie-ing these scalars before returning them, but it looks like the FETCH method is called just before the return, which results in an untied scalar bei...

How can I extract hash values into an array in their insertion order?

Given a hash in Perl (any hash), how can I extract the values from that hash, in the order which they were added and put them in an array? Example: my %given = ( foo => '10', bar => '20', baz => '15' ); I want to get the following result: my @givenValues = (10, 20, 15); ...