1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
sub new{ my $class = shift; my %cf = ( path => "/vars/wwws/vhosts/auth/files/blog_raw", name => '', atts => [qw(title descr parent ctime)], data => {}, @_); my $self = bless{}, $class; return eval{ my $fh = IO::File->new(); my $perms = keys %{$cf{data}} ? O_RDWR|O_BINARY|O_CREAT : O_RDWR|O_BINARY; $fh->open("$cf{path}/$cf{name}", $perms) or die $!; $self->{ATTS} = $cf{atts}; $self->{FH} = $fh; $self->{NAME} = $cf{name}; $self->{BIN} = keys %{$cf{data}} ? $cf{data} : $self->_parse(); $self; } }
1
2
3
4
5
6
7
8
9
10
11
12
title=Titel der Seite
descr=Eine Beschreibung
parent=/brd
body/HTML
<!-- title=Titel der Seite -->
<!-- descr=Beschreibung zum Inhalt -->
<!-- parent=/index -->
body/HTML
my $h = $self->{STYLE} eq 'new' ? { $head =~ /(\w+)=(.*)/g } : { $head =~ /(\w+)=(.*) /g };
1
2
3
4
5
6
7
8
9
10
# <!-- parent=/index -->
my $re1 = qr(^\Q<!--\E\s?(\w+)=(.+)\s?\Q-->\E$);
# parent=/index
my $re2 = qr(^(\w+)=(.+)$);
if ( $string =~ m/${re1}|${re2}/ ) {
if ( length($1) and length($2) ) {
}
elsif ( length($3) and length($4) ) {
}
1
2
3
4
5
6
if ( $string =~ m/${re1}/
or $string =~ m/${re2}/ )
{
if ( length($1) and length($2) ) {
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# read file sub _parse{ my $self = shift; my $fh = $self->{FH}; $fh->seek(0,0); read($fh, my $bin, -s $fh); my ($head, $body) = split "\n\n", $bin, 2; my $h = { $head =~ /(\w+)=(.*)/g }; foreach my $val(values %$h){ $val =~ s/-->//; $val =~ s/\s+$//; } $h->{body} = $body; return $h; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
use 5.020; my @testdata = ( 'title=Titel der Seite', '<!-- title=Titel der Seite, XML-Stil -->', ); my $re = qr/^ ( <!--)? # Die optionale Klammer wird (1) \s*(\w+)\s*=\s*(.*\S)\s* # "Captures" ohne Leerzeichen am Rand (?(1)-->|) # Wenn (1), dann nimm --> $ /x; for my $string (@testdata) { if ($string =~ m/$re/) { say "Gefunden: '$2' = '$3'"; } }
2025-03-25T18:48:58 GwenDragonAh, Conditional Pattern – habe ich nie benutzt. Wieder was gelernt.
Ich glaub, ich muss mal wieder das Buch Friedl: "Mastering Regular Expressions", O'Reilly reaktivieren.