Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I never noticed before that in order to use the method as an lvalue, you had to use it as an rvalue first to define the function. But you can define AUTOLOAD as an lvalue method, and then it almost works, as the following code demonstrates:
#!/usr/bin/perl package Foo; sub new { bless {}, shift } sub DESTROY {1} sub AUTOLOAD : lvalue { my ( $method ) = $AUTOLOAD =~ /^.*::(.+)$/ or die "Invalid call to $AUTOLOAD"; *$AUTOLOAD = sub : lvalue { my $self = shift; if (@_) { $self->{$method} = shift; return $self; } $self->{$method}; }; goto &$AUTOLOAD; # Uncomment the following 'useless' line # and it works: # $Foo::baz; } package main; my $foo = Foo->new; $foo->bar = 4; print $foo->bar,"\n";
With the above code you get this error:
Can't modify goto in lvalue subroutine return at ...
If you uncomment the last line of the AUTOLOAD, you seem to fool the compiler into thinking that AUTOLOAD is going to return a valid lvalue, even though it never actually returns that value, since you goto somewhere else first.

In reply to Re: Hash::AsObj by runrig
in thread Hash::AsObj by runrig

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found