[31392] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2644 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 20 21:09:42 2009

Date: Tue, 20 Oct 2009 18:09: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           Tue, 20 Oct 2009     Volume: 11 Number: 2644

Today's topics:
        Getting current time in milliseconds <laredotornado@zipmail.com>
    Re: Getting current time in milliseconds <jimsgibson@gmail.com>
    Re: Getting current time in milliseconds <jurgenex@hotmail.com>
    Re: How would I do this in perl? <cartercc@gmail.com>
    Re: How would I do this in perl? <spamtrap@shermpendley.com>
    Re: How would I do this in perl? <ben@morrow.me.uk>
    Re: How would I do this in perl? <cwilbur@chromatico.net>
    Re: How would I do this in perl? <cartercc@gmail.com>
    Re: How would I do this in perl? <ben@morrow.me.uk>
    Re: How would I do this in perl? <nospam-abuse@ilyaz.org>
    Re: prototypes <glennj@ncf.ca>
    Re: prototypes <sreservoir@gmail.com>
    Re: prototypes <ben@morrow.me.uk>
    Re: prototypes <sreservoir@gmail.com>
    Re: prototypes <ben@morrow.me.uk>
    Re: Want to write a script to note specific IP addresse <glex_no-spam@qwest-spam-no.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 20 Oct 2009 15:13:40 -0700 (PDT)
From: laredotornado <laredotornado@zipmail.com>
Subject: Getting current time in milliseconds
Message-Id: <c117c8d9-0749-4552-8e26-5188353a1c0b@o9g2000prg.googlegroups.com>

Hi,

I'm on a Mac OS 10.5.6 with Perl 5.8.8.  From a shell script (/bin/
sh), do you know how I could use perl to get the current time in
milliseconds (or microseconds) and store the value in a shell
variable?

Thanks, - Dave


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

Date: Tue, 20 Oct 2009 15:31:01 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Getting current time in milliseconds
Message-Id: <201020091531019233%jimsgibson@gmail.com>

In article
<c117c8d9-0749-4552-8e26-5188353a1c0b@o9g2000prg.googlegroups.com>,
laredotornado <laredotornado@zipmail.com> wrote:

> Hi,
> 
> I'm on a Mac OS 10.5.6 with Perl 5.8.8.  From a shell script (/bin/
> sh), do you know how I could use perl to get the current time in
> milliseconds (or microseconds) and store the value in a shell
> variable?

% set t=`perl -MTime::HiRes=gettimeofday -e 'print
int(1000*gettimeofday()).qq(\n);'` ; echo Time in msec is $t
Time in msec is 1256077820486
%

-- 
Jim Gibson


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

Date: Tue, 20 Oct 2009 17:14:51 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Getting current time in milliseconds
Message-Id: <5dksd55heg8tlrcklst5agu7qsbibclinr@4ax.com>

laredotornado <laredotornado@zipmail.com> wrote:
>I'm on a Mac OS 10.5.6 with Perl 5.8.8.  From a shell script (/bin/
>sh), do you know how I could use perl to get the current time in
>milliseconds (or microseconds) 

perldoc Time::HiRes

> and store the value in a shell variable?

That is impossible because child processes cannot alter variables of the
parent process. See "perldoc -q env" for a possible workaround.

jue


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

Date: Tue, 20 Oct 2009 07:05:38 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: How would I do this in perl?
Message-Id: <bf93129a-eb6d-4914-a08f-a631ffc92e13@j9g2000vbp.googlegroups.com>

On Oct 16, 3:44=A0pm, Tad J McClellan <ta...@seesig.invalid> wrote:
> You have never mistyped a filename?

Who types filenames? I use Windows at work, and Windows has a nifty
shortcut that allows the insertion of filenames without typing them.

> You are so awesome!

Thank you very much -- you've made my day. ;-)

> checking the return value from open() is needed in every situation,
> because it can fail in any situation.

Maybe this is the point. It's NOT needed in every situation. We all
can think, in theory anyway, and we all can determine when to take
shortcuts and when to paint by the numbers. Yesterday I revisited a
script that I had written some time ago and in which I had used a
number of infamous Perl shortcuts, and which took me several minutes
to interpret -- and which I immediately rewrote in a literate
programming idiom because I needed to reuse the logic in what will
become a standard script.

It's not that using shortcuts is wrong, and that using literate
programming practices is right -- it just depends.

> I have open() with return value checking saved in an edit buffer, so
> I won't be tempted to spew bad code upon the world.

