Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Perl templating/macro creating using 'BEGIN'...

by Corion (Patriarch)
on Sep 27, 2010 at 20:19 UTC ( [id://862284]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl templating/macro creating using 'BEGIN'...
in thread Perl templating/macro creating using 'BEGIN'...

The ->import subroutine that is called from a use statement can find out the name of the calling package by using caller.

Replies are listed 'Best First'.
Re^4: Perl templating/macro creating using 'BEGIN'...
by perl-diddler (Chaplain) on Sep 27, 2010 at 21:24 UTC
    Yup, this works:
    #!/usr/bin/perl -w #use strict; use feature ':5.10'; BEGIN { sub package_vars { my $pck=caller; foreach(@_) { eval "sub ${pck}::$_ { my \$p = shift; \$p->{$_} = \$_[0] if \@_; \$p->{$_}; }" } } } package MyPackage; *package_vars=\&main::package_vars; { 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;
    Yeah!

    Now I understand how this works and can write my own 'macros' as needed rather than trying to find some specialized package to write the macro I want...

    Thanks all!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-04-16 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found