[19412] in Perl-Users-Digest
Perl-Users Digest, Issue: 1607 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 24 18:10:26 2001
Date: Fri, 24 Aug 2001 15:10:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <998691011-v10-i1607@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 24 Aug 2001 Volume: 10 Number: 1607
Today's topics:
Re: quick(?) programming question for perl newbie (Nathan McDannold)
Re: quick(?) programming question for perl newbie <krahnj@acm.org>
Re: read next line into $_ <gnarinn@hotmail.com>
Re: round off operator on Perl? (Logan Shaw)
Re: round off operator on Perl? <ilya@martynov.org>
Re: round off operator on Perl? <holland@origo.ifa.au.dk>
Re: round off operator on Perl? <o.moser@mobilkom.at>
Re: sysread problem on socket <news@althepal#nospam#.com>
Re: UTF8 versus ISO-8859-2 (Honza Pazdziora)
Re: UTF8 versus ISO-8859-2 <ilya@martynov.org>
Yet another regex question (Stan Brown)
Re: Yet another regex question <jeff@vpservices.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 24 Aug 2001 11:12:54 -0700
From: nmcdannold@yahoo.com (Nathan McDannold)
Subject: Re: quick(?) programming question for perl newbie
Message-Id: <58f70619.0108241012.59c35ce7@posting.google.com>
"John W. Krahn" <krahnj@acm.org> wrote in message news:<3B85AB8F.63489817@acm.org>...
>
> sub getheaderval {
> my $filename = shift || return;
> open IN, $filename or die "Cannot open $filename: $!";
> binmode IN;
> my $shortval;
> read( IN, $shortval, 2, 2194 ) == 2 or die "Cannot read from
> $filename: $!";
> close IN;
> return unpack 'n', pack 'S', $shortval;
> }
>
>
>
> John
Thank you for the rapid and helpful response.
I tried this code:
$filename= "F:\\myfile.dat";
print &getheaderval($filename),"\n";
sub getheaderval {
my $filename = shift || return;
open IN, $filename or die "Cannot open $filename: $!";
binmode IN;
my $shortval;
read( IN, $shortval, 2, 2194 ) == 2 or die "Cannot read from
$filename: $!";
close IN;
return unpack 'n', pack 'S', $shortval;
}
and it returns "0". The original code returns 2, which is correct. Am
I doing this wrong here? Looking at "myfile.dat" in a HEX editor gives
values of 00 02 for bytes 2194-2195. I'm not sure I "get" the
pack/unpack commands and what they really do. Any ideas of what I'm
missing?
Nathan
------------------------------
Date: Fri, 24 Aug 2001 18:56:32 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: quick(?) programming question for perl newbie
Message-Id: <3B86A3D3.27EE0920@acm.org>
Nathan McDannold wrote:
>
> "John W. Krahn" <krahnj@acm.org> wrote in message news:<3B85AB8F.63489817@acm.org>...
> >
> > sub getheaderval {
> > my $filename = shift || return;
> > open IN, $filename or die "Cannot open $filename: $!";
> > binmode IN;
> > my $shortval;
> > read( IN, $shortval, 2, 2194 ) == 2 or die "Cannot read from
> > $filename: $!";
> > close IN;
> > return unpack 'n', pack 'S', $shortval;
> > }
>
> Thank you for the rapid and helpful response.
>
> I tried this code:
>
> $filename= "F:\\myfile.dat";
>
> print &getheaderval($filename),"\n";
>
> sub getheaderval {
> my $filename = shift || return;
>
> open IN, $filename or die "Cannot open $filename: $!";
> binmode IN;
> my $shortval;
> read( IN, $shortval, 2, 2194 ) == 2 or die "Cannot read from
> $filename: $!";
> close IN;
>
> return unpack 'n', pack 'S', $shortval;
> }
>
> and it returns "0". The original code returns 2, which is correct. Am
> I doing this wrong here? Looking at "myfile.dat" in a HEX editor gives
> values of 00 02 for bytes 2194-2195. I'm not sure I "get" the
> pack/unpack commands and what they really do. Any ideas of what I'm
> missing?
Sorry, I think I misread your original post. Change:
return unpack 'n', pack 'S', $shortval;
to:
return unpack 'n', $shortval;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 24 Aug 2001 19:15:29 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: read next line into $_
Message-Id: <998680529.12009758176282.gnarinn@hotmail.com>
In article <3B8659EA.513BA69A@abc.se>, kb <_nospam_m9652@abc.se> wrote:
>hi
>
>how can i manually (not using a loop) read the next line from a text
>file into $_?
$_=<FH>;
the 'while (<FH>)' construct does an implied 'while (defined($_=<FH>))'
for more info see perldoc perlop (the I/O operator chapter)
remember, perldoc is your friend.
gnari
------------------------------
Date: 24 Aug 2001 13:07:38 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: round off operator on Perl?
Message-Id: <9m655a$nll$1@charity.cs.utexas.edu>
In article <3B869590.F0B56210@yahoo.com>,
Valentin 30IR976 <radiotito@yahoo.com> wrote:
>Hello. I have results on my Perl program like 2.5, 87.9, and I want to
>have only the "integer" part of this number. I know I can do something
>like:
>
>printf("%d",87.9);
>
>and it prints 87 (what I want), but my question is if Perl has any
>operator/function that make this thing automatic.
What do you mean by "automatic"? Presumably, when the Perl
interpreter executes
printf ("%d%", 87.9);
it doesn't require the user's assistance to finish. So in my mind,
it's automatic.
Do you mean you'd like to be able to do this:
$x = 3.2;
print "x is $x";
and have it print "x is 3"? I don't think there's a way to do that.
- Logan
--
"Our grandkids love that we get Roadrunner and digital cable."
(Advertisement for Time Warner cable TV and internet access, July 2001)
------------------------------
Date: 24 Aug 2001 22:10:08 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: round off operator on Perl?
Message-Id: <87ofp5pbwf.fsf@abra.ru>
>>>>> On Fri, 24 Aug 2001 19:57:36 +0200, Valentin 30IR976 <radiotito@yahoo.com> said:
V3> Hello. I have results on my Perl program like 2.5, 87.9, and I want to
V3> have only the "integer" part of this number. I know I can do something
V3> like:
V3> printf("%d",87.9);
V3> and it prints 87 (what I want), but my question is if Perl has any
V3> operator/function that make this thing automatic. I know in C exists,
V3> but...in Perl?
int(). See 'perldoc -tf int'
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/) |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/) |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
------------------------------
Date: 24 Aug 2001 20:17:11 +0200
From: Steve Holland <holland@origo.ifa.au.dk>
Subject: Re: round off operator on Perl?
Message-Id: <w47lmk9wcew.fsf@origo.ifa.au.dk>
> Valentin 30IR976 <radiotito@yahoo.com> wrote:
> >Hello. I have results on my Perl program like 2.5, 87.9, and I want to
> >have only the "integer" part of this number. I know I can do something
> >like:
> >
> >printf("%d",87.9);
> >
> >and it prints 87 (what I want), but my question is if Perl has any
> >operator/function that make this thing automatic.
Are you trying to print out only the integer part of the number?
If so then you can use
my $x = 87.9;
print int($x);
or
my $x = 87.9;
$x = int($x);
print $x;
=====================================================================
To find out who and where I am look at:
http://www.nd.edu/~sholland/index.html
Spammers: Please send spam to: abuse@aol.com and abuse@yahoo.com
=====================================================================
------------------------------
Date: Fri, 24 Aug 2001 20:07:15 +0200
From: Oliver Moser <o.moser@mobilkom.at>
Subject: Re: round off operator on Perl?
Message-Id: <3s5dot8776ja279b1hkh2g4gqgc637f2ps@4ax.com>
my $integer_value = int (89.7);
On Fri, 24 Aug 2001 19:57:36 +0200, Valentin 30IR976
<radiotito@yahoo.com> wrote:
>Hello. I have results on my Perl program like 2.5, 87.9, and I want to
>have only the "integer" part of this number. I know I can do something
>like:
>
>printf("%d",87.9);
>
>and it prints 87 (what I want), but my question is if Perl has any
>operator/function that make this thing automatic. I know in C exists,
>but...in Perl?
------------------------------
Oliver Moser
mobilkom austria AG & Co. KG
IT://mob.app
Mobil: +43-664-331-2544
mailto:o.moser@mobilkom.at
------------------------------
Date: Fri, 24 Aug 2001 18:57:28 GMT
From: Alex Hart <news@althepal#nospam#.com>
Subject: Re: sysread problem on socket
Message-Id: <ssxh7.6$tS5.9581@typhoon2.gnilink.net>
Anno Siegel wrote:
> According to Alex Hart <news@althepal#nospam#.com>:
> > Benjamin Goldberg wrote:
> >
> > > Alex Hart wrote:
> > > >
> > > > I have a POP client that I wrote by hand (well, I use Socket.pm) and
> > > > its has worked perfectly until recently. I found one server that it
> >
> > Is there really no rhyme or reason to the way sysread works? I have tested
> > my program on tons of servers and it always worked. How come the behavior
> > was so predictable before this one server?? This is totally confusing to
> > me.
>
> sysread() doesn't screen you from the behavior of the data source as
> much as read() and company do. In particular, reading from a socket,
> it gives you whatever the far end makes available, when it becomes
> available (on package level, I suppose). If the remote end decides
> to send a certain message in two portions, it will take two sysread()s,
> no matter what.
>
> I comes as no surprise that some servers behave differently in how
> they package their material. You have just been lucky so far. You
> can either continue using sysread() and, calling it in appropriate
> loops, make it cope with the new behavior. Or you switch to
> <POP> and let the system cope with it.
>
> [detailed discussion snipped]
>
> Anno
<<a lot was cut out>>
Will <POP> ever time out. What if the server does not respond legally, and
never sends the end of line character? Will the client stick?
Why would the server spit out its confimation message in more than one
portion? Not only that, but it's unpredictable. It breaks up the data at
different points each time. The behavior of this server makes no sense. Or
does it?
- Alex
------------------------------
Date: Fri, 24 Aug 2001 18:14:33 GMT
From: adelton@fi.muni.cz (Honza Pazdziora)
Subject: Re: UTF8 versus ISO-8859-2
Message-Id: <slrn9od6c7.565b.adelton@nemesis.fi.muni.cz>
On Fri, 24 Aug 2001 18:54:16 +0200, Oliver Moser <o.moser@mobilkom.at> wrote:
>
> I've got a big problem with UTF8, LDAP and slovenian
> characters ( ISO-8859-2 ).
>
> It's about a portal regsitration for a slovenian company. The user
> fills out a HTML form, where special charcters like ^s (small s with
> carron ) are allowed.
>
> My perl script converts the CGI parameters and writes them into LDAP;
> The problem is, that i have to covert that freak stuff characters into
> UTF8, which is no problem when your source characters are encoded with
> ISO-8859-1 (Latin1). But those special characters I get from the HTML
> form are ISO-8859-2 (Latin2, as far as I know).
>
> Actually, I've two problems. First, I want to convert those Latin2
> chars in utf8, but the Unicode::String module does not support that (
> does it??). Second, when i read out LDAP data, it's encoded in utf8
> (which is correct [at least for IPlanet Directory Sever] ), and to
> display the registration summary correctly, I have to reconvert the
> userdata (in utf8) to ISO-8859-2.
>
> I 'm stuck and I'm really sick of those feaky chars, so I would really
> appreciate if you could help me.
First, you really should find a possitive attitude to the problem.
You might try to use iconv library, or Text::IConv interface.
Or you might modify the following code which provides exactly the
conversions you need -- I'm using them daily and work just fine. I'm
sure that determining which is which direction will be easy to figure
out.
use Unicode::String qw(utf8);
use Unicode::Map8;
my $l2_map = Unicode::Map8->new("latin2") or die;
while (<>) {
my $u = utf8($_);
print $l2_map->to8($u->utf16);
}
__END__
use Unicode::Map8;
my $l2_map = Unicode::Map8->new("latin2") or die;
while (<>) {
print $l2_map->tou($_)->utf8;
}
__END__
Yours,
--
------------------------------------------------------------------------
Honza Pazdziora | adelton@fi.muni.cz | http://www.fi.muni.cz/~adelton/
.project: Perl, mod_perl, DBI, Oracle, auth. WWW servers, XML/XSL, ...
Petition for a Software Patent Free Europe http://petition.eurolinux.org
------------------------------------------------------------------------
------------------------------
Date: 24 Aug 2001 22:06:49 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: UTF8 versus ISO-8859-2
Message-Id: <87wv3tpc1y.fsf@abra.ru>
>>>>> On Fri, 24 Aug 2001 18:54:16 +0200, Oliver Moser <o.moser@mobilkom.at> said:
OM> Dear NG,
OM> I've got a big problem with UTF8, LDAP and slovenian
OM> characters ( ISO-8859-2 ).
OM> It's about a portal regsitration for a slovenian company. The user
OM> fills out a HTML form, where special charcters like ^s (small s with
OM> carron ) are allowed.
OM> My perl script converts the CGI parameters and writes them into LDAP;
OM> The problem is, that i have to covert that freak stuff characters into
OM> UTF8, which is no problem when your source characters are encoded with
OM> ISO-8859-1 (Latin1). But those special characters I get from the HTML
OM> form are ISO-8859-2 (Latin2, as far as I know).
OM> Actually, I've two problems. First, I want to convert those Latin2
OM> chars in utf8, but the Unicode::String module does not support that (
OM> does it??). Second, when i read out LDAP data, it's encoded in utf8
OM> (which is correct [at least for IPlanet Directory Sever] ), and to
OM> display the registration summary correctly, I have to reconvert the
OM> userdata (in utf8) to ISO-8859-2.
Use Unicode::Map8 do conversions between ISO-8859-2 and UTF8.
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/) |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80 E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/) |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
------------------------------
Date: 24 Aug 2001 15:17:34 -0400
From: stanb@panix.com (Stan Brown)
Subject: Yet another regex question
Message-Id: <9m698e$2o4$1@panix2.panix.com>
given a string like in(0,1) where there may b 2 to many terms between the
parens, and they may or may not be numbers. How can best parse this, such
that I wind up with the terms between the parens in an array?
------------------------------
Date: Fri, 24 Aug 2001 12:30:35 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: Yet another regex question
Message-Id: <3B86AB5B.E5F359AD@vpservices.com>
Stan Brown wrote:
>
> given a string like in(0,1) where there may b 2 to many terms between the
> parens, and they may or may not be numbers. How can best parse this, such
> that I wind up with the terms between the parens in an array?
If none of the terms can contain embedded commas, then this will
suffice:
my $str = in(0,1);
$str =~ s/in\s*\(\s*(.*)\s*\)/$1/;
my @ary = split /,/, $str;
If some of the terms can contain embedded commas, use Text::CSV_XS or
one of the other solutions suggested in the perlfaq4 question on
comma-separated files. (perldoc -q delimited string).
--
Jeff
------------------------------
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.
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 1607
***************************************