[30630] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1875 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 25 18:09:46 2008

Date: Thu, 25 Sep 2008 15:09:11 -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, 25 Sep 2008     Volume: 11 Number: 1875

Today's topics:
    Re: expect.pm - stdout buffering issue <josef.moellers@fujitsu-siemens.com>
    Re: expect.pm - stdout buffering issue <yandry77@gmail.com>
    Re: expect.pm - stdout buffering issue <glex_no-spam@qwest-spam-no.invalid>
        How to get the class & method name <sun_tong_001@users.sourceforge.net>
    Re: How to get the class & method name <joost@zeekat.nl>
    Re: Opening files on the web for reading <tzz@lifelogs.com>
    Re: Opening files on the web for reading xhoster@gmail.com
    Re: Opening files on the web for reading <tzz@lifelogs.com>
    Re: Opening files on the web for reading <ben@morrow.me.uk>
    Re: Opening files on the web for reading <ben@morrow.me.uk>
        parallel processing via system() command and logging ou <clancampbell@gmail.com>
    Re: parallel processing via system() command and loggin <jurgenex@hotmail.com>
    Re: Regular Expressions & replacements <cartercc@gmail.com>
    Re: Regular Expressions & replacements (Vicky Conlan)
    Re: Regular Expressions & replacements <cartercc@gmail.com>
    Re: Regular Expressions & replacements (Vicky Conlan)
    Re: Regular Expressions & replacements <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 25 Sep 2008 10:25:52 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: expect.pm - stdout buffering issue
Message-Id: <gbfhug$p5l$1@nntp.fujitsu-siemens.com>

Andry wrote:

