I've been thinking alot about your post the past few days. It strikes me as a really cool idea, but some things about your solution bother me...

...mainly, I feel like you aren't really AUTOLOADing anything. Your import method is using eval to dynamicly create the method -- at that point it exists, and the AUTOLOAD isn't needed. (unless you add some direct calls to Games::Dice->foo, but even then: the eval called by your AUTOLOAD is dynamicly creating the method, after the first invokation, your AUTOLOAD won't be used.

I tweaked your code some (see below), mainly to replace the eval in the AUTOLOAD with a call out to a new "roll" method, and the eval in your import with some direct symbol table munging.

The problem is, This still isn't "importind autoloaded functions" ... anything a client tries to import, is acctually created at import time.

Can anybody out there think of a way to genuinely create an entry the symbol table for a package that points to "AUTOLOAD" ?

(PS: I changed the name from "Games::Dice" to "Smonk" to protect the innocent.)

#!/usr/bin/perl package main; use Smonk ( 'r3d6' ); use strict; use warnings; my %rolls; for (1..100) { my $roll = r3d6; $rolls{$roll}++; } for (1..100) { my $roll = Smonk->r3d6; $rolls{$roll}++; } for (sort {$a<=>$b} keys %rolls) { print "$_ => $rolls{$_}\n"; } ##### Smonk.pm ########################### package Smonk; use warnings; use strict; use vars qw($AUTOLOAD @ISA @EXPORT_OK); @ISA = qw(Exporter); sub roll { my ($rolls, $di) = @_; my $result = $rolls; for( my $i = 0; $i < $rolls; $i++ ) { $result += int(rand($di)); } return $result; } sub AUTOLOAD { my $self = shift; if ( $AUTOLOAD =~ /^.*::r(\d+)[Dd](\d+)/ ) { return roll($1, $2); } } sub import { no strict 'refs'; my $pkg = shift; for my $dice (@_) { if ($dice =~ /r(\d+)[Dd](\d+)/) { my $meth = \sub { roll($1, $2); }; *{$pkg . '::' . $dice} = $meth; } } @EXPORT_OK = @_; Smonk->export_to_level(1, $pkg, @_); } 1;

In reply to Re: Perlish dice for gamers (oh, and importing autoloaded functions) by hossman
in thread Perlish dice for gamers (oh, and importing autoloaded functions) by Solo

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.