Now, that's a good idea.

> Code posted here is likely to be copied by posters days, weeks or
> years from now.
> Seeding failure is a disservice to our community.

Can't disagree with that.

> If you cannot be troubled to post Good Code, then don't post code,
> Perl programmers will be the better for it.

And what is 'good code?' I got slammed in another thread a few weeks
ago for not using typical shortcuts, such as using $_ in a block
instead of assigning $_ to $line and using $line in the block.
Readability and terseness are both virtues, but they mostly are
mutually exclusive. Example: I've been writing Lisp in my off time and
have learned that writing Lisp is easy because it's very abstract, but
reading Lisp is difficult because it's very abstract.

'Good code' is many times a matter of opinion, and we've got better
things to do than imposing our opinion as code police.

CC.


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

Date: Tue, 20 Oct 2009 11:37:37 -0400
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: How would I do this in perl?
Message-Id: <m2hbtu3wla.fsf@shermpendley.com>

ccc31807 <cartercc@gmail.com> writes:

> On Oct 16, 3:44 pm, Tad J McClellan <ta...@seesig.invalid> wrote:
>
>> checking the return value from open() is needed in every situation,
>> because it can fail in any situation.
>
> Maybe this is the point. It's NOT needed in every situation.

Error-checking isn't only for the situations of which we're already aware,
it's also for those we haven't anticipated. No programmer, no matter how
careful and skilled, can predict when a hard drive will fail, a backhoe
operator will cut a cable, or a tree will fall on a power line and black
out half the city.

Also, code can develop a life of its own. What was originally intended
as a quick-and-dirty prototype can be (and often is) pushed into use
far beyond its original purpose by managers who think "the prototype
works fine, why waste time re-writing it?"

We never know with 100% certainty when error-checking open() will be
necessary. The safe bet is to do it every time, even if you *think* you
have a situation where it's not needed.

sherm--


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

Date: Tue, 20 Oct 2009 16:42:49 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How would I do this in perl?
Message-Id: <pc21r6-h73.ln1@osiris.mauzo.dyndns.org>


Quoth ccc31807 <cartercc@gmail.com>:
> On Oct 16, 3:44 pm, Tad J McClellan <ta...@seesig.invalid> wrote:
> > You have never mistyped a filename?
> 
> Who types filenames? I use Windows at work, and Windows has a nifty
> shortcut that allows the insertion of filenames without typing them.

Umm... OK. You've never written a script to create a file that didn't
exist yet?

> > checking the return value from open() is needed in every situation,
> > because it can fail in any situation.
> 
> Maybe this is the point. It's NOT needed in every situation. We all
> can think, in theory anyway, and we all can determine when to take
> shortcuts and when to paint by the numbers. Yesterday I revisited a
> script that I had written some time ago and in which I had used a
> number of infamous Perl shortcuts, and which took me several minutes
> to interpret -- and which I immediately rewrote in a literate
> programming idiom because I needed to reuse the logic in what will
> become a standard script.

I don't think you know what 'literate programming' means. AFAIK there
are no LP tools for Perl. (They are mostly unnecessary, of course, since
Perl is a higher-level language than Pascal.)

> > I have open() with return value checking saved in an edit buffer, so
> > I won't be tempted to spew bad code upon the world.
> 
> Now, that's a good idea.

    use autodie;

is a better. I'm seriously considering adding it to my standard 'strict'
and 'warnings'.

> > If you cannot be troubled to post Good Code, then don't post code,
> > Perl programmers will be the better for it.
> 
> And what is 'good code?' I got slammed in another thread a few weeks
> ago for not using typical shortcuts, such as using $_ in a block
> instead of assigning $_ to $line and using $line in the block.

IIRC you were criticised for writing

    $_ =~ s///;

which is redundant and confusing. If you want a real variable, give it a
real name; if you're going to use $_, use the shortcuts that go with it.

