Current location: Hot Scripts Forums » Programming Languages » Perl » OOP Programming Question


OOP Programming Question

Reply
  #1 (permalink)  
Old 12-26-03, 04:23 PM
VisualActive VisualActive is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Planet Earth
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Question OOP Programming Question

I'm still learning Object Oriented Programming but I have a question:

When I use the function br_queries and edit it so instead of print the value of
$IN{'act'}
you need to use
$IN->{'act'}

my only problem is that you get the absolute value of that variable with the %20 and all. When you use $IN{'act'} instead of $IN->{'act'} you don't get that.

here is the function/method:

sub br_queries
{
my ($string,%IN,@IN,$dat);

if ($ENV{'REQUEST_METHOD'} eq 'GET') { $string = $ENV{'QUERY_STRING'}; }
else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }

@IN = split(/&/, $string);
foreach (@IN)
{
split(/=/, $_);
$_[0] =~ s/\+/ /g;
$_[0] =~ s/\// /g;
$_[0] =~ s/\\/ /g;
$_[0] =~ s/%00//g;
$_[0] =~ s/%0a/newline/g;
$_[0] =~ s/(?:(\?)|&|[&;])s=[\w\d]{16,32}(?:&|&|;|$)//g;
$_[0] =~ s/%(..)/pack("c", hex($1))/ge;
if(defined($IN{$_[0]}))
{
$IN{$_[0]} .= "\0";
$IN{$_[0]} .= "$_[1]";
}
else
{
$IN{"$_[0]"} = $_[1];
}
}
foreach (keys %IN)
{
$IN{"$_"} =~ s/\+/ /g;
$IN{"$_"} =~ s/\// /g;
$IN{"$_"} =~ s/\\/ /g;
$IN{"$_"} =~ s/%00//g;
$IN{"$_"} =~ s/%0a/newline/g;
$IN{"$_"} =~ s/(?:(\?)|&|[&;])s=[\w\d]{16,32}(?:&|&|;|$)//g;
$IN{"$_"} =~ s/%(..)/pack("c", hex($1))/ge;
}

foreach (%IN)
{
$dat->{$_[0]} = $_[1];
}

return($dat);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-26-03, 07:02 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
I don't quite understand your question, can you rephrase possibly? I do have a question for you though. Is there any reason you're not using the CGI module to grab your query string? It's a standard Perl module and it sure beats reinventing the wheel:

Code:
#!/usr/bin/perl -w

# Don't import anything by default and use the 
# OOP interface to minimize overhead
use CGI;

# Create your new query CGI object
my $IN = CGI->new;

# Get a value from a formfield or query string parameter
my $value = $IN->param('form_field');

# Do something useful with $value here
Anyhow, if you can be a bit more specific with your question, I can try to help you out with your code.

~Charlie
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-26-03, 07:09 PM
VisualActive VisualActive is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Planet Earth
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, sorry about the confusion.

I am, as you can see, trying to parse the query string from the URI after a form is submitted for example. The routine does this fine but I the output is a hash in the form of $IN{''}. I want the output to be like this $IN->{''}. Since I added the foreach simple to do this, it works fine except that ASCII characters aren't being converted for example, %20 (a space).

I submit this: ?act=This%20is%20a%20string%20with%20spaces.

With the routine without the foreach command for the conversion, it works perfectly.

It prints this: This is a string with spaces.

With the foreach command some ASCII characters are left unchanged and

It prints this: This%20is%20a%20string%20with%20spaces.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-28-03, 11:39 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
See if this helps

Hi VisualActive,

I think I get what you're after now. See if this helps:

Code:
#!/usr/bin/perl
use warnings;
use strict;
use CGI::Carp qw/fatalsToBrowser/;

sub br_queries  {
  my ($string, $dat);
  if ($ENV{'REQUEST_METHOD'} eq 'GET') { $string = $ENV{'QUERY_STRING'}; }
  else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }

  # Regexes are expensive (CPU wise), do it as little as possible.  I see no
  # need to do it on every interation in your loop below.
  $string =~ s/\+/ /g;
  $string =~ s/\// /g;
  $string =~ s/\\/ /g;
  $string =~ s/%00//g;
  $string =~ s/%0a/newline/g;
  $string =~ s/(?:(\?)|&|[&;])s=[\w\d]{16,32}(?:&|&|;|$)//g;
  $string =~ s/%(..)/pack("c", hex($1))/ge;
  
  # If you are trying to put it in a hashref instead of a hash, do it from
  # the get-go.  Don't put it into a hash then iterate over it again to put
  # it in a hashref.
  foreach my $pair (split /&|;/, $string) {
    # It's much more readable if you use varialbe names here for your
    # keys and values
    my ($key, $value) = split /=/, $pair;
    if(defined $dat->{$key}) {
      $dat->{$key} .= "\0", $value;
    }
    else {
      $dat->{$key} = $value;
    }
  }
  return $dat;
}

# A quick test to see if it works
my $IN = br_queries;

print "Content-Type: Text/HTML\n\n";
print "<pre>\n";
print "Query string:\n";
print "-------------\n";
print "Key: $_ - Value: $IN->{$_}\n" for keys %$IN;
print "-------------\n";
print "</pre>\n";
I still highly reccomend that you look into using the CGI.pm module I linked to above. Unless you are required to do this for a school project the CGI module is a tried and tested system for doing this and is a standard perl module.

~Charlie
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 12-29-03, 05:41 PM
VisualActive VisualActive is offline
Newbie Coder
 
Join Date: Dec 2003
Location: Planet Earth
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Nice ward Chas! I appreciate your help, thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Oriented Programming Stefan PHP 29 12-30-03 12:22 PM
ADO Recordsets question vbNovice Visual Basic 0 12-22-03 11:25 PM
Newest of the new! with a question..., godavis4 New Members & Introductions 4 12-07-03 07:25 PM
Perl-PHP software Copyright question! kevin PHP 8 10-29-03 09:03 AM
php visual basic perl cgi - outsource programming to india outsource_india General Advertisements 0 10-28-03 04:21 AM


All times are GMT -5. The time now is 01:01 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.