Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] URL Decoding

Expand Messages
  • Jeff 'japhy' Pinyan
    ... Excellent start! Warnings, strictures, and properly declared variables. ... You should really check to see if the file opened. ... Um, I m guessing you
    Message 1 of 2 , Jun 28, 2002
    • 0 Attachment
      On Jun 28, Jeremiah Megie said:

      > I have a flat text file that is full of URL Encoded paragraphs. I
      >would like to read in the file, decode it, put all of the decoded info
      >into a new file. I have done URL decoding in a CGI form before, but I
      >can't seem to get this right. It doesn't make changes, just appends the
      >data to the new file.

      >#!/usr/bin/perl -w
      >use strict;
      >
      >my $encodedfile = "./data";
      >my $decodedfile = "./newdata";
      >my @lines;
      >my $lines;

      Excellent start! Warnings, strictures, and properly declared variables.

      >open(FILE, "$encodedfile");

      You should really check to see if the file opened.

      >@lines = ;

      Um, I'm guessing you had <FILE> in there.

      >close(FILE);
      >$lines[0] =~ tr/+/ /;
      >$lines[0] =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
      >$lines[0] =~ s///g;

      These three lines are ONLY affecting the FIRST element in @lines. Don't
      you want to decode ALL the lines?

      for (@lines) {
      tr/+/ /;
      s/%([a-fA-F0-9]{2})/chr hex $1/eg;
      }

      That s///g you had was doing nothing that I can tell, unless you actually
      had something like s/<!--.*-->//g in there and your mail client removed it
      because it looked a tag...

      Anyway, GET RID OF THAT. It's absolutely ridiculous. Don't remove
      anything from the string. You're just decoding it and looking at it. And
      the smarter thing to do would be:

      s/&/&/g;
      s/</</g;
      s/>/>/g;

      That would be good enough.

      >open (_FILE_, ">>$decodedfile");

      You should REALLY check to see if the file opened.

      >print _FILE_ $_ for (@lines);
      >close (_FILE_);

      You can reuse FILE, you needn't do _FILE_. But now I have a suggestion --
      why read the ENTIRE file all at once?

      open IN, "< $encodedfile" or die "can't read $encodedfile: $!";
      open OUT, "> $decodefile" or die "can't write $decodefile: $!";

      while (<IN>) {
      tr/+/ /;
      s/%([a-fA-F0-9]{2})/chr hex $1/eg;
      s/&/&/g;
      s/</</g;
      s/>/>/g;
      print OUT;
      }

      close OUT;
      close IN;

      --
      Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
      RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
      ** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
      <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
      [ I'm looking for programming work. If you like my work, let me know. ]
    Your message has been successfully submitted and would be delivered to recipients shortly.