(Oh, and, IMHO assignments to $_ are a code smell. for () {} is usually
a better solution. given would be still better if it weren't crippled.)

> Readability and terseness are both virtues, but they mostly are
> mutually exclusive.

I strongly disagree. Verbosity can be just as confusing in a programming
language as it is in a natural language. The aim should always be to
make it clear what the code is doing and why, with as little extraneous
mess as you can manage. Sometimes this means spelling things out;
sometimes an idiom like

    s/\s+$// for @lines;

is much easier to understand than something like

    for my $i (0..$#lines) {
        $lines[$i] =~ s/\s+$//;
    }

(and even that has at least two constructions that would be completely
bewildering to someone just starting to learn Perl.).

> Example: I've been writing Lisp in my off time and
> have learned that writing Lisp is easy because it's very abstract, but
> reading Lisp is difficult because it's very abstract.

AIUI reading (well-written) Lisp is supposed to be easy, because it's
very abstract. However, I don't know Lisp, so I can't really say.

Ben



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

Date: Tue, 20 Oct 2009 12:08:49 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: How would I do this in perl?
Message-Id: <86eioyrqsu.fsf@mithril.chromatico.net>

>>>>> "cc" == ccc31807  <cartercc@gmail.com> writes:

    cc> me several minutes to interpret -- and which I immediately
    cc> rewrote in a literate programming idiom because I needed to

"Literate programming" does not mean what you apparently think it means.

    cc> And what is 'good code?' I got slammed in another thread a few
    cc> weeks ago for not using typical shortcuts, such as using $_ in a
    cc> block instead of assigning $_ to $line and using $line in the
    cc> block.  Readability and terseness are both virtues, but they
    cc> mostly are mutually exclusive.

Except when they're not.

Using $_ idiomatically is terse and readable.  Providing a variable name
to alias is also idiomatic, though less terse, and also reasonable.
*Assigning* $_ is almost never idiomatic; I'd say "never," except that
if I did so, someone would come up with a rare situation where it is
idiomatic.

The correct solution for someone who cannot follow using $_
or @_ idiomatically because he or she is new to Perl is not to eliminate
$_ and @_, but to explain the use of $_ and @_ so that he or she can
learn about idiomatic Perl.  The audience for my code, simply put, is
not Perl novices.

    cc> 'Good code' is many times a matter of opinion, and we've got
    cc> better things to do than imposing our opinion as code police.

We're not talking only aesthetics, which are a matter of opinion --
we're also talking best practices versus sloppy practices.

There is no reasonable argument that not checking the return value of
open is a sloppy practice.  Do it in quick-and-dirty scripts if you must,
although typing 'or die "error: $!"' involves substantially less effort
than you've put into the defense so far; just don't delude yourself
that it's acceptable to post that quick-and-dirty code to Usenet.

And if you do persist in the delusion -- which looks likely, because
this is not the first time we've wrangled this out with you -- then at
least accept that if you persist in posting sloppy code, we will persist
in correcting it and commenting on it.

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Tue, 20 Oct 2009 09:15:21 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: How would I do this in perl?
Message-Id: <c51f730a-62f0-4e77-b950-e892e94a7afc@k26g2000vbp.googlegroups.com>

On Oct 20, 11:42=A0am, Ben Morrow <b...@morrow.me.uk> wrote:
> Umm... OK. You've never written a script to create a file that didn't
> exist yet?

I write them all the time, usually in the context of creating a report
from a data file. This might be hard to believe, but in my experience
open() usually fails when a file is locked (e.g., by Excel), and those
kinds of error messages are pretty clear.

> I don't think you know what 'literate programming' means. AFAIK there
> are no LP tools for Perl. (They are mostly unnecessary, of course, since
> Perl is a higher-level language than Pascal.)

I understand 'literate programming' to mean writing code as much as
possible like natural language, using self-documenting names, using
abstractions to hide the details, as so forth.

> =A0 =A0 use autodie;

Not familiar with this, but I'll check it out.

> > Readability and terseness are both virtues, but they mostly are
> > mutually exclusive.
>
> I strongly disagree. Verbosity can be just as confusing in a programming
> language as it is in a natural language. The aim should always be to
> make it clear what the code is doing and why, with as little extraneous
> mess as you can manage. Sometimes this means spelling things out;
> sometimes an idiom like
>
> =A0 =A0 s/\s+$// for @lines;
>
> is much easier to understand than something like
>
> =A0 =A0 for my $i (0..$#lines) {
> =A0 =A0 =A0 =A0 $lines[$i] =3D~ s/\s+$//;
> =A0 =A0 }
>
> (and even that has at least two constructions that would be completely
> bewildering to someone just starting to learn Perl.).

Not to mention 'common' shortcuts, like using <> alone to grab the
header of a file:
my $header =3D <DATAFILE>;
or using ||=3D to check for initialization of a variable:
my $header ||=3D $_;

I would write the above as:
foreach my $line (@lines) { $line =3D~ s/\s+//; }


> AIUI reading (well-written) Lisp is supposed to be easy, because it's
> very abstract. However, I don't know Lisp, so I can't really say.

My trouble with reading Lisp is that the style seems to promote the
multiplication of small functions that are deeply nested, so that it's
easy to get confused tracing all the calls. It's very easy to create
and test functions on the top level, so you write a myriad of very
small functions and use them to build a more complicated structure ---
but just try to revisit your code after a month and untangle it.

CC


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

Date: Tue, 20 Oct 2009 19:08:14 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How would I do this in perl?
Message-Id: <eta1r6-iu3.ln1@osiris.mauzo.dyndns.org>


Quoth ccc31807 <cartercc@gmail.com>:
> On Oct 20, 11:42 am, Ben Morrow <b...@morrow.me.uk> wrote:
> > Umm... OK. You've never written a script to create a file that didn't
> > exist yet?
> 
> I write them all the time, usually in the context of creating a report
> from a data file. This might be hard to believe, but in my experience
> open() usually fails when a file is locked (e.g., by Excel), and those
> kinds of error messages are pretty clear.

Most of us are working on systems where that isn't even an issue, yet we
still check our opens. You don't check return values because of what
'usually' happens, you check them so that when program gets called from
cron at 4 in the morning and open fails because the disk is full, it
doesn't do something deeply stupid.

> > I don't think you know what 'literate programming' means. AFAIK there
> > are no LP tools for Perl. (They are mostly unnecessary, of course, since
> > Perl is a higher-level language than Pascal.)
> 
> I understand 'literate programming' to mean writing code as much as
> possible like natural language, using self-documenting names, using
> abstractions to hide the details, as so forth.

No. http://www.literateprogramming.com/knuthweb.pdf .

> >     use autodie;
> 
> Not familiar with this, but I'll check it out.

It's a lexically-scoped replacement for Fatal, with (much) nicer error
messages.

    ~% perl -Mautodie -e'open my $F, "<", "/not/there"'
    Can't open '/not/there' for reading: 'No such file or directory' at
    -e line 1

Ben



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

Date: Tue, 20 Oct 2009 23:16:08 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: How would I do this in perl?
Message-Id: <slrnhdsh5o.uaa.nospam-abuse@chorin.math.berkeley.edu>

On 2009-10-20, Ben Morrow <ben@morrow.me.uk> wrote:
> IIRC you were criticised for writing
>
>     $_ =~ s///;
>
> which is redundant

No way!  (Unless you talk about 6 leftmost characters.)

> and confusing.

That's right.  So confusing that it confused even you...  (Or, maybe,
me - depending on what you meant.  ;-)

Ilya


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

Date: 20 Oct 2009 11:04:14 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: prototypes
Message-Id: <slrnhdr69e.qer.glennj@smeagol.ncf.ca>

At 2009-10-20 04:38AM, "Ben Morrow" wrote:
>      sub hpush (\%%) {
>          my ($h1, %h2) = @_;
>          %$h1 = (%$h1, %$h2);

That should be:
           %$h1 = (%$h1, %h2);

>          return scalar keys %$h1;
>      }
>  
>  which will allow
>  
>      hpush %hash, %h2;
>      hpush %hash, foo => 1, bar => 2;

-- 
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous


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

Date: Tue, 20 Oct 2009 16:56:40 -0400
From: sreservoir <sreservoir@gmail.com>
Subject: Re: prototypes
Message-Id: <hbl85m$m9b$1@aioe.org>

Ben Morrow wrote:
> Quoth ace <ace@invalid.com>:
>> I would like to make a prototype for hpush subroutine which then can be
>> used like:
>>
>> hpush %hash, %h2;
>> hpush %hash, {foo => 1};
> 
> You can't do that with prototypes.
> 
>> Currently I have to dereference the hash which looks quite ugly:
>>
>> hpush %hash, %{{foo => 1}};
> 
> The best answer here is
> 
>     sub hpush (\%%) {
>         my ($h1, %h2) = @_;
>         %$h1 = (%$h1, %$h2);
>         return scalar keys %$h1;
>     }

Note that a % in the prototype will still allow odd-numbered arguments.

-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


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

Date: Tue, 20 Oct 2009 22:39:00 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: prototypes
Message-Id: <k8n1r6-3r4.ln1@osiris.mauzo.dyndns.org>


Quoth sreservoir <sreservoir@gmail.com>:
> Ben Morrow wrote:
> > 
> >     sub hpush (\%%) {
> >         my ($h1, %h2) = @_;
> >         %$h1 = (%$h1, %$h2);
> >         return scalar keys %$h1;
> >     }
> 
> Note that a % in the prototype will still allow odd-numbered arguments.

Yes. % in a prototype is in every way equivalent to @.

The assignment will give a warning, and that can trivially be replaced
with a (more helpful) error with

    sub hpush (\%%) {
        @_ % 2 or croak "Odd number of elements in hpush";

In general trying to to compile-time argument checking is futile in Perl
5: the language is just too dynamic. Perl 6 is making a serious attempt
to fix that, at least where it's useful to do so.

Ben



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

Date: Tue, 20 Oct 2009 18:07:20 -0400
From: sreservoir <sreservoir@gmail.com>
Subject: Re: prototypes
Message-Id: <hblca6$q8f$1@aioe.org>

Ben Morrow wrote:
> Quoth sreservoir <sreservoir@gmail.com>:
>> Ben Morrow wrote:
>>>     sub hpush (\%%) {
>>>         my ($h1, %h2) = @_;
>>>         %$h1 = (%$h1, %$h2);
>>>         return scalar keys %$h1;
>>>     }
>> Note that a % in the prototype will still allow odd-numbered arguments.
> 
> Yes. % in a prototype is in every way equivalent to @.
> 
> The assignment will give a warning, and that can trivially be replaced
> with a (more helpful) error with
> 
>     sub hpush (\%%) {
>         @_ % 2 or croak "Odd number of elements in hpush";

that croak should be:
	@_ % 2 and croak ...

@_ % 2 == 1 if @_ is odd, 0 if even.


> In general trying to to compile-time argument checking is futile in Perl
> 5: the language is just too dynamic. Perl 6 is making a serious attempt
> to fix that, at least where it's useful to do so.

Yet another reason for perl6!

-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


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

Date: Tue, 20 Oct 2009 23:34:02 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: prototypes
Message-Id: <qfq1r6-845.ln1@osiris.mauzo.dyndns.org>


Quoth sreservoir <sreservoir@gmail.com>:
> Ben Morrow wrote:
> >
> > The assignment will give a warning, and that can trivially be replaced
> > with a (more helpful) error with
> > 
> >     sub hpush (\%%) {
> >         @_ % 2 or croak "Odd number of elements in hpush";
> 
> that croak should be:
> 	@_ % 2 and croak ...

No it shouldn't.

> @_ % 2 == 1 if @_ is odd, 0 if even.

Yes... 

(think about it more carefully :))

Ben



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

Date: Tue, 20 Oct 2009 13:20:18 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Want to write a script to note specific IP addresses.
Message-Id: <4addf4c5$0$1326$815e3792@news.qwest.net>

Hongyi Zhao wrote:
> Hi all,
> 
> I want to write a script to note specific IP
>  addresses by appending the corresponding location informations.  For
> detail, I describe my issue as follows:
> 
> Suppose I have two files, the first file is used to store the specific
> IP
>  addresses which I want to note, and the second file is used to store
> the IP database along with the corresponding location informations.
> 
> The first file has one IP address per line with dotted decimal format,
> e.g.:
> 
> 0.125.125.125
> 4.19.79.28
> 4.36.124.150
> ...
> 
> The second file has four field per line delimited by CHARACTER
> TABULATION (U+0009).  These four field are: StartIP, EndIP, Country,
> and Local, e.g.:
> 
> StartIP	EndIP	Country	Local
> 0.0.0.0	0.255.255.255	IANA	CZ88.NET
> 4.19.79.0	4.19.79.63	American	Armed Forces
> Radio/Television
> 4.36.124.128	4.36.124.255	American	Technical Resource
> Connections Inc
> ...
> 
> Based on the second file, I want to reformat the first file by
> appending the corresponding location informations for each IP address
> in it, i.e., for the above example, I want to obain the following
> result:
> 
> 0.125.125.125#IANA CZ88.NET
> 4.19.79.28#American Armed Forces Radio/Television
> 4.36.124.150#American Technical Resource Connections
> ...
> 
> Any hints on this issue will be highly appreciated.

Quite a few modules that help with IPs, however
you could translate the IPs to integers and simply
check if one IP is between the start and end.

Place to start:

use Net::Netmask qw(quad2int);


To open/read/process files:

perldoc -f open
perldoc perlopentut


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

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 2644
***************************************


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