[ic] Useful UserTags: [wrapper]

Stefan Hornburg (Racke) racke at linuxia.de
Sun Mar 5 13:13:57 UTC 2023


This tag makes it very easy to create an arbitrary object with initial parameters:

UserTag email_with_unicode AddAttr
UserTag email_with_unicode Routine <<EOR
sub {
     my ($args) = @_;
     my %all = %$args;
     if ($all{html} and !$all{body}) {
         $all{body} = $Tag->wrapper('HTML::FormatText::WithLinks')->parse($all{html});
     }
...

Requirements: Perl module in Interchange search path and:

AllowGlobal mycat
PerlAlwaysGlobal mycat

The (global) wrapper tag:

UserTag wrapper Order class
UserTag wrapper AddAttr
UserTag wrapper Routine <<EOR
sub {
     my ($class, $opt) = @_;
     my (%args, $object, $scratch_var);

     # copy tag attributes, drop class and reparse
     %args = %$opt if $opt;
     delete $args{class};
     delete $args{reparse};
     $scratch_var = delete $args{scratch};

     # load class
     eval "require $class";

     if ($@) {
         die "[wrapper]: Failed to load class $class: $@";
     }

     eval {
         $object = $class->new(%args);
     };

     if ($@) {
         die "[wrapper] Failed to instantiate class $class: $@";
     }

     if ($scratch_var) {
         $Scratch->{$scratch_var} = $object;
     }

     return $object;
}
EOR

-- 
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper



More information about the interchange-users mailing list