Help: How can I parse this properties file?

M

Michele Dondi

TJM> $line =~ s/\\\\/&backslash;/g; # translate literal backslashes [cut]
TJM> __DATA__
TJM> a=b=c
TJM> a\=b=c
TJM> a\\=b=c
TJM> a\\\=b=c
TJM> ----------------------------
MD> I like to be sure thus instead of adding "some other large number" I
MD> actually *find* something that *can't* be there:

MD> my $delim = "&". (sort @delims = $line =~ /&(\0+);/)[-1] . "\0;";
MD> $line =~ s/\\\\/$delim;/g; # translate literal backslashes
MD> # ...

Surely there's a CPAN module to do this... Or 10...

From the docs of Encode::Escape, there's also String::Escape,
Unicode::Escape, TeX::Encode, HTML::Mason::Escape,
Template::plugin::XML::Escape, URI::Escape.

There could be even more: each of them would make maximum sense in the
context it would be actually designed for, non of which seems to be
the case for the OP's requirement. That TJM chose (reasonably!) a
"markup" resemblink that appropriate for one of those *::Escape
thingies is a whole another matter. More precisely, it could get
ridiculously improbable, but if those modules don't "find something
that's surely NOT there" then one could actually concoct up an example
data file that would make them fail on it. And I was illustrating the
general concept and technique of "finding something that's surely NOT
there" which I (i) still support now (ii) FOR ONCE don't necessarily
advocate the use of an existing module for, given that it can be
expressed in an excessively big number of ways with a simple statement
too. (Here I chose sort() because the overhead is minimum, but it may
well have been reduction, and so on...)


Michele
 
D

David Combs

This is a nice example to add to an e.g. PP4, pod, etc. But first
maybe annotate it a bit more:
foreach my $line ( <DATA> ) {
chomp $line;
$line =~ s/\\\\/&backslash;/g; # translate literal backslashes

# explain how that converts SINGLE blackslash-chars.

# That is, please explain how the parsing of perl works,
# so the learner can see WHEN it will internally
# convert "//" to "/"



That's converting SINGLE "\"' backslashes to "&backslash"s
my($key, $value) = split /(?<!\\)=/, $line, 2; # use negative look-behind

Maybe add WHY you use the neg look-behind.

And just what kind of changes will that make to the file?


$key =~ tr/\\//d; # eliminate backslashes used for escaping

$key =~ s/&backslash;/\\/g; # put the literal backslashes back in

printf "%-10s %-10s\n", $key, $value;
}

__DATA__
a=b=c
a\=b=c
a\\=b=c
a\\\=b=c



Thanks!

David
 
D

David Combs

Thanks for all the reply. and this problem has been solved.
but sorry for my poor understanding on regex, and having to trouble

You want to learn about perl's regexps? You gotta buy a book,
THIS book:

"Mastering Regular Expressions", 2nd edition,
by Jeffrey Friedl, O'Reilly. (num pages: 460)

Just about everyone here will agree that this one book
is the unix|linux|perl "Regex BIBLE", and that no other
book even comes close!

Try it -- you'll like it.

David
 
T

Tim Greer

David said:
"Mastering Regular Expressions", 2nd edition,
by Jeffrey Friedl, O'Reilly.  (num pages: 460)

Probably, the third edition is a better suggestion? (542 pages, and
updated)
 
T

Tad J McClellan

David Combs said:
This is a nice example to add to an e.g. PP4, pod, etc.


What is PP4?

But first
maybe annotate it a bit more:



# explain how that converts SINGLE blackslash-chars.


That is impossible to explain...

.... because that does NOT convert single backslach characters!

That's converting SINGLE "\"' backslashes to "&backslash"s


No it isn't.

It is converting DOUBLE backslashes to "&backslash;"s

Maybe add WHY you use the neg look-behind.


Because at this point any backslashes that remain in $line are "meta".

If an equal sign is preceded by a backslash, then this is not the
equal sign that we want to split on (because it is escaped).

And just what kind of changes will that make to the file?


None.

My code does not change ANY file...

$key =~ tr/\\//d; # eliminate backslashes used for escaping

$key =~ s/&backslash;/\\/g; # put the literal backslashes back in

printf "%-10s %-10s\n", $key, $value;
}

__DATA__
a=b=c
a\=b=c
a\\=b=c
a\\\=b=c


It is poor manners to quote .sigs. Please don't do that.
 
D

David Combs

Probably, the third edition is a better suggestion? (542 pages, and
updated)

Thank you! I had no idea there was one.

I'll get it -- but briefly, roughly, what did he add in
those 80 new pages?

Thanks!

David
 
T

Tim Greer

David said:
Thank you! I had no idea there was one.

I'll get it -- but briefly, roughly, what did he add in
those 80 new pages?

Thanks!

David

I actually have no idea. The 3rd might not be better than the 2nd
edition, but the newer versions of books like this are almost always
better (usually also contain errata/fixes and more up to date
examples).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top