Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by Corion (Patriarch)
on Sep 27, 2010 at 18:06 UTC ( [id://862258]=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'...

Sorry - you don't even need "plain eval". Just assigning the anonymous subroutine to the glob is all that's needed and creates a named subroutine from the anonymous subroutine.

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

Replies are listed 'Best First'.
Re^4: Perl templating/macro creating using 'BEGIN'...
by perl-diddler (Chaplain) on Sep 27, 2010 at 20:25 UTC
    Sorry - you don't even need "plain eval". Just assigning the anonymous subroutine to the glob is all that's needed and creates a named subroutine from the anonymous subroutine.
    What glob? (and what anonymous subroutine?)

      The code block following that comment:

      sub main_package_vars { foreach my $name (@_) { no strict 'refs'; *{ $name } = sub { my $p = shift; $p->{$name} = $_[0] if @_; $p->{$name}; }; } }

      This creates accessor-style subroutines with names given in @_. You can call it as

      main_package_vars( 'Some::Namespace::foo', 'main::bar', 'baz' );
        Yuck! Don't you think the 2nd version we all came up with (somewhere below near the end of this 'Q') is much better -- it just allows it to be called:
        package_vars( qw( foo bar baz) );
        in each package, no references to what package it's in. Actually, made two, 'instance_vars' and 'class_vars', for defining/accessing either '$p->{name}' or 'our $name' type-vars in a package. That way each package can clearly declare their vars appropriately at the top of each package.

        Maybe not that important in all projects, but this one has 11 packages in 1 file, and should shorten it up considerably and make it more readable -- along with the other 'macros' I can now add, now that I understand how the concept works! Major Bonus!

        In general, it's not my first choice, to pull in in non-Core CPAN packages when I don't have to. Makes my package more prone to outside failure and slows down my development over using my own well-known routines (which usually are very simple for what they do). Exceptions are when something saves me tons of work for the trade-off (which I usually don't mind -- it's a favorable investment/trade-off in time), OR things I don't quite understand as they seem too much like magic.

        Those I accept when time or interest doesn't let me go off and figure out how to do them myself (and there are alot more than I'd like in this category), BUT, I insert them in my queue to eventually find out enough to do my own in those cases, since for what I want, like in this case, 10 lines of code I understand can me including hundreds lines of foreign, non-Core code that I don't. I'd think most perl programmers are like that, or they wouldn't be perl programmers -- they'd probably be using java or some other application language... eh?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found