I was just reading How to delete a file with a print statement, which mentions that the key in a hash is evaluated as code when the hash is interpoloated. I had the sudden inspiration that this might be a way to clean up (slightly) the ugliness of method interpolation.

For example, if you have an object $foo, and you want to print one of its attributes you can choose one of the following approaches:

my $attr = $foo->attr; print "Attribute is $attr\n"; print "Attribute is ". $foo->attr ."\n"; print "Attribute is @{[$foo->attr]}\n";

In my opinion all of these options leave somthing to be desired.

My inspiration was that, since a hash key is evaluated, why not just use a tied hash that does nothing but returns the key. So $foo{'a value' will return 'a value'. I'm not sure about the wisdom of this move, but it seems to make the syntax a little cleaner.

Any thoughts on whether this sort of thing is a good idea? Or, perhaps, retched and foolish?

use strict; use warnings; # Create a dummy class use Class::Struct Foo => [ attr => '$' ]; # with a dummy method sub Foo::Repeater { my $self = shift; my $times = shift || 1; return ($self->attr) x $times; # Make sure x operates in list con +text } # Create a dummy object my $foo = Foo->new(attr => 'foo'); # Create my interpolater. my %im; tie %im, 'InterpolateMethods'; # Example: Concatenation. print "The Foo object's attr attribute is: " . $foo->attr . "\n"; # Ugly inline print "The Foo object's attr attribute is: @{[$foo->attr]}\n"; # Use the interpolater print "The Foo object's attr attribute is: @im{ $foo->attr }\n"; # We can use the interpolater in list context as well. print "Calling Foo's Repeater(3) method yeilds: @im{ $foo->Repeater(3) + }\n"; package InterpolateMethods; use Tie::Hash; sub TIEHASH { my $self = shift; my $mi = \$self; return bless $mi, $self; } sub FETCH { my $self = shift; my $key = shift; return $key; } 1; __END__ The Foo object's attr attribute is: foo The Foo object's attr attribute is: foo The Foo object's attr attribute is: foo Calling Foo's Repeater(3) method yeilds: foo foo foo


TGI says moo


In reply to Interpolating method calls by TGI

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.