Badger::Codec::URL - URL encode/decode


Badger documentation Contained in the Badger distribution.

Index


Code Index:

NAME

Top

Badger::Codec::URL - URL encode/decode

SYNOPSIS

Top

    use Badger::Codec::URL;
    my $codec   = Badger::Codec::URL->new();
    my $encoded = $codec->encode("Hello World!");
    my $decoded = $codec->decode($encoded);

DESCRIPTION

Top

This module implements a subclass of Badger::Codec for URL encoding and decoding. Note the difference between URI and URL. URI encoding is strict and encodes characters like ;, ? and /. The URL codec is more lax and does not encode these characters.

The URI codec should be used for encoding URL parameters. The URL codec can be used to encode complete URLs.

FUNCTIONS

Top

encode_url($data)

This function URL-encodes the $data passed as an argument.

decode_url($data)

This function URL-decodes the $data passed as an argument.

METHODS

Top

encode($data)

This method URL-encodes the data referenced by the first argument. It delegates to the encode_url() function.

    $encoded = Badger::Codec::URL->encode($data);   

decode($data)

This method decodes the encoded data passed as the first argument. It delegates to the decode_url() function.

    $decoded = Badger::Codec::URL->decode($encoded);

encoder()

This method returns a reference to the encode_url() function.

decoder()

This method returns a reference to the decode_url() function.

AUTHOR

Top

Andy Wardley http://wardley.org/

COPYRIGHT

Top

SEE ALSO

Top

Badger::Codecs, Badger::Codec


Badger documentation Contained in the Badger distribution.

#========================================================================
#
# Badger::Codec::URL
#
# DESCRIPTION
#   Codec module for URL encoding/decoding 
#
# AUTHOR
#   Andy Wardley   <abw@wardley.org>
#
#========================================================================

package Badger::Codec::URL;

use Badger::Class
    version => 0.01,
    base    => 'Badger::Codec::URI';

# cache of escaped characters is shared with Badger::Codec::URI
our $URI_ESCAPES = $Badger::Codec::URI::URI_ESCAPES;

sub encode_url {
    my $url = shift;
    utf8::encode($url) if $] >= 5.008;
    
    $URI_ESCAPES ||= {
        map { ( chr($_), sprintf("%%%02X", $_) ) } 
        (0..255)
    };
  
    # the different between the URL and URI encoding is that URL does
    # not escape any of: ; / ? : @ & = + $
    $url =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()])/$URI_ESCAPES->{$1}/eg;
    $url;
}

*decode_url = \&Badger::Codec::URI::decode_uri;

sub encode {
    shift;
    goto &encode_url;
}

sub decode {
    shift;
    goto &decode_url;
}

sub encoder {
    \&encode_url;
}

sub decoder {
    \&decode_url;
}


1;


__END__

# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: