[26089] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8289 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 29 11:05:34 2005

Date: Fri, 29 Jul 2005 08:05:09 -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           Fri, 29 Jul 2005     Volume: 10 Number: 8289

Today's topics:
        comparing two dates <""alexjaquet\"@[no spam]msn.com">
    Re: comparing two dates <xyz@tnecul.moc.invalid>
        Old C dog. <zzzz@zzzz.com>
    Re: Old C dog. <klaus_gb@yahoo.com>
        Quick and easy way to check a port <nomail@hursley.ibm.com>
    Re: Quick and easy way to check a port <lv@aol.com>
    Re: slow substr? <tassilo.von.parseval@rwth-aachen.de>
    Re: true false ? : expression thingy <Juha.Laiho@iki.fi>
    Re: true false ? : expression thingy <sven-thorsten.fahrbach@gmx.net>
    Re: true false ? : expression thingy <tadmc@augustmail.com>
    Re: true false ? : expression thingy <sven-thorsten.fahrbach@gmx.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 29 Jul 2005 14:02:45 +0200
From: Alexandre Jaquet <""alexjaquet\"@[no spam]msn.com">
Subject: comparing two dates
Message-Id: <42ea1aee$0$1162$5402220f@news.sunrise.ch>

HI,

Anyone know how to compare two dates with time. I have a datetime field 
in my db and want to compare it with current date time.

		 
($ARTICLE{'counter'},$ARTICLE{'enchere_date_fin'})=sqlSelect("nbr_enchere,enchere_date_fin", 
"article", "id_article = '$article'");
$ARTICLE{'counter'} += 1;

local our  $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();
local our  $date = sprintf "%4d-%02d-%02d",$year+1900,$mon+1,$mday;	
local our  $time = sprintf("%4d:%02d:%02d",$hour,$min,$sec);
#current date time $date $time

thanks


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

Date: Fri, 29 Jul 2005 08:16:58 -0400
From: "T W Hu" <xyz@tnecul.moc.invalid>
Subject: Re: comparing two dates
Message-Id: <dcd6ns$t5t@netnews.net.lucent.com>

> Anyone know how to compare two dates with time. I have a datetime field
> in my db and want to compare it with current date time.
[snip]
Date::Calc
Delta_Days, Delta_DHMS, etc




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

Date: Fri, 29 Jul 2005 08:10:23 -0500
From: Kent Feiler <zzzz@zzzz.com>
Subject: Old C dog.
Message-Id: <ko9ke190v66g2957u6u3uofsqeht0r2o0h@4ax.com>

I'm a long-time C programmer starting on Perl. I always begin by
assuming things are C-ish and discover that sometimes they aren't.
Here's a statement I'm wondering about that seems to be all over the
place in Perl. Generically:

                        $a = b() or c();

Does the "or" check an internal Perl program field, or the final value
of $a, or the return from b()? I think I'd understand it if someone
would tell me whether z() is executed in the following statements:

                        $a = 0 or z();

Is that the same as:

                        if ( $a = 0 ) {
                           z();
                           }

 ...and how about the simple:

                         a() or z();

Or am I still missing the point?


Regards,
               
Regards,


Kent Feiler
www.KentFeiler.com


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

Date: Fri, 29 Jul 2005 15:51:36 +0100
From: "Klaus Eichner" <klaus_gb@yahoo.com>
Subject: Re: Old C dog.
Message-Id: <42ea32ae$0$5048$636a15ce@news.free.fr>

"Kent Feiler" <zzzz@zzzz.com> wrote in message
news:ko9ke190v66g2957u6u3uofsqeht0r2o0h@4ax.com...
> I'm a long-time C programmer starting on Perl. I always begin by
> assuming things are C-ish and discover that sometimes they aren't.
> Here's a statement I'm wondering about that seems to be all over the
> place in Perl. Generically:
>
>                         $a = b() or c();
>
> Does the "or" check an internal Perl program field, or the final value
> of $a, or the return from b()?

the equivalent expression with brackets:
( $a = b() ) or c();
see "perldoc perlop" -> "Logical or and Exclusive Or"

it calls subroutine b() and assigns the value returned by b() to variable
$a. If that value is logically false, i.e. numerically 0, '0', the empty
string '' or undef, then, and only then, subroutine c() is called. Any value
which might be returned by c() will be ignored.

