Hello,

I'm trying to use Class::Accessor to make accessors in a package inside the same file that my main script is, but apparently doesn't work. I reduced to a simple case:

#!/usr/bin/env perl use strict; use warnings; my $a = A->new({ hi => 'oi' }); print $a->hello."\n"; print $a->hi."\n"; package A; use base qw(Class::Accessor); A->mk_accessors(qw(hi)); sub hello { return "hello"; }

When I split package A into A.pm (adding '1;' at the end and 'use A;' in the main script) it works fine.

Am I missing something? Does Class::Accessor expect some kind of processing that only happens when the package is in a separate file? Are there alternatives that work with this setup (packages and main script together)?


In reply to Using Class::Accessor with packages in the same file as the main script by cmarcelo

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.