I was trying to expand my macro ability into per-class vars, and decided I'd like it to declare the variables using the 'our' construct. Unfortunately, while it work (under non-strict with warnings, running it under 'strict' generates errors. I'd rather it not generate either.

Presumably, its because my 'our' definition is defining the variables in the 'enclosed scope' of the eval, so pop, come out of the eval, and the 'our' is no longer in effect and vars are undefined. I've tried moving the package and our out of the eval, but to save context, I was storing the package and trying to reset it when done -- but perl is giving a syntax error when I use a variable with a package. Seems odd, but I guess that's not allowed (though I don't see that documented under 'perlfunc package'). Here's code that demonstrates the problem:

#!/usr/bin/perl -w use strict; use feature ':5.10'; # note use of BEGIN and 'caller' to achieve macro definition... BEGIN { sub class_vars { my $pck=caller; foreach(@_) { eval " my \$p=\"__PACKAGE__\"; package ${pck}; our \$$_; sub ${pck}::$_ { shift; \$$_ = \$_[0] if \@_; \$$_; } package \$p; " } } } package MyPackage; *class_vars=\&main::class_vars; { class_vars( qw(one two three) ); sub init_one_from_three { $one=$three-2; } sub new { my $package=shift; my $parms=$_[0]; my $this={}; foreach(keys %$parms) { $$_=$parms->{$_}; } bless $this, $package; } } package main; my $p=new MyPackage({three => 3,}); $p->two(1); $p->init_one_from_three; printf "two=%d, three=%d, one=%d\n",$p->two, $p->three, $p->one;
Without "-w", it will give the correct answer, but with warnings -- which I'd also like to get rid of. I switched to trying to switch package because it didn't like a package declaration in an 'our' statement. Obviously this version of the code isn't removing the redundant package declaration from the sub.

Any ideas from those more learned that I? Much appreciated!


In reply to More Macro work...out to export a definition to next-outer level? by perl-diddler

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.