> I think I'd understand it if someone
> would tell me whether z() is executed in the following statements:
>
>                         $a = 0 or z();
>
> Is that the same as:
>
>                         if ( $a = 0 ) {
>                            z();
>                            }

No, it's the same as:
                        if ( ! ( $a = 0 ) ) {
                           z();
                           }

or...
                        unless($a = 0) {
                           z();
                           }

or even ...
                        z() unless $a = 0;

> ...and how about the simple:
>
>                          a() or z();

This simply calls subroutine a(). If the value returned by a() is logically
false, then subroutine z() is called. Another way of achieving the same
result would be:

if (!a()) { z() }

or

unless (a()) { z() }

or

z() unless a();

> Or am I still missing the point?

I don't think so.
But I would recommend to read the standard perl-documentation, in particular
"perldoc perlop" and, if you have a background in C programming, read
"perldoc perltrap" --> "C/C++ Traps".

-- 
Klaus




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

Date: Fri, 29 Jul 2005 13:25:57 +0100
From: Derek Fountain <nomail@hursley.ibm.com>
Subject: Quick and easy way to check a port
Message-Id: <42ea2065$0$40320$892e7fe2@authen.white.readfreenews.net>

I'm starting up a child process which, after a delay of a few seconds, 
opens a port and waits for data on it. I want my main script - the 
parent - to pause until the child is ready. I don't have access to the 
source for the child, so I figured the best thing to do is wait until 
the port goes ready.

Only, I'm not sure how to do that. I could system() out to netstat on 
the Linux box I'm working on, but really need something more platform 
independent - it should work on any form of *NIX if possible. Oh, and I 
don't want to upset the child process, which is basically does: listen, 
accept data, close, done.

Is there some platform independent way of checking a port is open 
without actually trying to open it then closing it on success?


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

Date: Fri, 29 Jul 2005 07:20:44 -0500
From: l v <lv@aol.com>
Subject: Re: Quick and easy way to check a port
Message-Id: <42ea2e37$1_2@spool9-west.superfeed.net>

Derek Fountain wrote:
> I'm starting up a child process which, after a delay of a few seconds, 
> opens a port and waits for data on it. I want my main script - the 
> parent - to pause until the child is ready. I don't have access to the 
> source for the child, so I figured the best thing to do is wait until 
> the port goes ready.
> 
> Only, I'm not sure how to do that. I could system() out to netstat on 
> the Linux box I'm working on, but really need something more platform 
> independent - it should work on any form of *NIX if possible. Oh, and I 
> don't want to upset the child process, which is basically does: listen, 
> accept data, close, done.
> 
> Is there some platform independent way of checking a port is open 
> without actually trying to open it then closing it on success?

Look at IO::Socket::INET

Len

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


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

Date: Fri, 29 Jul 2005 12:46:45 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: slow substr?
Message-Id: <slrndek28l.3gd.tassilo.von.parseval@localhost.localdomain>

Also sprach bugbear:

> I'm attempting to write some IO layers, in
> an object oriented style.
>
> I had some initial performance problems
> in a READLINE implementation, because
> I was reading bytes 1 at a time from
> the underlying layer.
>
> Working 1 byte at a time in perl is death
> on multi-megabyte files.
>
> So I implemented a base class to provide buffering.
>
> It's not anything special, but works OK, and gave
> me a nice speed up (factor of 10).
>
> However, 50% of the time is not spent
> in a class utility; when a method
> has decided what to take from the class
> buffer, it calls this:
>
> sub use {
>          my ($self, $l) = @_;
>          $self->{buf} = substr($self->{buf}, $l);
> }
>
> Which simply notes the fact that $l bytes have been consumed.
>
> As I understand it, substr is a very simple internal
> "book keeping" method in perl, that shouldn't need to allocate
> memory, or copy memory.

In a case like this it will not allocate new memory but it will at
least copy bytes around using memmove(3). In the source I find no
indication that substr() uses an optimized approach for those cases
where a string is only truncated.

Tassilo
-- 
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);


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

Date: Fri, 29 Jul 2005 11:30:26 +0000 (UTC)
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: true false ? : expression thingy
Message-Id: <dcd40i$3o1$1@ichaos.ichaos-int>

