[28178] in Perl-Users-Digest
Perl-Users Digest, Issue: 9542 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 31 18:10:16 2006
Date: Mon, 31 Jul 2006 15:10:08 -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 Mon, 31 Jul 2006 Volume: 10 Number: 9542
Today's topics:
Printing Hash Marks During File Download <hlarons@yahoo.com>
Re: Printing Hash Marks During File Download xhoster@gmail.com
Re: Sending data from perl to gnuplot and getting an "A <a24061@yahoo.com>
Sharing a socket between instances <dbasch@yahoo.com>
Re: Sharing a socket between instances xhoster@gmail.com
Re: Sharing a socket between instances <dbasch@yahoo.com>
Re: Sharing a socket between instances xhoster@gmail.com
Re: Sharing a socket between instances <dbasch@yahoo.com>
Re: Strange characters using Term::Readline on Win32 <ThomasKratz@REMOVEwebCAPS.de>
Re: Using an input/csv file to rename files <tdcarr@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 31 Jul 2006 18:01:29 -0000
From: HaroldWho <hlarons@yahoo.com>
Subject: Printing Hash Marks During File Download
Message-Id: <slrnecshbo.qs.hlarons@Beagle.mylocal.net>
I am using the Net::POP3 module to retrieve mail from a dialup POP3 server.
The method 'get->(<msg number>)' returns a reference to an array which
contains the lines of message text read from the server. So far, so good.
Using dialup, large messages take some time to download, during which the
program gives no indication that the d/l is going OK.
I'd like to print hash marks during the d/l, much like an ftp d/l does, but
I'm stumped for a way to do it, at using some kind of loop.
Ideas?
HaroldWho
--
Powered by Slackware 10.2 Linux -- Kernel 2.6.13
News Reader slrn 0.9.8.1
------------------------------
Date: 31 Jul 2006 18:07:18 GMT
From: xhoster@gmail.com
Subject: Re: Printing Hash Marks During File Download
Message-Id: <20060731141456.586$Mh@newsreader.com>
HaroldWho <hlarons@yahoo.com> wrote:
> I am using the Net::POP3 module to retrieve mail from a dialup POP3
> server. The method 'get->(<msg number>)' returns a reference to an array
> which contains the lines of message text read from the server. So far, so
> good.
>
> Using dialup, large messages take some time to download, during which the
> program gives no indication that the d/l is going OK.
Why wouldn't it be going OK? Presumably, if it not going OK, it would
give you an error message, right?
>
> I'd like to print hash marks during the d/l, much like an ftp d/l does,
> but I'm stumped for a way to do it, at using some kind of loop.
>
> Ideas?
For CGI where the web browser (software, not human) might give up if it
doesn't hear anything after a while, I use something like this:
{
local $SIG{ALRM}= sub { print ".\n"; alarm 10 };
alarm 10;
$t->long_operation();
alarm 0;
};
The signals use by alarm might interfere with your long_operation (in my
case they don't) and, of course, if long_operation goes all wonky but fails
to abort or report it, this merrily goes along printing anyway just as if
everything were OK.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Mon, 31 Jul 2006 16:14:22 +0100
From: Adam Funk <a24061@yahoo.com>
Subject: Re: Sending data from perl to gnuplot and getting an "ASCII-art" graph back?
Message-Id: <ebj1q3-mkf.ln1@news.ducksburg.com>
On 2006-07-30, John W. Krahn <someone@example.com> wrote:
>> The Perl program runs and prints the plot to the screen. I'm about to
>> modify it to use backticks
>> $gnuplot_output = `gnuplot $cmd_file`
>> but I can't believe there isn't a better way than what I've done to
>> send a list of commands to gnuplot and get the plot back.
> You can probably do what you want with IPC::Open2 or Expect.
>
> perldoc IPC::Open2
> perldoc Expect
Those look quite useful, especially IPC::Open2 (which looks easier to
use).
I've got my program to work using Theo's suggestions, but I'll bear
those in mind for the future.
Thanks,
Adam
------------------------------
Date: 31 Jul 2006 10:28:50 -0700
From: "Derek Basch" <dbasch@yahoo.com>
Subject: Sharing a socket between instances
Message-Id: <1154366930.319838.90800@75g2000cwc.googlegroups.com>
I can only imagine what kind of problems would occur if I were to share
a socket between several instances of the Gps object. Short of threads,
forks or locking what would be the proper way to do something similar?
package Gps;
use base qw(Class::Accessor);
use IO::Socket;
use Error qw(:try);
# Declare class variables.
my $gpsclientport = 1409;
my $remote_port = 6635;
my $peer_addr = 'gps.spanky.com';
try {
$gps_socket = IO::Socket::INET->new(PeerAddr => $peer_addr,
PeerPort => $remote_port,
Proto => 'tcp',
Type => SOCK_STREAM) or throw Error::Simple("Can't bind socket\n");
}
catch Error with {
$E = shift;
print STDERR "File ", $E->{'-file'}, $E->{'-text'}, " \n";
};
Meanwhile back at the ranch!!
use GPS;
$a = Gps->new();
$b = Gps->new();
Do some socket client stuff......
Thanks everyone,
Derek Basch
------------------------------
Date: 31 Jul 2006 17:47:50 GMT
From: xhoster@gmail.com
Subject: Re: Sharing a socket between instances
Message-Id: <20060731135528.658$xe@newsreader.com>
"Derek Basch" <dbasch@yahoo.com> wrote:
> I can only imagine what kind of problems would occur if I were to share
> a socket between several instances of the Gps object.
Without more information, I can't even do that. :) Well, I mean I can, but
then there is no reason to think my imaginings would be at all relevent
to anything you care about.
> Short of threads,
> forks or locking what would be the proper way to do something similar?
It is not clear what you are trying to do, so I don't know what would be
similar to it (or why you would expect threads or forks to make things
better rather than worse; or why locking on a socket would be necessary in
the absense of forking or threads.)
>
> package Gps;
> use base qw(Class::Accessor);
> use IO::Socket;
> use Error qw(:try);
>
> # Declare class variables.
> my $gpsclientport = 1409;
> my $remote_port = 6635;
> my $peer_addr = 'gps.spanky.com';
Isn't $gps_socket a class variable? Why isn't it declared?
>
> try {
> $gps_socket = IO::Socket::INET->new(PeerAddr => $peer_addr,
> PeerPort => $remote_port,
> Proto => 'tcp',
> Type => SOCK_STREAM) or throw Error::Simple("Can't bind socket\n");
> }
> catch Error with {
> $E = shift;
> print STDERR "File ", $E->{'-file'}, $E->{'-text'}, " \n";
> };
Since this doesn't seem to be fatal, I take it that the socket is only
incidental to the operation of your program and not central to it?
>
> Meanwhile back at the ranch!!
> use GPS;
> $a = Gps->new();
> $b = Gps->new();
> Do some socket client stuff......
Presumable the problem arises when you use your socket. But you haven't
shown us anything that uses the socket. Is this a write-only socket?
How are messages delimited?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 31 Jul 2006 11:30:47 -0700
From: "Derek Basch" <dbasch@yahoo.com>
Subject: Re: Sharing a socket between instances
Message-Id: <1154370647.878075.284030@h48g2000cwc.googlegroups.com>
> It is not clear what you are trying to do, so I don't know what would be
> similar to it (or why you would expect threads or forks to make things
> better rather than worse; or why locking on a socket would be necessary in
> the absense of forking or threads.)
> Presumable the problem arises when you use your socket. But you haven't
> shown us anything that uses the socket. Is this a write-only socket?
> How are messages delimited?
Sorry, I was trying to keep my example concise but I guess I made it to
concise.
I am attempting to create a 'Member' object that represents a member of
a club. Lets say a gardening club. Members have attributes that are
stored on a telnet server called Gps and I am using an overridden
Class::Accessor object to get/set those attributes. The telnet server
requires that the Members numeric ID attribute be set prior to
getting/setting a Members attribute (address for example).
So, if I have 30,000 'Member' instances all sharing a single socket I
am afraid that the 'member_id' state of the socket will be changed by
another 'Member' instance ($b) before the 'address' attribute of the
first instance ($a) is set. Is this a valid concern?
<---------------------------------------------------------------------->
package Gps;
use base qw(Class::Accessor);
use IO::Socket;
use Error qw(:try);
# Declare class variables.
my $gpsclientport = 1409;
my $remote_port = 6635;
my $peer_addr = 'gps.spanky.com';
my $gps_socket;
try {
$gps_socket = IO::Socket::INET->new(PeerAddr => $peer_addr,
PeerPort => $remote_port,
Proto => 'tcp',
Type => SOCK_STREAM) or throw Error::Simple("Can't bind socket\n");
}
catch Error with {
$E = shift;
print STDERR "File ", $E->{'-file'}, $E->{'-text'}, " \n";
}
sub get {
my ($self, $key) = @_;
# get the desired value by key
@response = $self->_doCommand("get $key");
$value = join("", grep(!/^$key:/, @response));
return $value;
}
sub set {
my ($self, $key, $value) = splice(@_, 0, 3);
# set the desired value by key/value
@response = $self->_doCommand("set $key $value");
$value = join("", grep(!/^$key:/, @response));
return $value;
}
sub _doCommand {
($self, $command) = @_;
my @response;
try {
# Check that the socket is available
$gps_socket or throw Error::Simple("Can't bind : $gps_socket\n");
# set the user by user_id
print $gps_socket "ID $self->user_id", "\r\n";
#member_id state could be changed here by another Member instance
#calling the socket at the same time??????????
# print to the GPS socket
print $gps_socket $command, "\r\n";
while (<$gps_socket>) {
chop;
last if /^###/;
push (@response, $_);
}
throw Error::Simple("@response") if (grep(/Error/i, @response));
}
catch Error with {
$E = shift;
print STDERR "$E->{-text}\n";
}
finally {
return @response;
};
}
1;
<---------------------------------------------------------------------->
package Member;
use Gps;
sub new {
my $class = shift;
my $self = {};
$self->{'Gps'} = Gps->new;
bless ($self, $class);
return $self;
}
1;
<---------------------------------------------------------------------->
#Meanwhile in some other code
use Member;
$a = Member->new;
$a->{'Gps'}->user_id(323775);
$a->{'Gps'}->address("1234 Fake St.");
<---------------------------------------------------------------------->
#Meanwhile in some other code
use Member;
$b = Member->new;
$b->{'Gps'}->user_id(323776);
$b->{'Gps'}->address("666 Devil St.");
<---------------------------------------------------------------------->
------------------------------
Date: 31 Jul 2006 18:59:59 GMT
From: xhoster@gmail.com
Subject: Re: Sharing a socket between instances
Message-Id: <20060731150738.264$BT@newsreader.com>
"Derek Basch" <dbasch@yahoo.com> wrote:
>
> I am attempting to create a 'Member' object that represents a member of
> a club. Lets say a gardening club. Members have attributes that are
> stored on a telnet server called Gps and I am using an overridden
> Class::Accessor object to get/set those attributes. The telnet server
> requires that the Members numeric ID attribute be set prior to
> getting/setting a Members attribute (address for example).
>
> So, if I have 30,000 'Member' instances all sharing a single socket I
> am afraid that the 'member_id' state of the socket will be changed by
> another 'Member' instance ($b) before the 'address' attribute of the
> first instance ($a) is set. Is this a valid concern?
....
>
> sub _doCommand {
> ($self, $command) = @_;
>
> my @response;
> try {
> # Check that the socket is available
> $gps_socket or throw Error::Simple("Can't bind : $gps_socket\n");
>
> # set the user by user_id
> print $gps_socket "ID $self->user_id", "\r\n";
This doesn't print you think it prints. Method calls are not interpolated
into double-quotes. And if it did do that, then this would be the start of
an infinite recursion, would it not?
>
> #member_id state could be changed here by another Member instance
> #calling the socket at the same time??????????
>
> # print to the GPS socket
> print $gps_socket $command, "\r\n";
>
> while (<$gps_socket>) {
> chop;
> last if /^###/;
> push (@response, $_);
> }
> throw Error::Simple("@response") if (grep(/Error/i, @response));
In the absence of threads or fork (and the infinite recursion previously
mentioned), no _doCommand method serving a different Member instance can
seize control of the program during this code, so there should be no
problem with the state being changed.
>
> <---------------------------------------------------------------------->
> #Meanwhile in some other code
>
> use Member;
> $a = Member->new;
Shouldn't setting the user_id be part of the new()? Since it doesn't
make sense to do anything (including setting the user_id) on a Member with
an unset user_id.
> $a->{'Gps'}->user_id(323775);
> $a->{'Gps'}->address("1234 Fake St.");
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 31 Jul 2006 14:15:11 -0700
From: "Derek Basch" <dbasch@yahoo.com>
Subject: Re: Sharing a socket between instances
Message-Id: <1154380511.582503.268810@i3g2000cwc.googlegroups.com>
> > sub _doCommand {
> > ($self, $command) = @_;
> >
> > my @response;
> > try {
> > # Check that the socket is available
> > $gps_socket or throw Error::Simple("Can't bind : $gps_socket\n");
> >
> > # set the user by user_id
> > print $gps_socket "ID $self->user_id", "\r\n";
>
> This doesn't print you think it prints. Method calls are not interpolated
> into double-quotes. And if it did do that, then this would be the start of
> an infinite recursion, would it not?
Yes, you are right it would create an infinite recursion. However, once
again, I was trying to be concise and didn't include the overidden
accessor function for user_id:
sub user_id {
my($self) = shift;
my($user_id) = @_;
return $self->_user_id_accessor(@_);
}
This prevents the recursion for reasons that are explained in gory
detail here:
http://search.cpan.org/~kasei/Class-Accessor-0.27/lib/Class/Accessor.pm#Overriding_autogenerated_accessors
I did not know that the return value of methods could not be
interpolated. I guess I would have found out when I tried to run the
code :). I suppose I should use concatenation instead.
> > #member_id state could be changed here by another Member instance
> > #calling the socket at the same time??????????
> >
> > # print to the GPS socket
> > print $gps_socket $command, "\r\n";
> >
> > while (<$gps_socket>) {
> > chop;
> > last if /^###/;
> > push (@response, $_);
> > }
> > throw Error::Simple("@response") if (grep(/Error/i, @response));
>
> In the absence of threads or fork (and the infinite recursion previously
> mentioned), no _doCommand method serving a different Member instance can
> seize control of the program during this code, so there should be no
> problem with the state being changed.
Thanks for the information. Is this idiom documented anywhere? I
couldn't seem to find anything on the subject of order of execution and
locking for class variables. How does Perl know that the class variable
filehandle (file_handle) which is being called by an instance (foo) is
locked because a different instance (foo2) is already using that same
filehandle (file_handle)?
> > <---------------------------------------------------------------------->
> > #Meanwhile in some other code
> >
> > use Member;
> > $a = Member->new;
>
> Shouldn't setting the user_id be part of the new()? Since it doesn't
> make sense to do anything (including setting the user_id) on a Member with
> an unset user_id.
Yes, I was planning on implementing that once I figured out this
problem. So, setting the member_id using a constructor would ensure
that the member_id is available for any instance of 'Member'. However,
it seems that the member_id state would still have to be set prior to
any subsequent attribute gets/sets for that Member instance and my
original "problem" of conflicting filehandle access would still be
valid.
Such as:
# set the user by user_id
# user_id is set as a parameter to the class constructor
print $gps_socket "ID" . $self->user_id . "\r\n";
#member_id state could be changed here by another Member instance
#calling the socket at the same time??????????
# print to the GPS socket
print $gps_socket $command, "\r\n";
while (<$gps_socket>) {
chop;
last if /^###/;
push (@response, $_);
}
Thanks for all the help. I really appreciate it.
Derek Basch
------------------------------
Date: Mon, 31 Jul 2006 18:02:38 +0200
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: Strange characters using Term::Readline on Win32
Message-Id: <44ce299d$0$31168$bb690d87@news.main-rheiner.de>
Wolfram Humann wrote:
> I'm getting strange characters (escape sequences???) at the prompt and
> when typing characters using Term::ReadLine. As an example I type 'ABCD'
> followed by the return key at the prompt generated by this line:
>
> perl -M"Term::ReadLine" -e"Term::ReadLine->new('')->readline('PROMPT')"
>
> and what I get is this:
>
> ←[4;mPROMPT←[1mA←[0m←[1mB←[0m←[1mC←[0m←[1mD←[0m
Do you have a TERM environment variable? Try doing a
set TERM=
before running the script.
Thomas
--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
------------------------------
Date: 31 Jul 2006 14:08:30 -0700
From: "Terry" <tdcarr@gmail.com>
Subject: Re: Using an input/csv file to rename files
Message-Id: <1154380110.876491.57790@m73g2000cwd.googlegroups.com>
axel@white-eagle.invalid.uk wrote:
> Terry <tdcarr@gmail.com> wrote:
> > I am a few hours into my life after Perl.
>
> > I need to rename approximately 5000 .jpg files, which I think I have
> > figured out thanks to "Learning Perl" 3rd. Ed. (pg. 176).
>
> > Unlike that example, I would like to compare/match the file names to a
> > table/field in a csv file and once matched rename the file to the
> > second table/field in the csv file so that ABCDEFG.jpg becomes
> > 1111111.jpg and ABCDEFH.jpg becomes 1111112.jpg and so on.
>
> > Any suggestions of howto: or where to look would be greatly
> > appreciated.
>
> If I understand correctly, you have a CSV file based on two filenames (and
> maybe some extra fields?) and need to take your list of filenames, look
> up and old name if if in the CSV list, rename it to a new name from the
> same record in the CSV list.
>
> Others have mentioned CPAN modules to handle CSV data. Although if
> your data are simply filenames separated by a comma with no fields
> specified in quotation marks, then it may be easier to simply split
> the incoming records... but only if you are totally sure of your data
> source.
>
> Whichever method you choose, you can then create a hash based on a key
> of the 'old' filename and value of the 'new' filename.
>
> It is then trivial to check incoming filenames to see if they are in the
> hash and perform any required renaming, logging and so on.
>
> Axel
Axel,
You are correct understanding what I am saying.
My data file contains name, quasi-random letters (ROT'ed from the ID),
and an ID number.
If I understand you correctly, I should use split to read my file then
use a hash to rename my files? I will try it this way.
My appologies for the 5 days inbetween posts, but I went on vacation
and unplugged for a few days.
Thank you
Terry
------------------------------
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 9542
***************************************