Today I did something that's totally insane:
$image = \$image unless ref $image;
Obviously you want this:
$image = \do{my $x=$image} unless ref $image;
However, that will make a copy of the value before taking a reference, eating up memory. What we would really like here is to attach the data to another container – without copying it – and then store a reference to that container in the current one. Well, with Data::Alias from the CPAN, we can:
if ( not ref $image ) {
my $x = \$image;
alias { ( $image, $x ) = ( $x, $image ) };
}
Re:Solutions
polettix on 2008-07-06T07:35:56
Yes, eating up memory was what I was after actually. I ended up using another variable -$image_ref
- because the sub was reasonably short and thus the name change was manageable. Thanks for the tip anyway!