[24618] in Perl-Users-Digest
Perl-Users Digest, Issue: 6794 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 12 09:05:43 2004
Date: Mon, 12 Jul 2004 06: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 Mon, 12 Jul 2004 Volume: 10 Number: 6794
Today's topics:
Re: Code Style -- here-docs -- How do you make them loo <vetro@online.no>
Re: date problem <gabriell@programmer.net>
Re: date problem <gabriell@programmer.net>
Re: date problem <gabriell@programmer.net>
Re: date problem <gabriell@programmer.net>
File exists on Mac OS X <peter@semantico.com>
Re: File exists on Mac OS X (Anno Siegel)
Re: File exists on Mac OS X <peter@semantico.com>
Re: looping through array <bobsmith@[no-thankyou-very-much]jippii.fi>
MacPerl vs. Unix (OS X) Perl <nolson66@comcast.net>
Re: MacPerl vs. Unix (OS X) Perl <1usa@llenroc.ude.invalid>
Re: MacPerl vs. Unix (OS X) Perl <tim@vegeta.ath.cx>
Re: MacPerl vs. Unix (OS X) Perl (Jim Keenan)
Shareit information via Perl <n.vasiliev@apatit.com>
Re: Shareit information via Perl <peter@semantico.com>
Re: Useful one-liners and other short Perl scripts <Joe.Smith@inwap.com>
Re: what do you call funct ( funct()) <josef.moellers@fujitsu-siemens.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 12 Jul 2004 09:33:20 +0200
From: "Vetle Roeim" <vetro@online.no>
Subject: Re: Code Style -- here-docs -- How do you make them look good?
Message-Id: <opsa0kluuc3hk3cf@quickfix.opera.com>
On Sat, 10 Jul 2004 06:49:05 GMT, Ala Qumsieh <notvalid@email.com> wrote:
[...]
> That is of course correct. But, as an Emacs user, I know for a fact that
> heredocs confuse its cperl mode, and the auto indentation breaks after
> the end marker. Putting a lone semicolon on a line by itself after the
> end marker fixes the problem.
I believe you are mistaken. It is the normal perl-mode that is confused
by the heredocs, and cperl-mode is not. At least that is my experience.
[...]
--
It's not a bug, it's the future.
------------------------------
Date: Mon, 12 Jul 2004 09:17:41 +0200
From: Gabkin <gabriell@programmer.net>
Subject: Re: date problem
Message-Id: <2les8oFbng8vU1@uni-berlin.de>
Tapani Tarvainen wrote:
> Bob <bob@dont.spam.me> writes:
>
>
>>Ahh ... In that case we need the first digit of nanoseconds:
>>
>> set $(date +%Y%m%d%H%M%S%N)
>
>
> Interesting. In HP-UX date +%N gives emperor name (for Japanese
> calendar), not nanoseconds... it doesn't seem to be defined
> at all by POSIX. But it does seem to work with Gnu date.
>
>
>> echo ${1:0:15}
>
>
> That is also non-POSIX, it assumes bash or ksh93 or similar.
> But with Gnu date you can simply use "%-1N" to get only the first
> digit of nanoseconds (= deciseconds).
>
Aha!
Thanks
The answer I was looking for was
"date +%Y%m%d%H%M%S%-1N"
which accomplishes exactly what i wanted.
Super high precision isnt entirely necessary as this is just going to be
used as a unique number which must be 15 digits long, not 14. I was told
that the standard for this system is YYYYMMDDHHMMSSS.
Once again, thank you for the prompt and useful answer!
--
NH 3.4.3,S'EM W Gabkin-Rog-Hum-Fem-Cha HP:284(284) Pw:145(145) AC:-43
[+++ )++ i+ 2+++ P- S+ D++ P $+++ t-- s+++ W E++ PS-- PP+++
!G C- I++ PS+ @W* N++ Y- X+ So++ Sp+ !sb wb--
------------------------------
Date: Mon, 12 Jul 2004 09:45:00 +0200
From: Gabkin <gabriell@programmer.net>
Subject: Re: date problem
Message-Id: <2letrvFbsai9U1@uni-berlin.de>
Gunnar Hjalmarsson wrote:
> Gabkin wrote:
>
>> I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
>> program. (Note the _three_ digits allocated for Seconds, not the
>> usual _two_)
>
>
> sub timestamp {
> require Time::HiRes;
> my ($epoch, $micro) = Time::HiRes::gettimeofday();
> my ($s, $mi, $h, $d, $mo, $y) = (localtime $epoch)[0..5];
> sprintf '%d%02d%02d%02d%02d%02d%.0f',
> $y+1900, $mo+1, $d, $h, $mi, $s, $micro/100000
> }
>
> print timestamp();
>
> Outputs e.g.:
> 200407112303299
>
> P.S.
> http://groups.google.com/groups?selm=2lcs92Fbdp10U1%40uni-berlin.de
>
Grooh!
I suppose that may do the trick and that is kind of what i would have
done if I hadn't found this...
"date +%Y%m%d%H%M%S%-1N" (GNU date of course!)
The "-1%N" returns only the first digit on nanoseconds.
Once again, those cunning GNU hackers anticipated my needs!
Thanks all the same
------------------------------
Date: Mon, 12 Jul 2004 12:31:57 +0200
From: Gabkin <gabriell@programmer.net>
Subject: Re: date problem
Message-Id: <2lf7l0Fbtt9nU1@uni-berlin.de>
Gabkin wrote:
> I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
> program. (Note the _three_ digits allocated for Seconds, not the usual
> _two_)
>
> date +%Y%m%d%H%M%S will give YYYYMMDDHHMMSS which is close but it doesnt
> have that extra S at the end.
>
> I am using "date (sh-utils) 2.0.15" and "perl, v5.8.2" on Cygwin.
>
> Is there a quick and easy way of getting that extra degree of precision
> with 'date'?
> Failing that, a way to get it with perl?
>
> Thanks
Update: the correct answer is
"date +%Y%m%d%H%M%S%-1N"
However as it turns out, even this was inadequate as perl executed the
task too rapidly. This is basically what I am thinking of using...
(feel free to criticize my code)
<PERL>
#!/usr/local/bin/perl
for(1..15000) {
print(&getUnique15DigitDate(),"\n");
}
exit;
sub getUnique15DigitDate() {
select(undef,undef,undef,0.1);
open(DATE,'date +%Y%m%d%H%M%S%N| ');
my $date=<DATE>;close DATE;
$date =~ s/\n|\-//;
return substr($date,0,15);
}
</PERL>
Basically I force the program to 'wait' 0.1 seconds every time before
generating a timestamp, to ensure that it is unique.
This should work but it has the unfortunate side-effect of pushing up
execution speed dramatically.This script alone should take 15 minutes to
complete. The full program takes 5 minutes, a huge improvement over
traditional in-house methods using VB, which normally take several
hours. I chose 15000 for this script because I know for a fact that my
program will loop 15472 times, thus using waiting 0.1 seconds everytime
could push up execution speed by just over 25 minutes, a 500% increase!
Other methods:
use 'int(rand(9))' for the 15th digit and use a normal 14 digit
timestamp for the rest.
OR
Use an incremented counter for the 15th digit and only use the last
digit of that counter.
Is there a modulus function in perl?
I can't find one....
Suggestions, anyone?
------------------------------
Date: Mon, 12 Jul 2004 13:05:56 +0200
From: Gabkin <gabriell@programmer.net>
Subject: Re: date problem
Message-Id: <2lf9kkFbmd5fU1@uni-berlin.de>
> Basically I force the program to 'wait' 0.1 seconds every time before
> generating a timestamp, to ensure that it is unique.
>
> This should work but it has the unfortunate side-effect of pushing up
> execution speed dramatically.This script alone should take 15 minutes to
> complete. The full program takes 5 minutes, a huge improvement over
> traditional in-house methods using VB, which normally take several
> hours. I chose 15000 for this script because I know for a fact that my
> program will loop 15472 times, thus using waiting 0.1 seconds everytime
> could push up execution speed by just over 25 minutes, a 500% increase!
actually I timed it and it took 592 seconds(almost 10 minutes), which is
still quite the increase!
--
NH 3.4.3,S'EM W Gabkin-Rog-Hum-Fem-Cha HP:284(284) Pw:145(145) AC:-43
[+++ )++ i+ 2+++ P- S+ D++ P $+++ t-- s+++ W E++ PS-- PP+++
!G C- I++ PS+ @W* N++ Y- X+ So++ Sp+ !sb wb--
------------------------------
Date: Mon, 12 Jul 2004 11:25:07 +0100
From: Peter Hickman <peter@semantico.com>
Subject: File exists on Mac OS X
Message-Id: <40f26706$0$14058$afc38c87@news.easynet.co.uk>
I have a generic file renaming code that lowercases the filename but before it
does the renaming it checks if the lowercased version of the filename already
exists. This works fine on Linux however on Max OS X the filesystem is case
agnostic and so if the file ZXC exists then perl will report that zxc, zxC, zXc,
zXC, Zxc, ZxC and ZXc all also exist even if they don't.
Is there a way to keep my code portable but not get this problem?
------------------------------
Date: 12 Jul 2004 10:54:44 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: File exists on Mac OS X
Message-Id: <cctqlk$14e$1@mamenchi.zrz.TU-Berlin.DE>
Peter Hickman <peter@semantico.com> wrote in comp.lang.perl.misc:
> I have a generic file renaming code that lowercases the filename but before it
> does the renaming it checks if the lowercased version of the filename already
> exists. This works fine on Linux however on Max OS X the filesystem is case
> agnostic
Not necessarily, but usually it is.
> and so if the file ZXC exists then perl will report that zxc,
> zxC, zXc,
> zXC, Zxc, ZxC and ZXc all also exist even if they don't.
It's worse than that. rename() will fail on a case-insensitive system
when the only difference in the new name is in case.
> Is there a way to keep my code portable but not get this problem?
You need an intermediate name (or an auxiliary directory) to do this
kind of renaming. Look at File::Temp for a reasonably safe way of
creating unique file names.
Anno
------------------------------
Date: Mon, 12 Jul 2004 12:04:09 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: File exists on Mac OS X
Message-Id: <40f2702c$0$18229$afc38c87@news.easynet.co.uk>
Anno Siegel wrote:
> It's worse than that. rename() will fail on a case-insensitive system
> when the only difference in the new name is in case.
>
>
>>Is there a way to keep my code portable but not get this problem?
>
>
> You need an intermediate name (or an auxiliary directory) to do this
> kind of renaming. Look at File::Temp for a reasonably safe way of
> creating unique file names.
>
> Anno
Thanks for the information, I will look into File::Temp, it seems a mite
wasteful but if it works who cares.
------------------------------
Date: Mon, 12 Jul 2004 11:30:28 +0200
From: svenne <bobsmith@[no-thankyou-very-much]jippii.fi>
Subject: Re: looping through array
Message-Id: <9NrIc.3073$0l2.1932@reader1.news.jippii.net>
Peter Payne wrote:
> my %h_array2 = map { $_, 1 } @ar_array2;
>
> for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )
> {
> if ( exists( $h_array2{ $ar_array1[$i] } ) )
> {
> print( " Element \""
> . $ar_array1[$i]
> . "\" exists in array2!\n" );
> }
> }
>
> Ooh ooh my head hurts, ooh that was SOOO hard, oh oh oh why did
> I ever become a programmer?? I never expected to have to THINK
> for myself.. cruel world..
>
> svenne <bobsmith@[no-thankyou-very-much]jippii.fi> wrote:
>> this is trivvial question but how do you compare existense of elements
>> intwo arrays?, that is I want to compare two arrays and check the
>> existense of the first array's elements in the second array,
>>
>> one line of code if possible...
>>
>> any advice much appreciated, thank you.
>> /G
so basically
my %h_array2 = map { $_, 1 } @ar_array2;
for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )
{
if ( exists( $h_array2{ $ar_array1[$i] } ) ){
return true;
}
return false;
}
...is what I need, thank you for replying, *S*
have a great day!
--
--
www.gh-webinteractive.com
------------------------------
Date: Sun, 11 Jul 2004 22:53:59 -0700
From: Nathan Olson <nolson66@comcast.net>
Subject: MacPerl vs. Unix (OS X) Perl
Message-Id: <BD177587.14163%nolson66@comcast.net>
Any reason why the following behaves differently from MacPerl to Unix Perl
(in OS 10.2.8)? (Assume all I want to process is the first file; hence the
@ARGV[0].)
open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
@contents = <THISFILE>;
close (THISFILE);
In MacPerl, an ordinary text file is parsed into the @contents array with
each paragraph being a element. But in OS X/Unix Perl, the entire contents
of the text file is placed in @contents[0]; in other words, it comes in as a
long string, not as separate elements. Why the difference? Does it have to
do with line-endings? And how can I change this?
Any help will be appreciated.
Nate Olson
------------------------------
Date: 12 Jul 2004 06:10:00 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: MacPerl vs. Unix (OS X) Perl
Message-Id: <Xns95241609DAC50asu1cornelledu@132.236.56.8>
Nathan Olson <nolson66@comcast.net> wrote in
news:BD177587.14163%nolson66@comcast.net:
> Any reason why the following behaves differently from MacPerl to Unix
> Perl (in OS 10.2.8)? (Assume all I want to process is the first file;
> hence the @ARGV[0].)
>
> open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
> @contents = <THISFILE>;
> close (THISFILE);
It is too late for me to look up what line end character Mac uses right now
but a simple Google search should yield the answer.
OTOH, it looks like you might want to consult the following FAQ:
perldoc -q @array
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Mon, 12 Jul 2004 08:01:16 -0000
From: Tim Hammerquist <tim@vegeta.ath.cx>
Subject: Re: MacPerl vs. Unix (OS X) Perl
Message-Id: <slrncf4h57.rtc.tim@vegeta.saiyix>
Nathan Olson <nolson66@comcast.net> wrote:
> Any reason why the following behaves differently from MacPerl to Unix
> Perl (in OS 10.2.8)? (Assume all I want to process is the first file;
> hence the @ARGV[0].)
>
>
>
> open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
> @contents = <THISFILE>;
> close (THISFILE);
>
>
> In MacPerl, an ordinary text file is parsed into the @contents array
> with each paragraph being a element. But in OS X/Unix Perl, the
> entire contents of the text file is placed in @contents[0]; in other
> words, it comes in as a long string, not as separate elements. Why
> the difference? Does it have to do with line-endings? And how can
> I change this?
>
> Any help will be appreciated.
I'm not sure why neither perl seemed to operate correctly, but I'm sure
part of it had to do with line endings.
Mac OS <= 9 used line endings "\x0a\x0a", and Mac OS X (like other
Unixen) uses "\x0a". This would explain why MacPerl split on
paragraphs which, internally, would be two consecutive "\x0a".
I would suggest entering a hex editor and ensuring the line endings are
what OS X 10.2 perl expects them to be.
The following works correctly on both my system perl that came with OS
X 10.3 perl (5.8.1-RC3) and my custom built (non-fink)
/usr/local/bin/perl (5.8.4). Following is the code and the respective
-v output. (Can't offer any direction on MacPerl; sorry.)
#!/usr/local/bin/perl -w
open FILE, $ARGV[0] or die "can't open $ARGV[0]";
@contents = <FILE>;
close FILE;
for (@contents) {
chomp;
print "<$_>\n";
}
Default `perl -v`:
This is perl, v5.8.1-RC3 built for darwin-thread-multi-2level
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2003, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the GNU General Public License, which may be found in the Perl
5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to
the Internet, point your browser at http://www.perl.com/, the Perl Home
Page.
Custom `perl -v`:
This is perl, v5.8.4 built for darwin-2level
Copyright 1987-2004, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the GNU General Public License, which may be found in the Perl
5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to
the Internet, point your browser at http://www.perl.com/, the Perl Home
Page.
Cheers,
Tim Hammerquist
------------------------------
Date: 12 Jul 2004 06:00:20 -0700
From: jkeen_via_google@yahoo.com (Jim Keenan)
Subject: Re: MacPerl vs. Unix (OS X) Perl
Message-Id: <196cb7af.0407120500.64f3c2c7@posting.google.com>
Nathan Olson <nolson66@comcast.net> wrote in message news:<BD177587.14163%nolson66@comcast.net>...
> Any reason why the following behaves differently from MacPerl to Unix Perl
> (in OS 10.2.8)? (Assume all I want to process is the first file; hence the
> @ARGV[0].)
>
>
>
> open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
> @contents = <THISFILE>;
> close (THISFILE);
>
>
> In MacPerl, an ordinary text file is parsed into the @contents array with
> each paragraph being a element.
I don't have MacPerl, so I can't test this statement. But in general
reading a file into an array would result in each *line* -- or, to be
more precise, each *record* -- being a separate element of the array.
The ordinary English meaning of 'paragraph' is a series of lines
separated from another such series by a blank or whitespace-only line.
(See perldoc perlvar on $/.)
> But in OS X/Unix Perl, the entire contents
> of the text file is placed in @contents[0];
Did you mean to say: $contents[0] ??
------------------------------
Date: Mon, 12 Jul 2004 15:53:52 +0400
From: Nicolay Vasiliev <n.vasiliev@apatit.com>
Subject: Shareit information via Perl
Message-Id: <cctu4g$2va2$1@bull.east.ru>
Hello, there!
I am trying to connect into ShareIt system. I need to refresh my sales
information automatically. I know LWP is power to help me, but I try and
get nothing positive yet.
What I am doing:
#!/usr/bin/perl
use LWP::UserAgent;
use LWP::Simple;
use HTTP::Request::Common qw(POST);
use strict;
use warnings;
print "Content-type: text/html; charset=windows-1251\n\n";
$|++;
my $cont = get("https://secure.shareit.com/shareit/cp/login/index.html");
my ($uri, $sessid, $random);
if ($cont =~ /.*=(\d+)&.*=(\w+)"/g) {
$uri =
"https://secure.shareit.com/shareit/cp/login/index.html?sessionid=$1&random=$2";
($sessid, $random) = ($1, $2);
}
my $ua = LWP::UserAgent->new();
$ua->agent("Superzilla/v4.11 Platinum");
my $req = POST $uri,
[PUBLISHER_ID => "mylogin",
PASSWORD => "password"];
$cont = $ua->request($req)->as_string;
print $cont;
exit();
When I make POST and print then the variable $cont I get the login page
again. Where is a mistake?
Thank you in advance.
Nicolay A. Vasiliev
------------------------------
Date: Mon, 12 Jul 2004 13:23:44 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: Shareit information via Perl
Message-Id: <40f282d9$0$686$afc38c87@news.easynet.co.uk>
Nicolay Vasiliev wrote:
> When I make POST and print then the variable $cont I get the login page
> again. Where is a mistake?
There is no continuity between the get and the request. As far as the ShareIT
site is concerned the two request are two different sessions. I would enable
cookies in your user agent and use that to get your first page. That will
probably set a cookie that you require and then the second request will be seen
as part of the same session.
Least ways that's how we do it.
Having said that I have yet to play about with HTTPS in LWP so I don't know if
there are any other issues, like certificates.
------------------------------
Date: Mon, 12 Jul 2004 07:49:23 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: Useful one-liners and other short Perl scripts
Message-Id: <7orIc.73328$XM6.23735@attbi_s53>
John W. Krahn wrote:
>>This removes duplicate entries in the PATH environment variable:
>> exec perl -e'$ENV{PATH}=join":",grep{not$seen{$_}++}split/:/,$ENV{PATH};exec$ENV{SHELL}'
>
> Note that won't work with csh
It most certainly does work with csh.
> as it uses ' ' instead of ':' as aseparator.
The non-exported internal variable $path uses spaces.
The $PATH environment variable uses colons.
In perl, $ENV{PATH} is the latter.
-Joe
------------------------------
Date: Mon, 12 Jul 2004 09:02:41 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: what do you call funct ( funct())
Message-Id: <cctcu0$2i6$2@nntp.fujitsu-siemens.com>
Dale Henderson wrote:
>>>>>>"JM" =3D=3D Josef Moellers <josef.moellers@fujitsu-siemens.com> wri=
tes:
> JM> It then also bears the notion of two vectors being independent
> JM> of each other so they span a plane where you can construct
> JM> points out of both vectors independently, which probably led
> JM> to the adoption of this term in CS:
>=20
>=20
> Vectors needn't be orthogonal to be linearly independent. For
> example (0,1) and (1,1) are linearly independent and span the
> plane but are not orthogonal, <(0,1),(1,1)>=3D1!=3D0.
As I'm already known to be a pedant and trying to show off:
I was using an implication:
"at right angles" -> "linearly independency"
> This is all WAY off topic and further discussion should be moved
> to sci.math.
I don't really want to discuss this particular subject.
<EOD>
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
------------------------------
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 6794
***************************************