Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if someone could find a way of condensing the below snippet into something much easier. It seems to me I am repeating the logic over and over again and I imagine there is a way to improve it.

The code is pretty straight forward I think but let me know if you need me to clear something up. Also, I know regexes like this aren't pretty since the data is read from an HTML email so maybe you can think of ways to make it work BETTER?

I'm posting this because I'm having difficulties sometimes with data not being parsed correctly and I can't pinpoint it. Usually it works just fine but sometimes unexplainable data gets parsed.

my ($title, $tags, $content, $cat, $status); $stored_post =~ m/\[:title\](.+)\[:title\]/is; $title = $1; $stored_post =~ m/\[:tags\](.+)\[:tags\]/is; $tags = $1; $stored_post =~ m/\[:categories\](.+)\[:categories\]/is; $cat = $1; $stored_post =~ m/\[:post\](.+)\[:post\]/is; $content = $1; $stored_post =~ m/\[:status\](.+)\[:status\]/is; $status = $1;

Replies are listed 'Best First'.
Re: Reduce amount of duplicated code
by AnomalousMonk (Archbishop) on Aug 31, 2011 at 21:10 UTC
    ... I'm having difficulties sometimes with data not being parsed correctly and I can't pinpoint it. Usually it works just fine but sometimes unexplainable data gets parsed.

    Just a guess, but the  (.+) regex expression might be a source of trouble. The  + quantifier is greedy. See example below. (Otherwise, what 'difficulties' are you having, exactly?)

    >perl -wMstrict -le "my $s = 'foo XXX bar YYY foo ZZZ bar'; print $s =~ m{ foo (.+) bar }xmsg; print $s =~ m{ foo (.+?) bar }xmsg; " XXX bar YYY foo ZZZ XXX ZZZ
Re: Reduce amount of duplicated code
by ikegami (Patriarch) on Aug 31, 2011 at 20:40 UTC

    Assumes no tag nesting:

    my %data; while (/\[:([^\]]+)\](.+)\[:\1\]/gis) { $data{$1} = $2; }

    Update: Added missing /g

Re: Reduce amount of duplicated code
by Anonymous Monk on Sep 01, 2011 at 07:15 UTC