Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
There's more than one way to do things
 
PerlMonks  

Replacing perl-5.8.x's filehandle "layers" in perl-5.6.x

by shlomif (Beadle)
on Jun 25, 2007 at 14:07 UTC ( #623176=perlquestion: print w/ replies, xml ) Need Help??
shlomif has asked for the wisdom of the Perl Monks concerning the following question:

In perl-5.8.x the binmode built-in (and "open") have added more Unicode-related layers like ":utf8" or ":encoding(iso-8859-1)". However, they are completly unavailable and throw an exception in perl-5.6.x. I'm looking for a way to replace or emulate their behaviour in perl-5.6.x.

Specifically, in XML-RSS-1.30, we have the following code:

sub save { my ($self, $file) = @_; open(OUT, ">:encoding($self->{encoding})", "$file") or croak "Cannot open file $file for write: $!"; print OUT $self->as_string; close OUT; }

Assuming we want the contents of $self->as_string() to be stored in the file using the encoding $self->{encoding}, what should we do in perl-5.6.x instead? Will Encode.pm be of any help?

We'll probably keep the code for perl-5.8.x as is, but also add some workarounds for perl-5.6.x.

Comment on Replacing perl-5.8.x's filehandle "layers" in perl-5.6.x
Download Code
Re: Replacing perl-5.8.x's filehandle "layers" in perl-5.6.x
by ikegami (Pope) on Jun 25, 2007 at 14:38 UTC

    When writing, :encoding only makes sense if you try to print "strings of chars". Perl 5.6 doesn't have string of chars, so there can be no parallel to :encoding.

    To replace :encoding, you'd have to replace three things:

    • The function that converts the string of encoded bytes to a string of chars, such as :encoding() on read and decode.
    • Any string function used, such as regexps, substr, eq, etc.
    • The function that converts the string of chars to a string of encoded bytes, such as :encoding() on write and encode.

    Basically, you need a "string of chars" class for Perl 5.6 to fill in for the native one in Perl 5.8. I don't know if such a thing exists. You really should upgrade to Perl 5.8 if you're going to deal with characters sets.

    (And no, Encode won't help since it allows you to do explicitly what :encoding does implicitly. You can't convert "string of chars" if in a version of Perl where they don't exist.)

      I see. I guess we'll just default to ">", $file and don't worry about encodings at all for Perl 5.6.x and below. I'll need to consult the other maintainer about it, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://623176]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-04-18 10:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (466 votes), past polls