"Bigus" <someone@somewhere.com> said:
>"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message 
>news:dcaenn$d40$1@mamenchi.zrz.TU-Berlin.DE...
>my $incsubs;
>$line =~ /\ts/i ? ($incsubs = 0) : ($incsubs = 1);
>
>> Generally, the "?:" construct (sometimes called "ternary conditional",
>> btw) should not be used for operations that have a side effect, like
>> the assignments you are doing.  Use a normal if/else for that, even if
>> it's a bit longer.
>>
>> Using "?:", your conditional assignment can be written:
>>
>>    my $incsubs = $line =~ /\ts/i ? 0 : 1;
>>
>> Now the assignment is outside the "?:".  But now it becomes apparent
>> that this is (almost) equivalent to
>>
>>    my $incsubs = $line !~ /\ts/i;
>
>OK, that sounds good. I 'll use that in this case. The previous syntax would 
>be useful in situations where I want to make a non boolean assigment 
>resulting from a regexp match.

Hmm.. somewhat yes; a non-boolean assignment based on whether a regexp
did match or not. But then, if you assign constant values, you're after
all assigning "a form of boolean" (that is, after the operation the
variable will have one of two possible values, and later on something
will depend on which of the two values was assigned).

So, cases where you really have a use for this shorthand are not that
common (esp. given the overall syntactic flexibility of perl), and
when you finally have a case complex enough to warrant this, it might
be at the same time complex enough to warrant a full-blown conditional
construct.
-- 
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
         PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)


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

Date: Fri, 29 Jul 2005 13:46:00 +0200
From: Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net>
Subject: Re: true false ? : expression thingy
Message-Id: <20050729134600.189c928e.sven-thorsten.fahrbach@gmx.net>

On Fri, 29 Jul 2005 10:04:54 +0000 (UTC)
Ian Wilson <scobloke2@infotop.co.uk> wrote:

> Bigus wrote:
> > when you want a
> > 1-line alternative to an if...else.. construct,
> 
> Whilst I quite like
>    $this = condition ? "foo" : "bar";
> 
> What's so horrible about
>    if (condition) {$this="foo";} else {$this="bar"}
> which I find quite readable?
> 

The ternary operator looks wonderfully geeky ;-). But I agree with you, you should use if ... else for more sophisticated conditionals, for your own sake and your co-workers'.


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

Date: Fri, 29 Jul 2005 08:42:32 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: true false ? : expression thingy
Message-Id: <slrndekci8.5gt.tadmc@magna.augustmail.com>

Ian Wilson <scobloke2@infotop.co.uk> wrote:
> Bigus wrote:
>> when you want a
>> 1-line alternative to an if...else.. construct,
> 
> Whilst I quite like
>    $this = condition ? "foo" : "bar";
> 
> What's so horrible about


"horrible" is prejudicial, putting the question in a more reasoned manner:

   Why prefer it over


>    if (condition) {$this="foo";} else {$this="bar"}


As with most style issues, the choice should be based on clearly
communicating the code's intent.

A commonly accepted style-choice here is to use the 1st for a
conditional _value_ and the 2nd for conditional flow control.

So the 1st would be preferred _here_, because ?: (should) always means
"select a value", while you have to analyse the 2nd only to come
to what would be a foregone conclusion if the 1st had been used instead.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 29 Jul 2005 16:55:51 +0200
From: Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net>
Subject: Re: true false ? : expression thingy
Message-Id: <20050729165551.1f4e2d8c.sven-thorsten.fahrbach@gmx.net>

On Fri, 29 Jul 2005 08:42:32 -0500
Tad McClellan <tadmc@augustmail.com> wrote:

> As with most style issues, the choice should be based on clearly
> communicating the code's intent.
> 
> A commonly accepted style-choice here is to use the 1st for a
> conditional _value_ and the 2nd for conditional flow control.
> 
> So the 1st would be preferred _here_, because ?: (should) always means
> "select a value", while you have to analyse the 2nd only to come
> to what would be a foregone conclusion if the 1st had been used instead.

Good point. I think a good illustration is the following code:

	printf ("\$foo is %s.\n", $foo ? "true" : "false");

This is (in my opinion) even more concise than:

	if ($foo) {
	    print "\$foo is true.\n";
	}
	else {
	    print "\$foo is false.\n";
	}

You shouldn't use the ternary operator if you're dealing with something like:

	if (($condition1) && ($value1 == $value2) && (! $condition2)) {
	    # long piece of code
	}
	else {
	    # another long piece of code
	}

This may sound trivial though, I think one should rely on common sense as when to use the two conditionals.


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

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 8289
***************************************


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