[26134] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8326 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 16 18:05:39 2005

Date: Tue, 16 Aug 2005 15:05:06 -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           Tue, 16 Aug 2005     Volume: 10 Number: 8326

Today's topics:
    Re: Image::GD::Thumbnail <zentara@highstream.net>
        LWP gives 302 Found after update? <john@castleamber.com>
    Re: LWP gives 302 Found after update? <no@email.com>
    Re: LWP gives 302 Found after update? <glex_no-spam@qwest-spam-no.invalid>
    Re: LWP gives 302 Found after update? <john@castleamber.com>
    Re: LWP gives 302 Found after update? <no@email.com>
    Re: LWP gives 302 Found after update? <glex_no-spam@qwest-spam-no.invalid>
        lwp package head question <xyz@tnecul.moc.invalid>
    Re: lwp package head question <thepoet_nospam@arcor.de>
        Problem with Curses <babacio@free.fr>
        Uri Guttman -> Where are you boy->Group is Missing you  <Harrison.Bhan@perrot.com>
    Re: Very basic question on using references inside func <Mark.Seger@hp.com>
    Re: Very basic question on using references inside func xhoster@gmail.com
    Re: Very basic question on using references inside func (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 16 Aug 2005 08:23:40 -0400
From: zentara <zentara@highstream.net>
Subject: Re: Image::GD::Thumbnail
Message-Id: <qcm3g15cuu2v1m68vdqfjhr7nlitp7slk1@4ax.com>

On Tue, 16 Aug 2005 10:01:29 +0200, Alexandre Jaquet <""alexjaquet\"@[no
spam]msn.com"> wrote:

>sub createTumb {
>
>     local our  $filename = shift || '';
>     open (IN, "<$dir/upload/$filename")  or die "Could not open 
>$dir/upload/$filename";
>     local our  $srcImage = GD::Image->newFromJpeg(*IN);
>     close IN;
>     local our  ($thumb,$x,$y) = Image::GD::Thumbnail::create($srcImage,50);
>     open (OUT,">>$dir/upload/thumb.$filename") or die "Could not save ";
>     binmode OUT;
>     print OUT $thumb->jpeg;
>     close OUT;
>}
>
>But I want to optimize the image by using GIF or PNG format, does anyone 
>have already used this module and know how to use GIF format ?
>
>Image size are to heavy with JPG format ...

Try  
print OUT $thumb->png;
or
print OUT $thumb->gif;

but your GD libs must have been built with png and\or gif support

Make sure you change your filename to reflect the new filetype, or
image viewers will choke on the misnamed files.




-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html


------------------------------

Date: 16 Aug 2005 18:05:50 GMT
From: John Bokma <john@castleamber.com>
Subject: LWP gives 302 Found after update?
Message-Id: <Xns96B4853877CDcastleamber@130.133.1.4>

The following script used to work (Logs in to a PHPbb message board):

use strict;
use warnings;

use LWP::UserAgent;
use LWP::Debug qw(+);

my $ua = LWP::UserAgent->new();

my $response = $ua->post(

    "http://toxicice.com/login.php", [

        username => 'xxxxxxx',
        password => 'xxxx',
        autlogin => 'off',
        redirect => '',
        login    => 'Log in',
    ]
);
$response->is_success or
    die "Login failed: ", $response->status_line, "\n";

With an invalid username/password (as above), it gives:

LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: POST http://toxici
LWP::UserAgent::_need_proxy: Not proxied
LWP::Protocol::http::request: ()
LWP::Protocol::collect: read 398 bytes

 ... snipped ...

LWP::Protocol::collect: read 188 bytes
LWP::UserAgent::request: Simple response: OK

However, with a valid one it gives:

LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: POST http://toxicice.com/login.php
LWP::UserAgent::_need_proxy: Not proxied
LWP::Protocol::http::request: ()
LWP::UserAgent::request: Simple response: Found
Login failed: 302 Found

$response->content is empty ('').

I updated some time ago to a more recent version of ActiveState Perl, 
and probably LWP was upgraded as well. OTOH it might be a server thing.

perl -v
 ...
This is perl, v5.8.7 built for MSWin32-x86-multi-thread
 ...

query *
 ...
libwww-perl          [5.803.0.1] Web API for Perl
 ...

