#!/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;