W
WRX
Hi all,
I am writing a program(code at bottom) to clean up bot posts on a web
forum.
To do this, I need to build various URL's with session id's etc.
First time I have actually used a perl module, and it's all really
smooth sailing and good fun.
However, I'm really stuck on WHY, I have has to write some code in a
particular way to achieve something.
In order to follow the methodology of re-using code, I want a
subroutine that I can pass a "regex", in order to build URL's from a
base URL.
Here it is.
my $input_url = 'http://xyz.com.au/forum/index.php';
my $test_url = &build_url( \'admin/index.php?admin=22&sid=' );
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ s/index\.php/$$tmp_regex/;
$tmp_url .= $s_id;
}
Ok, so I have created a hard reference to a string (remembers the
"Camel Book" saying one day you might need to). I then dereference it
in the actual regex function itself, and I get what I want. BUT, it
seems a bogus way to do it, and it's not even a regex I'm passing,
which is going to cost me flexibility down the road.
What I wanted to do was something like this.
my $input_url = 'http://xyz.com.au/forum/index.php';
my $test_url = &build_url( qr|index.php/admin/index.php?admin=22&sid=|
);
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ s/index\.php/$tmp_regex/;
$tmp_url .= $s_id;
}
BUT, I get the "regex reference" being dereferenced and expressed as
the regex, including status of switches etc. As follows:
http://xyz.com.au/forum/(?-xism:admin/index.php?admin=22&sid=)21292773ce2e5de93dea126ac35784df
(so, probably I need to build the regex reference beforehand, and I'm
then writing more code which is kind of self defeating, unless the
subroutine was like 20 lines long - but it's not)
....AND, what I REALLY wanted to do, was pass the ENTIRE "Regex" to the
function if one dirty big long swoop, somehow.
my $input_url = 'http://xyz.com.au/forum/index.php';
my $test_url = &build_url(
s/index\.php/index.php/admin/index.php?admin=22&sid=/ )
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ $tmp_regex;
$tmp_url .= $s_id;
}
Obviously not correct, but why can't I do something like this?
OR can I? Is there a way?
Thanks people in advance.
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new( autocheck => 1 );
use WWW::Mechanize::Frames;
my $mech_f = WWW::Mechanize::Frames->new();
my $input_url = 'http://xyz.com.au/forum/index.php';
my $name = 'username';
my $password = 'password';
my $button = 'login';
$mech->get( $input_url );
&authenticate;
my $raw_mech = $mech->content;
my $s_id = $raw_mech;
$s_id =~ tr/\n/ /;
$s_id =~ s/^.*admin\/index\.php\?sid=([0-9a-z]*)">.*$/$1/;
my $login_url = $input_url;
$login_url =~ s/index\.php/admin\/index\.php\?sid=/;
$login_url .= $s_id;
my $logout_url = $input_url;
$logout_url =~ s/index\.php/login\.php\?logout=true&sid=/;
$logout_url .= $s_id;
$mech->get( $login_url );
### Forum asks for authentication again to get into admin panel ###
&authenticate;
my $admin_url = $input_url;
$admin_url =~ s/index\.php/admin\/index\.php\?admin=1&sid=/;
$admin_url .= $s_id;
### admin panel is frames ###
$mech_f->get( $admin_url );
my @frames = $mech_f->get_frames();
$raw_mech = "";
$raw_mech = $mech->content;
### JUST TESTING RIGHT HERE ###
my $test_url = &build_url( \'admin/index.php?admin=22&sid=' );
print "$test_url\n";
#print $frames[0]->content;
#print $frames[1]->content;
&logout;
exit 0;
#==================================================================#
sub authenticate {
$mech->set_fields( $name => '' );
$mech->set_fields( $password => '' );
$mech->click;
}
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ s/index\.php/$$tmp_regex/;
$tmp_url .= $s_id;
}
sub logout {
$mech->get( $logout_url );
}
I am writing a program(code at bottom) to clean up bot posts on a web
forum.
To do this, I need to build various URL's with session id's etc.
First time I have actually used a perl module, and it's all really
smooth sailing and good fun.
However, I'm really stuck on WHY, I have has to write some code in a
particular way to achieve something.
In order to follow the methodology of re-using code, I want a
subroutine that I can pass a "regex", in order to build URL's from a
base URL.
Here it is.
my $input_url = 'http://xyz.com.au/forum/index.php';
my $test_url = &build_url( \'admin/index.php?admin=22&sid=' );
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ s/index\.php/$$tmp_regex/;
$tmp_url .= $s_id;
}
Ok, so I have created a hard reference to a string (remembers the
"Camel Book" saying one day you might need to). I then dereference it
in the actual regex function itself, and I get what I want. BUT, it
seems a bogus way to do it, and it's not even a regex I'm passing,
which is going to cost me flexibility down the road.
What I wanted to do was something like this.
my $input_url = 'http://xyz.com.au/forum/index.php';
my $test_url = &build_url( qr|index.php/admin/index.php?admin=22&sid=|
);
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ s/index\.php/$tmp_regex/;
$tmp_url .= $s_id;
}
BUT, I get the "regex reference" being dereferenced and expressed as
the regex, including status of switches etc. As follows:
http://xyz.com.au/forum/(?-xism:admin/index.php?admin=22&sid=)21292773ce2e5de93dea126ac35784df
(so, probably I need to build the regex reference beforehand, and I'm
then writing more code which is kind of self defeating, unless the
subroutine was like 20 lines long - but it's not)
....AND, what I REALLY wanted to do, was pass the ENTIRE "Regex" to the
function if one dirty big long swoop, somehow.
my $input_url = 'http://xyz.com.au/forum/index.php';
my $test_url = &build_url(
s/index\.php/index.php/admin/index.php?admin=22&sid=/ )
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ $tmp_regex;
$tmp_url .= $s_id;
}
Obviously not correct, but why can't I do something like this?
OR can I? Is there a way?
Thanks people in advance.
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new( autocheck => 1 );
use WWW::Mechanize::Frames;
my $mech_f = WWW::Mechanize::Frames->new();
my $input_url = 'http://xyz.com.au/forum/index.php';
my $name = 'username';
my $password = 'password';
my $button = 'login';
$mech->get( $input_url );
&authenticate;
my $raw_mech = $mech->content;
my $s_id = $raw_mech;
$s_id =~ tr/\n/ /;
$s_id =~ s/^.*admin\/index\.php\?sid=([0-9a-z]*)">.*$/$1/;
my $login_url = $input_url;
$login_url =~ s/index\.php/admin\/index\.php\?sid=/;
$login_url .= $s_id;
my $logout_url = $input_url;
$logout_url =~ s/index\.php/login\.php\?logout=true&sid=/;
$logout_url .= $s_id;
$mech->get( $login_url );
### Forum asks for authentication again to get into admin panel ###
&authenticate;
my $admin_url = $input_url;
$admin_url =~ s/index\.php/admin\/index\.php\?admin=1&sid=/;
$admin_url .= $s_id;
### admin panel is frames ###
$mech_f->get( $admin_url );
my @frames = $mech_f->get_frames();
$raw_mech = "";
$raw_mech = $mech->content;
### JUST TESTING RIGHT HERE ###
my $test_url = &build_url( \'admin/index.php?admin=22&sid=' );
print "$test_url\n";
#print $frames[0]->content;
#print $frames[1]->content;
&logout;
exit 0;
#==================================================================#
sub authenticate {
$mech->set_fields( $name => '' );
$mech->set_fields( $password => '' );
$mech->click;
}
sub build_url {
my $tmp_regex = shift;
my $tmp_url = $input_url;
$tmp_url =~ s/index\.php/$$tmp_regex/;
$tmp_url .= $s_id;
}
sub logout {
$mech->get( $logout_url );
}