(Complete script is at:
http://johnbokma.com/perl/phpbb-remote-backup.html )

( If you want to test but have no PHP board, mail: phpbb at johnbokma 
dot com, and I arrange a test log in, don't create a test login 
yourself, thanks )

-- 
John                   Small Perl scripts: http://johnbokma.com/perl/
               Perl programmer available:     http://castleamber.com/
            Happy Customers: http://castleamber.com/testimonials.html
                        


------------------------------

Date: Tue, 16 Aug 2005 19:43:37 +0100
From: Brian Wakem <no@email.com>
Subject: Re: LWP gives 302 Found after update?
Message-Id: <3meqepF16hsvrU1@individual.net>

John Bokma wrote:

> Login failed: 302 Found
> 
> $response->content is empty ('').


The page is printing a Location: header, which tells the browser to go
somewhere else.  LWP::UserAgent does not follow this by default for POSTs.

Add this:-

push @{ $ua->requests_redirectable }, 'POST';


-- 
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png


------------------------------

Date: Tue, 16 Aug 2005 13:48:33 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: LWP gives 302 Found after update?
Message-Id: <6yqMe.49$l4.10972@news.uswest.net>

John Bokma wrote:
> The following script used to work (Logs in to a PHPbb message board):
> 
> use strict;
> use warnings;
> 
> use LWP::UserAgent;
> use LWP::Debug qw(+);
> 
> my $ua = LWP::UserAgent->new();
> 
> my $response = $ua->post(
> 
>     "http://toxicice.com/login.php", [
> 
>         username => 'xxxxxxx',
>         password => 'xxxx',
>         autlogin => 'off',
>         redirect => '',
>         login    => 'Log in',
>     ]
> );
> $response->is_success or
>     die "Login failed: ", $response->status_line, "\n";

> However, with a valid one it gives:
> 
> LWP::UserAgent::new: ()
> LWP::UserAgent::request: ()
> LWP::UserAgent::send_request: POST http://toxicice.com/login.php
> LWP::UserAgent::_need_proxy: Not proxied
> LWP::Protocol::http::request: ()
> LWP::UserAgent::request: Simple response: Found
> Login failed: 302 Found

Just a quick guess...  Dumping $ua:

$VAR1 = bless( {
                  'max_redirect' => 7,
                  'protocols_forbidden' => undef,
                  'no_proxy' => [],
                  'protocols_allowed' => undef,
                  'use_eval' => 1,
                  'requests_redirectable' => [
                                               'GET',
                                               'HEAD'
                                             ],
                  'from' => undef,
                  'timeout' => 180,
                  'agent' => 'libwww-perl/5.803',
                  'def_headers' => undef,
                  'parse_head' => 1,
                  'proxy' => {},
                  'max_size' => undef
                }, 'LWP::UserAgent' );

Since a 302 means a redirect is being requested, and 
'requests_redirectable' only contain GET and HEAD requests, possibly the 
POST isn't seen as being redirecable. Maybe adding 'POST' to that 
attribute, or possibly doing a GET will resolve it.

WWW::Mechanize might provide a better interface, for interacting with 
the site.


------------------------------

Date: 16 Aug 2005 18:50:52 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: LWP gives 302 Found after update?
Message-Id: <Xns96B48CDA58F35castleamber@130.133.1.4>

Brian Wakem <no@email.com> wrote:

> John Bokma wrote:
> 
>> Login failed: 302 Found
>> 
>> $response->content is empty ('').
> 
> 
> The page is printing a Location: header, which tells the browser to go
> somewhere else.  LWP::UserAgent does not follow this by default for
> POSTs. 
> 
> Add this:-
> 
> push @{ $ua->requests_redirectable }, 'POST';

Aargh, it was even in the manual :-( Many thanks, it fixed my script. I 
have no idea however, why it started to fail in the first place. Was POST 
removed from the list recently? (I can't remember I updated PHPbb 
recently).

-- 
John                   Small Perl scripts: http://johnbokma.com/perl/
               Perl programmer available:     http://castleamber.com/
            Happy Customers: http://castleamber.com/testimonials.html
                        


------------------------------

Date: Tue, 16 Aug 2005 19:56:55 +0100
From: Brian Wakem <no@email.com>
Subject: Re: LWP gives 302 Found after update?
Message-Id: <3mer7nF164pmaU1@individual.net>

John Bokma wrote:

> Aargh, it was even in the manual :-( Many thanks, it fixed my script. I
> have no idea however, why it started to fail in the first place. Was POST
> removed from the list recently? (I can't remember I updated PHPbb
> recently).


I don't recall POST ever being in that redirectable array.  I've only been
using Perl for 5yrs though.


-- 
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png


------------------------------

Date: Tue, 16 Aug 2005 14:35:20 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: LWP gives 302 Found after update?
Message-Id: <ZdrMe.30$Xh6.1585@news.uswest.net>

Brian Wakem wrote:
> John Bokma wrote:
> 
> 
>>Aargh, it was even in the manual :-( Many thanks, it fixed my script. I
>>have no idea however, why it started to fail in the first place. Was POST
>>removed from the list recently? (I can't remember I updated PHPbb
>>recently).

More likely is that the Web site changed something.

> 
> I don't recall POST ever being in that redirectable array.  I've only been
> using Perl for 5yrs though.

Much older versions used redirect_ok(), which was:

sub redirect_ok
{
     # draft-ietf-http-v10-spec-02.ps from www.ics.uci.edu, specify:
     #
     # If the 30[12] status code is received in response to a request using
     # the POST method, the user agent must not automatically redirect the
     # request unless it can be confirmed by the user, since this might 
change
     # the conditions under which the request was issued.

     my($self, $request) = @_;
     return 0 if $request->method eq "POST";
     1;
}


------------------------------

Date: Tue, 16 Aug 2005 10:47:32 -0400
From: "T W Hu" <xyz@tnecul.moc.invalid>
Subject: lwp package head question
Message-Id: <ddsua4$g2n@netnews.net.lucent.com>

When I do
HEAD www.tmn.pt
it tells me 404 Not found.
But when I do
GET www.tmn.pt
it gives me the page.

Is this a bug in the lwp package or web server setup problem?

more info:
HEAD www.tmn.pt/index.shtml
200 OK
HEAD www.tmn.pt/default.shtml
200 OK
HEAD www.tmn.pt/index.html
404 not found




------------------------------

Date: Tue, 16 Aug 2005 17:46:31 +0200
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: lwp package head question
Message-Id: <43020a57$0$11754$9b4e6d93@newsread4.arcor-online.net>

T W Hu wrote:
> When I do
> HEAD www.tmn.pt
> it tells me 404 Not found.
> But when I do
> GET www.tmn.pt
> it gives me the page.
> 
> Is this a bug in the lwp package or web server setup problem?
[....]

Obviously a server thing:

myclient:~ # telnet www.tmn.pt 80
Trying 194.65.141.2...
Connected to www.tmn.pt.
Escape character is '^]'.
HEAD / HTTP/1.1
Host: www.tmn.pt

HTTP/1.1 404 Not found
Server: Netscape-Enterprise/4.0
Date: Tue, 16 Aug 2005 15:37:16 GMT
Set-Cookie: TMN=217.160.174.213-1124206636.927625; path=/; expires=Sun,
1-Jan-2012 23:59:59 GMT
Content-type: text/html

HTH
-Chris


------------------------------

Date: Tue, 16 Aug 2005 14:00:54 +0200
From: Babacio <babacio@free.fr>
Subject: Problem with Curses
Message-Id: <m2r7cu9jm1.fsf@baba.ba>


Hi,

Sorry if the question looks stupid to some of you...

Here is a piece of code:
####################################
use Curses;

initscr();
# here I should use curses
endwin();

print "Hello!\n";
print "What do you say? ";
$x=<STDIN>;
print "You said $x\n";
#####################################

The functions initscr() and endwin() are to be use before and after
doing stuff with curses...

When I run it (on Mac OS X / darwin), I have the following problem :
the text of the three prints does not appear until the end of the
program, so I enter the value of $x with nothing printed, and
after that the three line appear.

This is due to some pertubation Curses makes in the terminal.

This second piece of code sort of give a solution to this problem:

####################################
use Curses;
use IO::Handle;

initscr();
# here I should use curses
endwin();

STDOUT->autoflush(1);  # or $|=1

print "Hello!\n";
print "What do you say? ";
$x=<STDIN>;
print "\nYou said $x\n";
#####################################

Forcing STDOUT to flush contiuously, the behaviour is (seems to be)
what is expected. 

But it is obvious that this in not the right way to solve the
problem. The vale of $| should remain 0.

Any idea?


------------------------------

Date: Tue, 16 Aug 2005 14:53:58 GMT
From: "Harrison Bhan" <Harrison.Bhan@perrot.com>
Subject: Uri Guttman -> Where are you boy->Group is Missing you ?
Message-Id: <xn0e62k441vk2lh000@news.europe.nokia.com>

-


------------------------------

Date: Tue, 16 Aug 2005 09:34:09 -0400
From: Mark Seger <Mark.Seger@hp.com>
Subject: Re: Very basic question on using references inside functions
Message-Id: <4301eb52$1@usenet01.boi.hp.com>

Anno Siegel wrote:
> Mark Seger  <Mark.Seger@hp.com> wrote in comp.lang.perl.misc:
> 
>>>Perl *does* pass scalars "by reference" in the sense that a parameter
>>>value can be changed from within the function.  Example:
>>>
>>>    sub foe {
>>>        $_[ 0] = 'new value';
>>>    }
>>>    my $x = 'old value';
>>>    fum( $x);
>>>    print "$x\n";
>>>
>>
>>What I meant by 'pass by reference' is that any changes you make in the 
>>function are reflected back at caller who will then see changes to that 
>>parameter.  I believe what perl does is 'pass by value', in that it 
>>makes a copy of the data, thereby allowing you to change it without fear 
>>of clobbering the orignal value.
> 
> 
> You believe wrong.  The code example above shows that changes to sub
> arguments (the elements of @_) are indeed visible to the caller.  The
> "call-by-value" thing happens when you assign the content of @_ to
> local variables, as in "my ( $x, $y, $z) = @_;".  If you change $x etc.
> in the sub body, nothing is changed in the environment.  If you change
> $_[ 0] etc. directly, it is.

yikes!  I was indeed wrong.  I guess my habit of ALWAYS saying
   my $xyz=shift
for each parameter has protected me in my ignorance.  8-)

but from yours and other notes I think I'll be a lot more comfortable 
with passing references around now.

-mark


------------------------------

Date: 16 Aug 2005 16:13:08 GMT
From: xhoster@gmail.com
Subject: Re: Very basic question on using references inside functions
Message-Id: <20050816121308.941$ZN@newsreader.com>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>  <xhoster@gmail.com> wrote in comp.lang.perl.misc:
> > anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> > > >
> > > > sub foo3 {
> > > >   our $y;
> > > >   *y = \$_[0];
> > > >   $y =~ s/j/k/g;
> > > > };
> > >
> > > Yes, but why?
> >
> > The point of a short but complete example script (like people here are
> > always asking for) is to show how something is done.  It is not to show
> > why it is done.  The why is covered in the prose.  I thought it was
> > pretty obvious why to do that.
>
> You say "in a pinch", but I don't see the pinch that makes a solution
> involving package variables attractive.

It wasn't attractive.  Just better than the alternatives I could think of.
We desperately needed to refactor the code.  But we even more desperately
needed an answer which couldn't wait until the refactoring was done.

>
> > >     sub foo4 { $_[ 0] =~ s/j/k/g }
> >
> > unlike $_[0], the name of $y can be arbitrarily chosen.  If you are
> > going to use the variable fifty times in your subroutine, that is quite
> > a benefit.
>
> You can have that lexically too:
>
>     for my $y ( shift ) {
>         $y =~ s/j/k/g;
>     }
>
> (Admitted, it doesn't scale well for more than one variable.)

And that was exactly the pinch that spurred me to use typeglobs[1].

Although I guess I could have used

foreach my $x (shift) {
foreach my $y (shift) {
foreach my $z (shift) {
foreach my $w (shift) {
foreach my $q (shift) {
 ....
}}}}}

Which is not that much uglier than the typeglobs, and definitely cleaner
from a "symbolic reference like things are evil" POV.  (BTW, before someone
asks, I didn't indent the above on purpose.  Since I want all the foreachs
together to act essentially as one atomic aliasing operation, I want them
all indented together.  I would have put them all on one line, but that
would be too long.  Besides, if I am using foreach to do something out of
the ordinary, it deserves to look out of the ordinary.)


[1] Ok, maybe not exactly.  Looking back at that actual code, there was one
more factor involved.

my @x=1..20;
print "@x\n";
foo6($foo,$bar,\@x,$baz,$and,\@many,$more);
print "@x\n";

sub foo6 {
  our @fooarray;
  *fooarray=$_[2];
  @fooarray=reverse @fooarray; # legacy code.
};

Where the 'reverse' line is standing in for >1500 lines that use @fooarray
several hundred times, plus several similarly situated other variables.

That one I still don't know a quick non-typeglob solution to (other than
refusing to take over crappy code from other people.  But then they would
probably refuse to pay me, which would make me sad.)

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: 16 Aug 2005 18:17:27 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Very basic question on using references inside functions
Message-Id: <ddtajn$lia$1@mamenchi.zrz.TU-Berlin.DE>

 <xhoster@gmail.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> >  <xhoster@gmail.com> wrote in comp.lang.perl.misc:
> > > anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> > > > >
> > > > > sub foo3 {
> > > > >   our $y;
> > > > >   *y = \$_[0];
> > > > >   $y =~ s/j/k/g;
> > > > > };
> > > >
> > > > Yes, but why?

[...]

> > You can have that lexically too:
> >
> >     for my $y ( shift ) {
> >         $y =~ s/j/k/g;
> >     }
> >
> > (Admitted, it doesn't scale well for more than one variable.)
> 
> And that was exactly the pinch that spurred me to use typeglobs[1].

Ah, I see.

> Although I guess I could have used
> 
> foreach my $x (shift) {
> foreach my $y (shift) {
> foreach my $z (shift) {
> foreach my $w (shift) {
> foreach my $q (shift) {
> ....
> }}}}}
> 
> Which is not that much uglier than the typeglobs, and definitely cleaner
> from a "symbolic reference like things are evil" POV.  (BTW, before someone
          ^^^^^^^^^^^^^^^^^^
Just package variables.  I don't think they are evil, sometimes their
program-wide scope is just what is needed.  Still the rule is that scopes
should be as small as possible.

> asks, I didn't indent the above on purpose.  Since I want all the foreachs
> together to act essentially as one atomic aliasing operation, I want them
> all indented together.  I would have put them all on one line, but that
> would be too long.  Besides, if I am using foreach to do something out of
> the ordinary, it deserves to look out of the ordinary.)

It's a perfectly reasonable way to write this bit of code, imo.  The nesting
of blocks that indentation would draw attention to is an artifact, we have
no other way to express this in Perl.  If there was syntax for walking
through multiple lists in parallel, we'd use that with a single level.

> [1] Ok, maybe not exactly.  Looking back at that actual code, there was one
> more factor involved.
> 
> my @x=1..20;
> print "@x\n";
> foo6($foo,$bar,\@x,$baz,$and,\@many,$more);
> print "@x\n";
> 
> sub foo6 {
>   our @fooarray;
>   *fooarray=$_[2];
>   @fooarray=reverse @fooarray; # legacy code.
> };
> 
> Where the 'reverse' line is standing in for >1500 lines that use @fooarray
> several hundred times, plus several similarly situated other variables.

 ...which used to be globals and had to be shoehorned into the parameter
list post factum, am I right?

> That one I still don't know a quick non-typeglob solution to (other than

One could tie @fooarray appropriately (Brian's Tie::OneOff would make that
a snap), but that might scare your co-workers.  Well, if they said it was
over-engineering they might be right.

With the typeglob solution, your variables are still globals, (just
controlled locally, which is also a gain), so you have come only part
of the way if elimination of globals was indeed the purpose.

After this discussion I'd use the nested for-loops where applicable,
particularly after you invented such a nice format for it.  About
those arrays (and similarly used hashes) I don't know.  I'd have
to code up a tie-solution to imagine how it looks in context, but
not now.

Anno
-- 
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on 
"show options" at the top of the article, then click on the 
"Reply" at the bottom of the article headers.


------------------------------

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 8326
***************************************


home help back first fref pref prev next nref lref last post