in reply to Re: Perl templating/macro creating using 'BEGIN'...
in thread Perl templating/macro creating using 'BEGIN'...
Also, why are you using string-eval, when the plain eval works just as well.Because it didn't work when I tried it. Perhaps your perl works differently?
I got: Illegal declaration of anonymous subroutine at pck0.pl line 10. What do you get?#!/usr/bin/perl -w use strict; use feature ':5.10'; package MyPackage; { BEGIN { sub package_vars { foreach(@_) { eval sub $_ { my \$p = shift; \$p->{$_} = \$_[0] if \@_; \$p->{$_}; }; } } } package_vars( qw(one two three) ); sub new { my $package=shift; my $parms=$_[0]; my $this={}; foreach(%$parms) { $this->{$_}=$parms->{$_}; } bless $this, $package; } } package main; my $p=new MyPackage({three => 3,}); $p->two(1); printf "two=%d, three=%d\n",$p->two, $p->three;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl templating/macro creating using 'BEGIN'...
by Corion (Patriarch) on Sep 27, 2010 at 18:06 UTC | |
by perl-diddler (Chaplain) on Sep 27, 2010 at 20:25 UTC | |
by Corion (Patriarch) on Sep 27, 2010 at 20:29 UTC | |
by perl-diddler (Chaplain) on Sep 28, 2010 at 02:36 UTC |