Hi, I realize that the lvalue syntax looks a little weird, maybe I should kick it out and just have:
$obj->header('test'); # Get header 'test' $obj->header('test','new value'); # set header test $obj->header; # Get all headers as hash
You C++-ish suggestion:
my $hdr_slot = $self->header ('test'); $hdr_slot = "new value"; # Set print "New header: $hdr_slot\n"; # Get
doesn't work in Perl because there all objects are references (to talk C++: from type *myclass not myclass). So assigning to $hdr_slot would make this variable a string with the value "new value" and would remove the reference to the object. Overloading the '=' operator only is used for generating temp copies for operators like '+=', so far I know.

The only way to do this is to tie a class to $hdr_slot. Then you can define a STORE method which gets called at every '='. See 'perldoc perltie' for more.

I read all this in the book 'Object Oriented Perl' just two weeks ago.


In reply to Re^2: Mixed lvalue/rvalue sub function by mscharrer
in thread Mixed lvalue/rvalue sub function by mscharrer

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.