Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I am completely stumped! I'm working on a class which is implemented on a scalar reference. It overloads operations to pass through to the referenced scalar, mostly by building an appropriate string and evaluating it. It's not quite working. The frustrating thing is that after I added a few little print statements, it reports that it's eval'd the statement and gotten 1, but returns false! This code is a fairly minimal test case. You can remove the __END__ tag to see some extra testing using Test::More.
use strict; use warnings; package Object::Capsule; sub encapsulate { my $object = shift; bless \$object => 'Object::Capsule'; } use overload '${}' => sub { $_[0] }, '""' => sub { "${$_[0]}" }, '0+' => sub { 0 + ${$_[0]} }, nomethod => sub { my $expr = $_[2] ? "\$_[1] $_[3] \${\$_[0]}" : "\${\$_[0]} $_[3] \$_[1]"; print "# capsule overload eval-ing : $expr\n"; my $result = eval $expr; print "# capsule overload returning: ", $result, "\n"; return $result; }, ; package Widget; sub new { my $class = shift; bless { @_ } => $class } sub size { (shift)->{size} } use overload '""' => sub { "It's a widget!" }, '0+' => sub { $_[0]->{size} }, fallback => 1 ; package main; my $widget = new Widget size => 10; my $capsule = Object::Capsule::encapsulate($widget); my $result = $capsule eq "It's a widget!"; print "# result of comparison: ", ($result?'true':'false'), "\n"; $result = $capsule ne "It's a widget!"; print "# result of comparison: ", ($result?'true':'false'), "\n"; print "\n"; print "# -- bytes of returned strings --\n"; print "# ", join(' ',map { ord($_) } split //, "$capsule"), "\n"; print "# ", join(' ',map { ord($_) } split //, "It's a widget!"), "\n" +; print "\n--(Test::More stuff below this point)--\n"; __END__ use Test::More 'no_plan'; isa_ok($widget, 'Widget'); cmp_ok($widget, '==', 10, "widget numifies as intended" +); cmp_ok($widget, 'eq', "It's a widget!", "widget stringifies as intend +ed"); print "\n"; isa_ok($capsule, 'Object::Capsule'); isa_ok($$capsule, 'Widget'); cmp_ok($capsule, '==', 10, "capsule cmp_ok == 10"); cmp_ok($capsule, 'eq', "It's a widget!", "capsule cmp_ok eq the string +"); ok($capsule == 10, "capsule numifies as intended"); ok($capsule eq "It's a widget!", "capsule stringifies as intended" +);

In reply to proxying overloads: returns seem to lie by rjbs

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2020-11-29 23:10 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found