> I already tried that and log_file still doesn't capture the last
> "ls" (it does it only if I put '$exp->log_file(undef)' after '$exp-
>> soft_close()').

Why should it? As Glenn points out: you fire the "ls" and then forget to 
pick up the answer.
Whenever you send a command, you *must* expect() somethign that follows, 
otherwise you won't see the response.

Personally I have resorted to this construct:

sub sendexp {
     my $exp = shift;
     my $cmd = join(' ', @_);
     $exp->send("$cmd\r");
     $exp->expect($prompt);
     # remove echoed command and prompt
     # split returned data into lines
     return @lines;
}

When starting up, I just have to do a single "$exp->expect(prompt)". 
After that, I just have calls to sendexp().

Josef
-- 
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html


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

Date: Thu, 25 Sep 2008 08:44:39 -0700 (PDT)
From: Andry <yandry77@gmail.com>
Subject: Re: expect.pm - stdout buffering issue
Message-Id: <8db3a66d-3f40-408f-b5f4-25f2f0808cf4@d45g2000hsc.googlegroups.com>

On Sep 25, 10:25=A0am, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
> Andry wrote:
> > I already tried that and log_file still doesn't capture the last
> > "ls" (it does it only if I put '$exp->log_file(undef)' after '$exp-
> >> soft_close()').
>
> Why should it? As Glenn points out: you fire the "ls" and then forget to
> pick up the answer.
> Whenever you send a command, you *must* expect() somethign that follows,
> otherwise you won't see the response.
>
> Personally I have resorted to this construct:
>
> sub sendexp {
> =A0 =A0 =A0my $exp =3D shift;
> =A0 =A0 =A0my $cmd =3D join(' ', @_);
> =A0 =A0 =A0$exp->send("$cmd\r");
> =A0 =A0 =A0$exp->expect($prompt);
> =A0 =A0 =A0# remove echoed command and prompt
> =A0 =A0 =A0# split returned data into lines
> =A0 =A0 =A0return @lines;
>
> }
>
> When starting up, I just have to do a single "$exp->expect(prompt)".
> After that, I just have calls to sendexp().
>
> Josef
> --
> These are my personal views and not those of Fujitsu Siemens Computers!
> Josef M=F6llers (Pinguinpfleger bei FSC)
> =A0 =A0 =A0 =A0 If failure had no penalty success would not be a prize (T=
 . =A0Pratchett)
> Company Details:http://www.fujitsu-siemens.com/imprint.html

Thanks Josef and Glenn,
I thought the last expect statement (the one with the sub doing
send(exit)) would do the job; instead another simple expect statement
needs to put in between and now STDOUT looks complete as well as the
log file.

Nevertheless, the 'sleep' commands still look out of sync. I'm
starting thinking that also some delayed answers from the ssh server
are somehow affecting the script behavior for I/O STDOUT operations
(and I'm curious to try the same thing with telnet instead).
Especially because today I also put some additional logging operations
in the middle of the script (teeing STDOUT). I'm gonna tell you about
that tomorrow and post an updated version of the script.

Thanks,
Andrea


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

Date: Thu, 25 Sep 2008 11:38:12 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: expect.pm - stdout buffering issue
Message-Id: <48dbbe75$0$89878$815e3792@news.qwest.net>

Andry wrote:
[...]
> Nevertheless, the 'sleep' commands still look out of sync. I'm
> starting thinking that also some delayed answers from the ssh server

No and there's no such thing as an ssh server.

You're script is basically doing:

print "Running ls\n";
#send()
my $out = `ls`;

print "sleeping for 5\n";
sleep 5;

#expect(...)
print "the output of ls: $out\n" if $out;

Expect will run the command, then you tell it what you
expect to happen and/or when that command is finished,
usually by testing if the prompt is being displayed.
At that point your command has finished running and
you can get the output by using before(), or other
methods.  Then you can sleep, or send another command
and wait for the prompt. Rinse and repeat...


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

Date: Thu, 25 Sep 2008 15:58:06 -0500
From: * Tong * <sun_tong_001@users.sourceforge.net>
Subject: How to get the class & method name
Message-Id: <Hfidnc7lMK_DZkbVnZ2dnUVZ_oWdnZ2d@golden.net>

Hi, 

quick question, 

Any way to get the current class & method name from within an object 
method?

Especially, is there any way to do so in Class::Std build classes? 

Thanks

tong



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

Date: Thu, 25 Sep 2008 23:07:17 +0200
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: How to get the class & method name
Message-Id: <87abdw7x4q.fsf@zeekat.nl>

* Tong * <sun_tong_001@users.sourceforge.net> writes:

> Hi, 
>
> quick question, 
>
> Any way to get the current class & method name from within an object 
> method?

yes.

sub whatever {
  my $class = __PACKAGE__;  # if you mean, the class the method was
                            # compiled into.

  my $class = shift;               # if you mean, the class the current object
  $class = ref($class) || $class;  # is an instance of

  my ($class,undef,undef,$subname) = caller(0);  # find the current sub name and
                                                 # package from the stack
}



-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Thu, 25 Sep 2008 08:39:22 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Opening files on the web for reading
Message-Id: <86zllw8hv9.fsf@lifelogs.com>

On Wed, 24 Sep 2008 18:25:48 +0100 Ben Morrow <ben@morrow.me.uk> wrote: 

BM> Quoth xhoster@gmail.com:
>> Jürgen Exner <jurgenex@hotmail.com> wrote:
>> > "Graham Stow" <graham.stow@gmail.com> wrote:
>> > >Can anyone give me some Perl code to open an html file on the web (i.e.
>> > >an html file stored on somebody elses web server and not mine), for
>> > >reading. Or is it more complicated than that?
>> >
>> > Is there anything wrong with the answer in "perldoc -q HTML":
>> >
>> >          How do I fetch an HTML file?
>> 
>> Other than it not answering the question?  At least on my Perl version,
>> none of the answers there return a file handle opened for reading.  Now
>> maybe he is fine with downloading the entire file (either to disk or to
>> memory) and then reading from that, but I'd be inclined to give the benefit
>> of the doubt that he meant what he asked.
>> 
>> LWP::UserAgent using a callback with for example :content_cb would "stream"
>> the data back, but not via a file handle.  One could probably come up with
>> an adaptor that ties a file handle front end to the callback backend.
>> 
>> There might be a more direct way, but I don't know what it is.

BM> IO::All::LWP

Unfortunately, the docs say "The bad news is that the whole file is
stored in memory after getting it or before putting it. This may cause
problems if you are dealing with multi-gigabyte files!"

It would be nice to have a buffered reader/writer which wouldn't grab
the whole file, using the LWP callbacks, as xhoster suggests...  I
haven't seen such a module.

Ted


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

Date: 25 Sep 2008 15:23:24 GMT
From: xhoster@gmail.com
Subject: Re: Opening files on the web for reading
Message-Id: <20080925112326.156$ax@newsreader.com>

Ted Zlatanov <tzz@lifelogs.com> wrote:
> On Wed, 24 Sep 2008 18:25:48 +0100 Ben Morrow <ben@morrow.me.uk> wrote:
>
> BM> IO::All::LWP
>
> Unfortunately, the docs say "The bad news is that the whole file is
> stored in memory after getting it or before putting it. This may cause
> problems if you are dealing with multi-gigabyte files!"
>
> It would be nice to have a buffered reader/writer which wouldn't grab
> the whole file, using the LWP callbacks, as xhoster suggests...  I
> haven't seen such a module.

And it doesn't seem as easy as I thought.  In order for the callback to be
invoked, the thing invoking the callback has to be "in control".  But to
read from a file handle, the thing reading is in control.  You'd have to
fork a process and in one have the callback invoker in control, streaming
data to the other process as it comes in and the callback is invoked.  So
then you would have portability problems.

It seems like it is easy to write a wrapper that turns an iterator into a
callback, but vice versa is not easy.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: Thu, 25 Sep 2008 14:28:26 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Opening files on the web for reading
Message-Id: <86ljxg81ph.fsf@lifelogs.com>

On 25 Sep 2008 15:23:24 GMT xhoster@gmail.com wrote: 

x> Ted Zlatanov <tzz@lifelogs.com> wrote:
>> It would be nice to have a buffered reader/writer which wouldn't grab
>> the whole file, using the LWP callbacks, as xhoster suggests...  I
>> haven't seen such a module.

x> And it doesn't seem as easy as I thought.  In order for the callback to be
x> invoked, the thing invoking the callback has to be "in control".  But to
x> read from a file handle, the thing reading is in control.  You'd have to
x> fork a process and in one have the callback invoker in control, streaming
x> data to the other process as it comes in and the callback is invoked.  So
x> then you would have portability problems.

You can do it with buffering but it's ugly code I would not want to
write.  It's very easy to get it wrong.

x> It seems like it is easy to write a wrapper that turns an iterator into a
x> callback, but vice versa is not easy.

Right, since iterators are stateful, so you have to manufacture and
preserve the state when you only have a callback.

Ted


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

Date: Thu, 25 Sep 2008 20:48:13 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Opening files on the web for reading
Message-Id: <tg6tq5-ef8.ln1@osiris.mauzo.dyndns.org>


Quoth xhoster@gmail.com:
> Ted Zlatanov <tzz@lifelogs.com> wrote:
> > On Wed, 24 Sep 2008 18:25:48 +0100 Ben Morrow <ben@morrow.me.uk> wrote:
> >
> > BM> IO::All::LWP
> >
> > Unfortunately, the docs say "The bad news is that the whole file is
> > stored in memory after getting it or before putting it. This may cause
> > problems if you are dealing with multi-gigabyte files!"
> >
> > It would be nice to have a buffered reader/writer which wouldn't grab
> > the whole file, using the LWP callbacks, as xhoster suggests...  I
> > haven't seen such a module.
> 
> And it doesn't seem as easy as I thought.  In order for the callback to be
> invoked, the thing invoking the callback has to be "in control".  But to
> read from a file handle, the thing reading is in control. 

So use Net::HTTP::NB. Not quite as convenient as LWP::UA, but it
provides non-blocking reads.

It's a real shame Perl doesn't have a decent lightweight userland thread
library, as this sort of thing is exactly what it would be useful for.
If I *wanted* to write select loops, I'd be writing C; since I'm writing
Perl, it would be nice if perl could handle the messy stuff for me :).

Ben

-- 
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
                                                               ben@morrow.me.uk


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

Date: Thu, 25 Sep 2008 21:40:44 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Opening files on the web for reading
Message-Id: <cj9tq5-aln.ln1@osiris.mauzo.dyndns.org>


Quoth Ted Zlatanov <tzz@lifelogs.com>:
> On 25 Sep 2008 15:23:24 GMT xhoster@gmail.com wrote: 
> 
> x> It seems like it is easy to write a wrapper that turns an iterator into a
> x> callback, but vice versa is not easy.
> 
> Right, since iterators are stateful, so you have to manufacture and
> preserve the state when you only have a callback.

That's not the issue: callbacks in Perl are closures, so they do have
state. The trouble is that you would need LWP::UserAgent->simple_request
and whatever is driving the <$FH> loop to be coroutines, and Perl
doesn't have 'yield'.

Just for fun, here's an implementation using Coro:

#!/usr/bin/perl

use warnings;
use strict;

{
    package LWP::FH;

    use Coro;
    use Coro::Channel;
    use LWP::UserAgent;

    use overload '<>' => sub {
        my ($s) = @_;
        my $eol;
        until (($eol = length($/) + index $s->{buf}, $/) > 0) {
            my $new = $s->{ch}->get;
            if (defined $new) {
                $s->{buf} .= $new;
            }
            else {
                $eol = length $s->{buf};
                last;
            }
        }
        return substr $s->{buf}, 0, $eol, "";
    };

    my $UA = LWP::UserAgent->new;

    sub new {
        my ($c, $url) = @_;
        my $s = bless {
            buf => "",
            ch  => Coro::Channel->new(1),
        }, $c;
        async {
            my ($UA, $s) = @_;
            $UA->get(
                $url,
                ":content_cb" => sub {
                    $s->{ch}->put($_[0]);
                },
            );
            $s->{ch}->put(undef);
        } $UA, $s;
        return $s;
    }
}

my $FH = LWP::FH->new("http://perl.org");
while (<$FH>) {
    print "LINE: $_";
}

__END__

Ben

-- 
   If you put all the prophets,   |   You'd have so much more reason
   Mystics and saints             |   Than ever was born
   In one room together,          |   Out of all of the conflicts of time.
ben@morrow.me.uk                                    The Levellers, 'Believers'


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

Date: Thu, 25 Sep 2008 06:47:37 -0700 (PDT)
From: ClanCampbellChandler <clancampbell@gmail.com>
Subject: parallel processing via system() command and logging output
Message-Id: <3ab926e5-4614-4588-a73c-4eab562bbbef@x35g2000hsb.googlegroups.com>

Trying to do parallel clearcase operations over many VOBs in
parallel...code snippet:

foreach $vob (@VLIST) {

		chdir($vob);
                system("cleartool find -all -version 'lbtype($OLDIB)
&& !lbtype($NEWIB)' -exec \"cleartool mklabel -replace $NEWIB \\
\"%CLEARCASE_XPN%\\\"\" &");
}

This seems to work just fine as is, but I would prefer to log the
output rather than seeing stdout.  The current implementation allows
all the spawned processes to return stdout to screen in a mumbled
order.  I would simply like the individual processes to get logged to
unique logfiles....perhaps /tmp/$vob.txt.

Ideas?

Thanks,

CCC


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

Date: Thu, 25 Sep 2008 07:14:33 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: parallel processing via system() command and logging output
Message-Id: <nt6nd4hedvkii8mb4cms377o1kidhbe5qf@4ax.com>

ClanCampbellChandler <clancampbell@gmail.com> wrote:
[system()]
>I would prefer to log the
>output rather than seeing stdout.  The current implementation allows
>all the spawned processes to return stdout to screen in a mumbled
>order.  I would simply like the individual processes to get logged to
>unique logfiles....perhaps /tmp/$vob.txt.
>
>Ideas?

Yep. reading the manual for the functions you are using would help. See
'perldoc -f system',  third paragraph, fourth sentence:

            This is
            *not* what you want to use to capture the output from a
command,
            for that you should use merely backticks or "qx//", as
described
            in the section on "`STRING`" in the perlop manpage. 


Or just redirect the output into a file using the means provided by your
command shell.

jue


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

Date: Thu, 25 Sep 2008 06:27:57 -0700 (PDT)
From: cartercc <cartercc@gmail.com>
Subject: Re: Regular Expressions & replacements
Message-Id: <62e596b6-5f31-4ab8-9355-826b01202878@e39g2000hsf.googlegroups.com>

On Sep 24, 12:42=A0pm, jhgaylor <jhgay...@gmail.com> wrote:
> So, I am working on a program to take a list of First and Last names
> exported from Novell Groupwise and making the list become Last, First,
> firstfourlettersofLastName, password ||

No need to use a regular expression. substr() is more intuitive and
works just as well if not better.

my $value;
my @line;

while (<DATA>)
{
    next unless /^To/;
    @line =3D split;
    $value =3D substr($line[1], 0, 4);
    $value .=3D "0001\n";
    print $value;
}

exit(0);

__DATA__
Cof2001Members
To: LastName1, FirstName1
To: LastName2, FirstName2
To: LastName3, FirstName3
To: LastName4, FirstName4
To: Washington, George
To: Adams, John
To: Jefferson, Thomas
To: Madison, James
To: Monroe, James
Bc: Zzip


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

Date: Thu, 25 Sep 2008 16:00:47 +0000 (UTC)
From: comps@riffraff.plig.net (Vicky Conlan)
Subject: Re: Regular Expressions & replacements
Message-Id: <gbgcjf$16v1$1@magenta.plig.net>

According to <cartercc@gmail.com>:
>No need to use a regular expression. substr() is more intuitive and
>works just as well if not better.
>
>my $value;
>my @line;
>
>while (<DATA>)
>{
>    next unless /^To/;
>    @line = split;
>    $value = substr($line[1], 0, 4);
>    $value .= "0001\n";
>    print $value;
>}

I find it interesting that you consider substr more intuitive.  To me it
screams RE, but I guess the match is a fixed length, so yes, a substr
should be more logical.  Do you come from a C or Java background?

Then again, you /do/ use a RE for your next, so you don't avoid them anyhow.

If I get bored later I may have to benchmark them.  :-)
-- 


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

Date: Thu, 25 Sep 2008 09:12:47 -0700 (PDT)
From: cartercc <cartercc@gmail.com>
Subject: Re: Regular Expressions & replacements
Message-Id: <6d94ec83-695c-447f-a136-0614a5abb090@w7g2000hsa.googlegroups.com>

On Sep 25, 12:00=A0pm, co...@riffraff.plig.net (Vicky Conlan) wrote:
> I find it interesting that you consider substr more intuitive. =A0To me i=
t
> screams RE, but I guess the match is a fixed length, so yes, a substr
> should be more logical. =A0Do you come from a C or Java background?

My first language, circa 1979, was BASIC, GOTO's and all. After that,
I come from a web background, so I guess you would say that I got my
start with in modern languages with JavaScript. Since 2001, about
60%-70% of my programming has been in Perl. I've done a fair amount of
Java and a modest amount of C++, as well as some Python, ColdFusion,
Lisp, and Erlang.

>
> Then again, you /do/ use a RE for your next, so you don't avoid them anyh=
ow.

Don't get me wrong. My 'real' job is a database manager and I do a LOT
of reporting, so I use RE's all the time. After a long time, I came to
the conclusion that using the built in Perl functions were quicker and
easier than batting my head trying to figure out a RE for a problem
that the function allowed me to solve and move on.

CC


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

Date: Thu, 25 Sep 2008 16:17:52 +0000 (UTC)
From: comps@riffraff.plig.net (Vicky Conlan)
Subject: Re: Regular Expressions & replacements
Message-Id: <gbgdjg$17cu$1@magenta.plig.net>

According to <cartercc@gmail.com>:
>Don't get me wrong. My 'real' job is a database manager and I do a LOT
>of reporting, so I use RE's all the time. After a long time, I came to
>the conclusion that using the built in Perl functions were quicker and
>easier than batting my head trying to figure out a RE for a problem
>that the function allowed me to solve and move on.

Maybe it's a girl thing.  ;-)
It just seems more obvious to me to do a RE, which is a visual pattern
matching approach, rather than bothering to start counting.  It's never
really occurred to me before.

-- 


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

Date: Thu, 25 Sep 2008 11:08:42 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Regular Expressions & replacements
Message-Id: <gfknd4tskh1lo7qkcp0mr6mag3fu1to9hg@4ax.com>

comps@riffraff.plig.net (Vicky Conlan) wrote:
>I find it interesting that you consider substr more intuitive.  To me it
>screams RE, but I guess the match is a fixed length, so yes, a substr
>should be more logical.  Do you come from a C or Java background?
>
>Then again, you /do/ use a RE for your next, so you don't avoid them anyhow.

To me it is a question of using the right tool for the right job. If a
task doesn't require the _regular_ in RE then most likely REs are
overkill.

Another infamous example is using m// to check if a string is a
substring (no RE involved!) of another. index() is custom-made for
exactly that task, why not use it?

jue


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

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 V11 Issue 1875
***************************************


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