Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Class::Std, 5.6.1, and AUTOLOAD

by rjbs (Pilgrim)
on Sep 22, 2005 at 15:51 UTC ( [id://494180]=perlquestion: print w/replies, xml ) Need Help??

rjbs has asked for the wisdom of the Perl Monks concerning the following question:

Recently, looking at Class::Std, a friend of mine complained that it was failing its tests on 5.6.1; it complained about deep recursion in AUTOLOAD. He later said that the problem was this:
# this line starts in column 0, and seems like runtime assignment *ID = \&Scalar::Util::refaddr; # the same for these, later: *_extract_default = _extractor_for_pair_named('default'); *_extract_init_arg = _extractor_for_pair_named('init_arg'); *_extract_get = _extractor_for_pair_named('get'); *_extract_set = _extractor_for_pair_named('set'); # later, in Class::Std::SCR... my %values_of : ATTR ( :init_arg<values> );

The handler for ATTR ends up getting called at compile time, and it, in turn, uses _extract_default. That isn't defined yet (because the ATTR handler (MODIFY_HASH_ATTRIBUTES) is being called at compile time) so AUTOLOAD gets called. AUTOLOAD, in turn, uses ID, which isn't defined yet, so it calls AUTOLOAD...!

Deep recursion happens, and we all cry.

Why isn't this a problem under, say, perl 5.8.7?

rjbs

Replies are listed 'Best First'.
Re: Class::Std, 5.6.1, and AUTOLOAD
by ikegami (Patriarch) on Sep 22, 2005 at 16:34 UTC

    You can make something execute at compile time using BEGIN. From what you say, the following would work:

    BEGIN { # this line starts in column 0, and seems like runtime assignment *ID = \&Scalar::Util::refaddr; # the same for these, later: *_extract_default = _extractor_for_pair_named('default'); *_extract_init_arg = _extractor_for_pair_named('init_arg'); *_extract_get = _extractor_for_pair_named('get'); *_extract_set = _extractor_for_pair_named('set'); } # later, in Class::Std::SCR... my %values_of : ATTR ( :init_arg<values> );
      Thanks. I understand compile time and how to cram code into it. The question isn't "how do I fix this problem" but "why isn't this a problem under my modern perl build?" Any idea?
      rjbs
        No idea. Maybe if I had something minimal I could run and debug.
Re: Class::Std, 5.6.1, and AUTOLOAD
by chromatic (Archbishop) on Sep 22, 2005 at 16:38 UTC

    The glob assignments only sort of take place at run time. They're in a module which, presumably, your code uses, so Perl loads it, compiles it, executes the code in it outside of subroutines, and then calls its import() if it exists.

    I don't know what specific bug Perl 5.6.1 had though.

      ...but those glob assignments are inside the same file as everything else I showed you. Within that file, the subs should be defined first, the attribute handling set up, and then the "runtime" assignment should occur, right? Being inside use doesn't flatten the file's internal order of execution. (That is, BEGIN inside a use'd module still happens before non-BEGIN in there.) If that's true, and I'm fairly sure it is, then I still don't see why this works.
      rjbs

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found