Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: autoload usage

by targetsmart (Curate)
on Feb 06, 2009 at 17:20 UTC ( [id://741960]=note: print w/replies, xml ) Need Help??


in reply to autoload usage

I admire the autoload feature of perl, I generally use this autoload for method making(accessors for object/instance specific get/set variables).

for eg,

sub AUTOLOAD { my $Self = $_[0]; my $CalledAs = $AUTOLOAD; $CalledAs =~ s/^.*:://; return if $CalledAs =~ /DESTROY/; $code = qq { sub $CalledAs { my \$Self = \$_[0]; \@_ > 1 ? \$Self->{\$CalledAs} = \$_[1]: \$Self->{\$Called +As} ; } }; eval $code; if($@){ die "Error defining sub routine, $CalledAs,$code, $@\n"; } goto &{$AUTOLOAD}; }
If you defined your autoload subroutine like above inside any of your package, you are reducing lot of coding, because with the help of above routine you achieve some thing like this.

$SomeObject->name("john");
# will set hash $SomeObject's hash like $SomeObject{'name'} = "john";
# later you can retrieve the value using
$SomeObject->name(); # will return "john";
easy isn't it.

but defining a sub-routine for every attribute like name, age, address, etc., is quite faster in terms of access than this way of creating subroutine in autoload. But the above method reduces lot of code.


Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://741960]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-03-28 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found