[24578] in Perl-Users-Digest
Perl-Users Digest, Issue: 6754 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 1 06:05:39 2004
Date: Thu, 1 Jul 2004 03:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 1 Jul 2004 Volume: 10 Number: 6754
Today's topics:
Re: Getting to variables contained in a typeglob refere (Anno Siegel)
Re: HTTP::Request, trailing slash <gisle@ActiveState.com>
LWP POST Not Working (James Thornton)
Re: My 1st question about locking (and other related is (Anno Siegel)
split crazy <nospam@nospam.net>
Re: split crazy (Sam Holden)
Re: Totally stuck <trentcurryREM0VE@hotmail.com>
Re: Totally stuck <bigal187.invalid@adexec.com>
Re: Totally stuck (Sam Holden)
Re: Why can't I get WWW::Mechanize->find_all_links to w <kuujinbo@hotmail.com>
Why can't I upload file in my this CGI script? <komsbomb@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 1 Jul 2004 08:23:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Getting to variables contained in a typeglob referenced by a scalar.
Message-Id: <cc0hl6$pjl$1@mamenchi.zrz.TU-Berlin.DE>
ddtl <this.is@invalid> wrote in comp.lang.perl.misc:
>
> >$fh is a reference to a GLOB
> >$$fh is the GLOB itself
> >$$$fh is the SCALAR inside the GLOB
> >
>
> But how do you get to the typeglob using a funny character used to
> access scalars - glob is not scalar.
In most respects, globs *do* behave like scalars. In particular, you
can assign them to scalar variables and take references of those.
De-referencing gives you back the plain glob. That is what happens
in the code above.
> I could understand why it works if
> instead of '$' it had been possible to use other funny characters as
> well, like '@$fh' or '%$fh' -
You are describing how globs behave. They are themselves a kind of
reference.
A ref to a glob is just that. You de-reference it with "$" to
retrieve the glob, then you add a "funny character" (a sigil) to
specify which part of the glob you want.
> then i would infer: because glob contains
> everything, you can get to glob by dereferencing glob reference with
> any funny character, but in fact only '$$f' gets you a glob, all the other
> constructs generate an error. Then why it works?
Because it is programmed to work that way. It doesn't make much
sense to ask why, it just is implemented that way.
Anno
------------------------------
Date: 01 Jul 2004 01:25:16 -0700
From: Gisle Aas <gisle@ActiveState.com>
Subject: Re: HTTP::Request, trailing slash
Message-Id: <m34qosf82b.fsf@eik.g.aas.no>
"gnari" <gnari@simnet.is> writes:
> "Sebastian Bauer" <sebastian.baua@t-online.de> wrote in message
> news:cbv80l$a8e$01$1@news.t-online.com...
> > i would accept that they dont allow me to download the image via a script,
> > but what drives me crazy is that i can obviously get the image via mozilla
> > but NO other way. I tried telnet 5 mins ago -> didnt work. So it seems i
> > will need to download the images by myself or i get it working any other
> > way.
> > Thx a lot, if you have any ideas please let me know ;-)
>
> sessions, referer, cookies, useragent ...
They check the 'referer'. This program works for me:
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(keep_alive => 1);
my $res = $ua->get("http://www.taz.de/pt/.nf/gif.t,tom.d,1088676000",
referer => "http://www.taz.de/pt/2004/07/01.nf/tomnf");
print $res->as_string;
------------------------------
Date: 30 Jun 2004 21:54:55 -0700
From: james@unifiedmind.com (James Thornton)
Subject: LWP POST Not Working
Message-Id: <cabf0e7b.0406302054.3e099961@posting.google.com>
I'm trying to write an LWP script that POSTs to
https://lending.etrade.com/e/t/mortgage/expressratequote and returns
mortgage rates, but I cannot get it to work. Instead of returning
rates, it returns the same URL (as it should), but the page contains
the form autopopulated with the supplied values.
The page uses a session ID, and I think I'm managing it properly. To
rule out JavaScript interference, I turned off JavaScript in my
browser, submitted the form, and it returned the rates so JavaScript
isn't required. What am I missing? Thanks.
###################################
#!/usr/bin/perl -w
# Perl 5.8.0
# libwww-perl-5.79
use LWP::Debug qw(+);
use LWP::UserAgent;
use HTTP::Cookies;
use URI::URL;
my %tags = ();
#$tags{'frm'} = '/e/t/mortgage/expressratequote';
$tags{'loanpurpose'} = 'P';
$tags{'_formtarget'} = 'expressratequote';
$tags{'verifiedincome'} = 'y';
$tags{'cashout'} = 'none';
$tags{'genericloanprogram'} = '30-year fixed';
$tags{'amount'} = '200000';
$tags{'propertytype'} = 'SFR';
$tags{'estval'} = '300000';
$tags{'state'} = 'CA';
$tags{'purpose'} = '0';
$tags{'County'} = 'nothing';
$tags{'county1'} = 'nothing';
$tags{'getrates'} = 'submit';
my $curl = url("http:");
$curl->query_form(%tags);
my $ua = LWP::UserAgent->new(keep_alive => 50,
timeout => 30,
requests_redirectable => ['GET', 'HEAD', 'POST']);
$ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
autosave => 1,
ignore_discard => 0));
my $cookie_jar = HTTP::Cookies->new;
#
# First request to init session ID
#
my $req = HTTP::Request->new(GET =>
'https://lending.etrade.com/e/t/mortgage/expressratequote');
$req->user_agent('Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)');
my $res = $ua->request($req);
# Grab the request object that generated the response
# (it could have redirected in which case the request object
# would be different than the one we created)
$req = $res->request();
$cookie_jar->extract_cookies($res);
&dump($req,$res);
#
# Actual Form Request
#
$req = HTTP::Request->new(POST =>
'https://lending.etrade.com/e/t/mortgage/expressratequote');
$req->content_type('application/x-www-form-urlencoded');
$req->content($curl->equery);
$req->referer('https://lending.etrade.com/e/t/mortgage/expressratequote');
$req->user_agent('Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)');
$cookie_jar->add_cookie_header($req);
$res = $ua->request($req);
# Grab the request object that generated the response
# (it could have redirected in which case the request object
# would be different than the one we created)
$req = $res->request();
$cookie_jar->extract_cookies($res);
&dump($req,$res);
print $res->content();
print "This is libwww-perl-$LWP::VERSION\n";
#
# dump sub for debugging
#
sub dump {
($req,$res) = @_;
print "\nREQUEST-HEADERS\n";
print $req->headers_as_string();
print "\nREQUEST-CONTENT\n";
print $req->content;
if ($res->is_success) {
print "\nRESPONSE-HEADERS\n";
print $res->headers_as_string();
print "\nRESPONSE-CONTENT\n";
#print $res->content;
} else {
print "\nRESPONSE-ERROR\n";
print $res->error_as_HTML();
}
}
------------------------------
Date: 1 Jul 2004 09:53:22 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: My 1st question about locking (and other related issues)
Message-Id: <cc0mui$svn$1@mamenchi.zrz.TU-Berlin.DE>
Michele Dondi <bik.mido@tiscalinet.it> wrote in comp.lang.perl.misc:
> I have a *working* .sig rotation script that begins like this:
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use POSIX;
>
> $|++;
> setsid;
>
> # ...
> # Do some administrative things like
> # redirecting STDIN STDOUT and STDERR
> # ...
>
> fork and exit;
>
>
> then it starts an infinite loop and writes to a fifo (creating it if
> it doesn't exist).
>
> Now I'd like to use some mechanism to prevent more than one copy of
> the program to run at the same time on the same machine. The named
> pipe is in my home, mounted on nfs.
>
> I've thought of using a lockfile, but then since I can be logged at
> the same time on different machines, I *think* I should write in it
> the hostname, possibly along with other info.
I wouldn't write the hostname in the lockfile but name the lockfile
after the host. In fact, you don't need explicit file locking,
the existence of the lockfile can be used. The usual problem with
this approach is to reliably remove the lockfile when the program
terminates. %SIG handlers and an END block cover most of that.
use Sys::Hostname;
use Fcntl;
my $lockdir = "$ENV{ HOME}/lockfiles";
my $host = hostname;
my $lockfile = "$lockdir/$host";
sysopen my $lock, $lockfile, O_CREAT | O_EXCL, or
die "$0 already running on $host";
my $havelock = 1;
END { unlink $lockfile if $havelock }
$SIG{ $_} = sub { unlink $lockfile if $havelock } for
qw( INT TERM); # add more signals if needed
# rest of script
# ...
This ignores possible problems with NFS and atomic file operations.
Anno
------------------------------
Date: Thu, 01 Jul 2004 04:22:22 GMT
From: "Jeff Thies" <nospam@nospam.net>
Subject: split crazy
Message-Id: <2kMEc.3301$yy1.1231@newsread2.news.atl.earthlink.net>
I have this:
my $s='xxxxxxxx';
my @split= /x/,$s;
I thought I would get: ('','','','','','','')
But I get: ()
How do I get what I intended? I've tried a lot of things that don't work!
Cheers,
Jeff
------------------------------
Date: 1 Jul 2004 04:33:02 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: split crazy
Message-Id: <slrnce74vt.rlk.sholden@flexal.cs.usyd.edu.au>
On Thu, 01 Jul 2004 04:22:22 GMT, Jeff Thies <nospam@nospam.net> wrote:
> I have this:
>
> my $s='xxxxxxxx';
>
> my @split= /x/,$s;
I assume you mean:
my @s = split /x/, $s;
>
> I thought I would get: ('','','','','','','')
>
> But I get: ()
>
> How do I get what I intended? I've tried a lot of things that don't work!
But you didn't try reading the documentation?
Surely that should be the first thing you try when something doesn't work
as you expect.
perldoc -f split:
split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split Splits the string EXPR into a list of strings and returns that
list. By default, empty leading fields are preserved, and empty
trailing ones are deleted.
Since all your fields are empty, that deletion of empty trailing fields
would delete all the fields.
Note, the phrase "By default", usually that indicates you can change the
behaviour, so reading further we find:
If LIMIT is unspecified or zero, trailing null fields are
stripped (which potential users of "pop" would do well to remember).
If LIMIT is negative, it is treated as if an arbitrarily large LIMIT
had been specified.
And there a mere three paragraphs further down is the answer.
--
Sam Holden
------------------------------
Date: Wed, 30 Jun 2004 22:16:35 -0700
From: "Trent Curry" <trentcurryREM0VE@hotmail.com>
Subject: Re: Totally stuck
Message-Id: <cc06n4$id0$1@news.astound.net>
thundergnat wrote:
> Andrew Lee wrote:
>> On 29 Jun 2004 00:32:36 -0700, darnold@northcoast.com (David Arnold)
>> wrote:
[..]
>>> # replace \backtomargin with instructions environment
>>> my @instructions;
>>> my $instruct_line;
>>> while (my $line=<>) {
>>> chomp($line);
>>> if ($line=~/^\s*\\backtomargin/) {
>>
>>
>> But there is no space before "\backtomargin" ... so the following
>> lines never get executed.
>>
>
> I don't know... It looks to me like there is zero or more spaces
> before '\backtomargin'.
Correct, ^\s* optionally allows for any whitespace padding before
'\backtomargin'. The person you were quoting, Andrew Lee, was mistaken
:) To that person, I at least recommand O'Reilly's infinately useful
"Perl Pocket Reference, easily avaiable at any good book or computer
store.
> Actually, I think the logic problem is the lines:
>
> > while($line=pop(@instructions)) {
>
> and
> > while($line=shift(@instructions)) {
>
>
> Since the first line that gets popped off of the array is a blank
> line, the while() short circuits and the code block never gets
> executed.
Seems to me both of those while's should begin like this: while(defined
$line=...)
if nothing can be pop'ed ot shift'ed off anymroe, undef will be returned
instead, and THEN the loop condition will fail, which sems to be what
was desired, and not if the line was empty, but if something was
returned (aka testing if there is anything left in the array.)
--
Trent Curry - trentcurryREM0VE@hotmail.com
perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'
------------------------------
Date: Wed, 30 Jun 2004 22:31:53 -0700
From: "187" <bigal187.invalid@adexec.com>
Subject: Re: Totally stuck
Message-Id: <2khls2F2eq4qU1@uni-berlin.de>
Michele Dondi wrote:
> On 29 Jun 2004 00:32:36 -0700, darnold@northcoast.com (David Arnold)
> wrote:
> I was under the impression that your program was overly complex for
> the task it was aimed at. OTOH I was too lazy to try to understand
> *exactly* what you meant to do, so from your other post I grabbed the
> "working version" and I tried it on your sample. Now, if I'm not
> mistaken, the following should serve your needs and is considerably
> simpler:
>
> #!/usr/bin/perl -ln
>
> print, next unless /\\backtomargin/;
I dont udnerstand whats going on here. I thought 'next' can only be used
in loops, but here you're tryign to print something? Very confusing,
could you please explain what exactly is happening here?
> {
> local $/='';
> chomp(my $par=<>);
> print <<EOT;
And I don't see how you've opened the file. I thought <> read from a
file passed though STDIN, but you say below "no need for '<'" but
withotu that you're just passing "new" to new.pl, and not the file
contends, or am I missing something here..? Thanks for any explainations
you can give, aas I'm quite confused :(
> \\begin{instructions}
> $par
> \\end{instructions}
>
> EOT
> }
>
> __END__
>
>
>> When I enter:
>>
>> perl new.pl < new
>
> BTW: no need for '<',
>
> perl new.pl new
>
> will work!
------------------------------
Date: 1 Jul 2004 06:08:12 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: Totally stuck
Message-Id: <slrnce7aic.sof.sholden@flexal.cs.usyd.edu.au>
On Wed, 30 Jun 2004 22:31:53 -0700, bigal187.invalid@adexec.com wrote:
> Michele Dondi wrote:
>> On 29 Jun 2004 00:32:36 -0700, darnold@northcoast.com (David Arnold)
>> wrote:
>
>> I was under the impression that your program was overly complex for
>> the task it was aimed at. OTOH I was too lazy to try to understand
>> *exactly* what you meant to do, so from your other post I grabbed the
>> "working version" and I tried it on your sample. Now, if I'm not
>> mistaken, the following should serve your needs and is considerably
>> simpler:
>>
>> #!/usr/bin/perl -ln
>>
>> print, next unless /\\backtomargin/;
>
> I dont udnerstand whats going on here. I thought 'next' can only be used
> in loops, but here you're tryign to print something? Very confusing,
> could you please explain what exactly is happening here?
perldoc perlrun
And see the description of what the -n command line option that
is being passed to perl (via the -ln in the #! line) does.
>
>> {
>> local $/='';
>> chomp(my $par=<>);
>> print <<EOT;
>
> And I don't see how you've opened the file. I thought <> read from a
> file passed though STDIN, but you say below "no need for '<'" but
> withotu that you're just passing "new" to new.pl, and not the file
> contends, or am I missing something here..? Thanks for any explainations
> you can give, aas I'm quite confused :(
perldoc perlvar
And see the description of ARGV.
<> is equivalent to <ARGV> which manages the opening of files specified
on the command line.
--
Sam Holden
------------------------------
Date: Thu, 01 Jul 2004 16:38:17 +0900
From: ko <kuujinbo@hotmail.com>
Subject: Re: Why can't I get WWW::Mechanize->find_all_links to work?
Message-Id: <2khtbeF2bknmU1@uni-berlin.de>
Peter M. Jagielski wrote:
>>>Neither...the URL you're passing are valid.
>
> I know, but if I get rid of the space between "smith," and "john", the
> URL (ie. my "query") returns an empty dataset.
No one's suggesting you get rid of the space.
URL's are restricted to a certain set of characters. Any characters not
in that set, including the space, must be escaped if they are present in
a URL. Someone else in the thread already suggested using a regular
expression to do this. Just did a quick look through the source code,
and wouldn't you know - WWW::Mechanize 'use's URI::URL to escape invalid
characters for you. So you can keep the space in the string you assign
to the $URL variable.
If you're interested:
http://search.cpan.org/~gaas/URI-1.31/URI/Escape.pm
gives a quick explanation of the restricted characters and references to
the RFCs that define the standards.
HTH - keith
------------------------------
Date: Thu, 1 Jul 2004 15:05:57 +0800
From: "Koms Bomb" <komsbomb@hotmail.com>
Subject: Why can't I upload file in my this CGI script?
Message-Id: <2khracF2dr18U1@uni-berlin.de>
Environment: Win2000 professional, Apache 2.0, Perl 5.8, CGI.pm
I write and test my script on Win2K but I hope my script will be platform
independent.
The code is,
#!d:/perl/bin/perl.exe
use strict;
use warnings;
use CGI;
use MIME::Lite;
use File::Copy;
[snip]
my $fn = $cgi->param('screenshot'); #here $fn is the path of my client side!
if(defined $fn) {
$fn =~ s/.*[\/\\](.*)/$1/;
my $fh = $cgi->upload('screenshot');
binmode($fh);
copy($fh, './' . $fn);
print $fn;
#attach the file to mail message.
$msg->attach(Type =>'image/gif',
Path =>$fn,
Filename =>'./' . $fn
);
}
'screenshot' is the field name of fileupload, in html,
<td><input type="file" name="screenshot"
enctype='multipart/form-data'></td>.
I saw $fn is my client side file name, eg, if I select e:\temp\x.zip,
$fn is e:\temp\x.zip, not relate to any Apache environment. That is
obviously wrong.
My script works find except I can't upload file.
I've googled sample code for file uploading, but they can't work on my
system too.
I'm not good at Perl, and I don't know whether the error is due to my script
or
due to Apache/Win2K.
Any suggestion or anyone can give some sample code that can work on
Apache/Win2K ?
--
Koms Bomb
*****Pardon my poor English*****
---------------------
My TASM homepage, resource for assembly. Tools, articles, links.
http://komsbomb.ols-lab.com
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6754
***************************************