Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > module for generic tag processing/formatting

Reply
Thread Tools

module for generic tag processing/formatting

 
 
Morty Abzug
Guest
Posts: n/a
 
      01-25-2013
One thing I've wanted a couple of times, and have coded once: a module for generic string processing/formatting. I looked on CPAN, and don't see it. I vaguely recall having seen something like this before. Am I missing something? Or should I genericize my current code and publish it?

The idea is to do something like this:

my $formatter=new String::Formatter(h=>"red-sonja", o=>"linux", r=>"3.0");
my $output=$formatter->format("Output for host %h\n"); # should yield "Output for host red-sonja\n"

Does something like this already exist? I'd prefer not to reinvent the wheel if at all possible. I can't find it on CPAN, but maybe I'm not looking hard enough.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      01-25-2013
Morty Abzug <(E-Mail Removed)> writes:
> One thing I've wanted a couple of times, and have coded once: a
> module for generic string processing/formatting. I looked on CPAN,
> and don't see it. I vaguely recall having seen something like this
> before. Am I missing something? Or should I genericize my current
> code and publish it?
>
> The idea is to do something like this:
>
> my $formatter=new String::Formatter(h=>"red-sonja", o=>"linux", r=>"3.0");
> my $output=$formatter->format("Output for host %h\n"); # should yield "Output for host red-sonja\n"
>
> Does something like this already exist?


Ehh ... yes ... it is called 'interpolating variables into a string',
in this case

%values = (h => 'red-sonia');
print "Output for host $values{h}";

Unless you want something much more complicated than that,

------------
package Formatter;

sub new
{
my $class = shift;
my %self;

%self = @_;
return bless(\%self, $class);
}

sub format
{
my ($self, $s) = @_;

$s =~ s/%([A-Za-z0-9_]+)/$self->{$1}/g;
return $s;
}

package main;

my $fmt = Formatter->new(h => 'red-sonja');
print $fmt->format("Host %h\n");
-------------

it exists now.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM



Advertisments