[31406] in Perl-Users-Digest
Perl-Users Digest, Issue: 2658 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 29 11:09:42 2009
Date: Thu, 29 Oct 2009 08:09: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 Thu, 29 Oct 2009 Volume: 11 Number: 2658
Today's topics:
How to reverse date? <nobody@nowhere.com>
Re: How to reverse date? sln@netherlands.com
Re: How to reverse date? <jimsgibson@gmail.com>
Re: How to reverse date? <jurgenex@hotmail.com>
Re: How to reverse date? <kkeller-usenet@wombat.san-francisco.ca.us>
Re: How to reverse date? <jurgenex@hotmail.com>
How to supress output of format / write? <jegan473@comcast.net>
Re: How to supress output of format / write? <jegan473@comcast.net>
Re: How to supress output of format / write? <ben@morrow.me.uk>
Re: Net::SFTP Fails, sftp OK <derykus@gmail.com>
Re: software engineering, program construction <rvtol+usenet@xs4all.nl>
Re: software engineering, program construction <cartercc@gmail.com>
Re: software engineering, program construction <cartercc@gmail.com>
Re: software engineering, program construction <jurgenex@hotmail.com>
tk window resizing <robin1@cnsp.com>
Re: tk window resizing <ben@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 28 Oct 2009 22:56:17 GMT
From: nobody <nobody@nowhere.com>
Subject: How to reverse date?
Message-Id: <l_3Gm.94178$Xw3.58949@en-nntp-04.dc1.easynews.com>
If I have a date in the format YYYYMMDD like:
20090329
How can I change that to:
03-29-2009
-Thanks!
------------------------------
Date: Wed, 28 Oct 2009 16:02:24 -0700
From: sln@netherlands.com
Subject: Re: How to reverse date?
Message-Id: <4ajhe596rfki7n4bhd82tjlgag8sr4ke2b@4ax.com>
On Wed, 28 Oct 2009 22:56:17 GMT, nobody <nobody@nowhere.com> wrote:
>If I have a date in the format YYYYMMDD like:
>
>20090329
>
>How can I change that to:
>
>03-29-2009
>
>
>-Thanks!
$date = "20090329";
$date =~ s/(\d{4})(\d{2})(\d{2})/$2-$3-$1/;
print $date;
-sln
------------------------------
Date: Wed, 28 Oct 2009 16:19:20 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: How to reverse date?
Message-Id: <281020091619208754%jimsgibson@gmail.com>
In article <l_3Gm.94178$Xw3.58949@en-nntp-04.dc1.easynews.com>, nobody
<nobody@nowhere.com> wrote:
> If I have a date in the format YYYYMMDD like:
>
> 20090329
>
> How can I change that to:
>
> 03-29-2009
One way:
my $s = '20090329';
if( $s =~ /(\d{4})(\d{2})(\d{2})/ ) {
$s = "$2-$3-$1";
}
You could also use substr.
--
Jim Gibson
------------------------------
Date: Wed, 28 Oct 2009 16:35:56 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to reverse date?
Message-Id: <d3lhe5pfru5esfbrefrravu3krivun1095@4ax.com>
nobody <nobody@nowhere.com> wrote:
>If I have a date in the format YYYYMMDD like:
>
>20090329
Perfect. ISO-Standard, non-ambiguous, easy to sort as text as well as as
number. Can't get any better than that.
>How can I change that to:
>
>03-29-2009
What's the twentyninth month of a year? ;-)
Anyway, I would use a capturing RE with 3 groups for 4, 2 and 2
characters (no need to bother with digits because you already know that
those are digits).
And then just $s="$3-$2-#1";
jue
------------------------------
Date: Wed, 28 Oct 2009 19:17:27 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: How to reverse date?
Message-Id: <nianr6xkdu.ln2@goaway.wombat.san-francisco.ca.us>
On 2009-10-28, Jürgen Exner <jurgenex@hotmail.com> wrote:
>
> What's the twentyninth month of a year? ;-)
Icosanovember? :)
--keith
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
------------------------------
Date: Thu, 29 Oct 2009 05:34:38 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to reverse date?
Message-Id: <2l2je55rrobnrgc8napaqs95v16m74kar3@4ax.com>
Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid> wrote:
>In <d3lhe5pfru5esfbrefrravu3krivun1095@4ax.com>, on 10/28/2009
> at 04:35 PM, Jürgen Exner <jurgenex@hotmail.com> said:
>
>>Anyway, I would use a capturing RE with 3 groups for 4, 2 and 2
>>characters (no need to bother with digits because you already know that
>>those are digits).
>
>Does he? Sound programming practice is to validate your input. I concur
>with the posters who used "\d" rather then ".".
Well, in general I agree. However the OP stated explicitely "If I have a
date in the format [...]".
To me this implies that he already validated the data by some means,
including e.g. making sure that there is no 30th of February or 63rd of
the 92nd month. No need to do it twice.
jue
------------------------------
Date: Wed, 28 Oct 2009 23:12:13 GMT
From: James Egan <jegan473@comcast.net>
Subject: How to supress output of format / write?
Message-Id: <hd4Gm.115543$uO.102597@en-nntp-09.dc1.easynews.com>
Lets say the $servers[1] variable below does NOT contain data. But the
$jobs[1] variable DOES contain data. Since the $servers[1] variable does
not contain data, I want to suppress the output of $servers[1] AND $jobs
[1]. Is this possible?
.
format STDOUT =
@<<<<<<< @<<<<<<<
$servers[0], $jobs[0]
@<<<<<<< @<<<<<<<
$servers[1], $jobs[1]
@<<<<<<< @<<<<<<<
$servers[2], $jobs[2]
'
------------------------------
Date: Wed, 28 Oct 2009 23:46:51 GMT
From: James Egan <jegan473@comcast.net>
Subject: Re: How to supress output of format / write?
Message-Id: <LJ4Gm.126209$BL3.80977@en-nntp-08.dc1.easynews.com>
On Wed, 28 Oct 2009 23:12:13 +0000, James Egan wrote:
> Lets say the $servers[1] variable below does NOT contain data. But the
> $jobs[1] variable DOES contain data. Since the $servers[1] variable
> does not contain data, I want to suppress the output of $servers[1] AND
> $jobs [1]. Is this possible?
>
>
> .
> format STDOUT =
> @<<<<<<< @<<<<<<<
> $servers[0], $jobs[0]
> @<<<<<<< @<<<<<<<
> $servers[1], $jobs[1]
> @<<<<<<< @<<<<<<<
> $servers[2], $jobs[2]
> '
I just read in the perldoc perlform that you can just insert a tilde ~ on
the line like:
.
format STDOUT =
@<<<<<<< @<<<<<<<
$servers[0], $jobs[0]
@<<<<<<< @<<<<<<<
~$servers[1], $jobs[1]
@<<<<<<< @<<<<<<<
$servers[2], $jobs[2]
.
------------------------------
Date: Wed, 28 Oct 2009 23:41:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to supress output of format / write?
Message-Id: <0e1nr6-tqt2.ln1@osiris.mauzo.dyndns.org>
Quoth James Egan <jegan473@comcast.net>:
> Lets say the $servers[1] variable below does NOT contain data. But the
> $jobs[1] variable DOES contain data. Since the $servers[1] variable does
> not contain data, I want to suppress the output of $servers[1] AND $jobs
> [1]. Is this possible?
I have no idea if this is possible with perl 4 formats, but I suspect
not. I would always use the Perl6::Form module instead (which actually
has nothing to do with Perl 6).
Ben
------------------------------
Date: Thu, 29 Oct 2009 04:01:01 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Net::SFTP Fails, sftp OK
Message-Id: <32e46c1a-8604-470e-a122-530213e20f7c@m33g2000pri.googlegroups.com>
On Oct 28, 9:46=A0am, MaggotChild <hsomob1...@yahoo.com> wrote:
> This just fails unexpectedly. With sftp I can connect no problem:
>
> =A0perl -MNet::SFTP -le'$ftp =3D Net::SFTP->new("sftp.xj999.com", user =
=3D>
> "X", password =3D> "Z", debug =3D> 1); print $_->{filename} for $ftp->ls;=
'
> localhost: Reading configuration data /mounts/sysadm/xj/.ssh/config
> localhost: Reading configuration data /etc/ssh_config
> localhost: Connecting to sftp.xj999.com, port 22.
> localhost: Remote protocol version 2.0, remote software version
> 5.1.3.8 SSH Tectia Server
> localhost: Net::SSH::Perl Version 1.34, protocol version 2.0.
> localhost: No compat match: 5.1.3.8 SSH Tectia Server
> localhost: Connection established.
> localhost: Sent key-exchange init (KEXINIT), wait response.
> localhost: Algorithms, c->s: 3des-cbc hmac-sha1 none
> localhost: Algorithms, s->c: 3des-cbc hmac-sha1 none
> localhost: Entering Diffie-Hellman Group 1 key exchange.
> Broken Pipe
>
> I had some trouble installing Net::SFTP, so I figured =A0maybe it was
> the install but, on another machine:
>
> localhost: Reading configuration data /home/xj/.ssh/config
> localhost: Reading configuration data /etc/ssh_config
> localhost: Connecting to sftp.xj999.com, port 22.
> localhost: Remote version string: SSH-2.0-5.1.3.8 SSH Tectia Server
>
> localhost: Remote protocol version 2.0, remote software version
> 5.1.3.8 SSH Tectia Server
> localhost: Net::SSH::Perl Version 1.30, protocol version 2.0.
> .ocalhost: No compat match: 5.1.3.8 SSH Tectia Server
> localhost: Connection established.
> localhost: Sent key-exchange init (KEXINIT), wait response.
> Connection closed by remote host. at /usr/lib/perl5/site_perl/5.8.8/
> Net/SSH/Perl/Kex.pm line 142
>
> These are both v0.10
Sorry don't know but a quick Google filtered lookup
using the exact phrase:
Can't locate object method "_session_channel"
has a thread about possible Win32 install related
problems that might be helpful.
>
> Also, what's up with the SFTP docs -they suck! Does new() die?
How do
> I cd, can I cd? How do I close the connection?
>
Sounds like you may need to eval wrap new() to catch fatalities..
There're examples in the /eg distro directory and a mailing list for
usage questions. See SUPPORT in the docs. Also I see SFTP only
implements a subset of SSH FTP protocol commands. This is mentioned
in the docs as well. (Alternatively, Net::SCP does provide a 'cwd')
--
Charles DeRykus
------------------------------
Date: Thu, 29 Oct 2009 10:07:31 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: software engineering, program construction
Message-Id: <4ae95b53$0$83243$e4fe514c@news.xs4all.nl>
David Filmer wrote:
> FWIW, I have never used a global variable in any production program.
I often have one (and only one) called "%default".
In that hash there are all kinds of defaults, like for optional
parameters, the date-time at the start, etc.
--
Ruud
------------------------------
Date: Thu, 29 Oct 2009 06:38:06 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: software engineering, program construction
Message-Id: <3cd2041e-432d-43f1-9ddb-020b39781572@z2g2000yqm.googlegroups.com>
On Oct 28, 5:03=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
I'm not trying to be a devil's advocate, but simply to learn. I posted
the question because I didn't know the answer, and I've learned some
things from the responses.
> I wouldn't. I would write this as
Okay, you declare the variable.
> =A0 =A0 =A0 =A0 my %data; #no point in naming a hash hash
Then, you initialize the variable by calling a function.
> =A0 =A0 =A0 =A0 %data =3D get_data();
Here is where we differ. Suppose %data is a very large hash. What do
you gain by passing a copy to the function? And if you pass a
reference to the function, you have to dereference it in the function.
To me, it just seems easier to modify the top level variable in the
function and protect yourself my giving the function a descriptive
name.
> =A0 =A0 =A0 =A0 sort_data(%data);
Same comment as above. Why create an extra copy of the data structure,
and why worry about dereferencing a reference?
> =A0 =A0 =A0 =A0 print_data(%data);
> Maybe with references as appropriate.
> Then - I know what data items those functions are working on
> - I know which data items those functions are _NOT_ working on (if it is
> not in the parameter list then they don't touch them)
> - and i can use the same functions to process a second or third or
> fourth set of data, which maybe has a different input format and
> therefore requires a different get_other_data() sub, but my internal
> representation is the same such that I can reuse the sort_data() and
> print_data() functions.
As Uri pointed out, you have to smart rather than consistent. If I
have repeated code, then I put it into a function and pass an argument
to the function. That way, I localize the logic and create a modular
structure.
> It is very hard to unlearn bad habits and even harder to refactor poorly
> written code.
This is true. However, sometimes the circumstances can determine
whether a particular habit is good or bad. There are 'global' bad
habits (like smoking, drinking, and cheating on your wife) and then
there are habits that are bad only because of the specific
circumstance.
CC.
------------------------------
Date: Thu, 29 Oct 2009 06:46:06 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: software engineering, program construction
Message-Id: <0633ed2a-eed4-42dc-b120-c65b3b357123@a32g2000yqm.googlegroups.com>
On Oct 28, 5:34=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
Points taken, and thanks for demonstrating the code. This has been
helpful to me. (I'm not stupid, merely ignorant.)
CC.
> I might write that program something like this:
>
> =A0 =A0 #!/usr/bin/perl
> =A0 =A0 my $data =3D read_data("in.dat");
> =A0 =A0 write_csv($data, "out.csv");
> =A0 =A0 sub read_data {
> =A0 =A0 =A0 =A0 my ($file) =3D @_;
> =A0 =A0 =A0 =A0 open my $IN, "<", $file
> =A0 =A0 =A0 =A0 =A0 =A0 or die "can't read '$file': $!";
> =A0 =A0 =A0 =A0 my %hash;
> =A0 =A0 =A0 =A0 while (<$IN>) {
> =A0 =A0 =A0 =A0 =A0 =A0 chomp;
> =A0 =A0 =A0 =A0 =A0 =A0 my ($val1, $name, $id) =3D split /,/;
> =A0 =A0 =A0 =A0 =A0 =A0 $hash{$val1} =3D {name =3D> $name, id =3D> $id};
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 return \%hash;
> =A0 =A0 }
> =A0 =A0 sub write_csv {
> =A0 =A0 =A0 =A0 my ($data, $file) =3D @_;
> =A0 =A0 =A0 =A0 open my $OUT, ">", $file
> =A0 =A0 =A0 =A0 =A0 =A0 or die "can't write to '$file': $!";
> =A0 =A0 =A0 =A0 for my $key (sort keys %$hash) {
> =A0 =A0 =A0 =A0 =A0 =A0 my $person =3D $hash{$key};
> =A0 =A0 =A0 =A0 =A0 =A0 print $OUT qq("$person->{name}","$person->{id}"\n=
);
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 close $OUT or die "writing to '$file' failed: $!";
> =A0 =A0 }
> The point here is that there's no *point* decomposing your code into
> subs unless you're going to make those subs self-contained. You aren't
> gaining anything.
> Hmm, you're still thinking about 'this script'. You need to be thinking
> 'What will I be doing tomorrow, next week, next year? Can I take some of
> this and make it reusable, to save myself some work tomorrow?'.
> What about the second or third time you write a given function? Do you
> place it in a module and import it from there? Once you start doing that
> regularly, you'll start to see why relying on global state just makes
> things harder in the long run.
>
> Ben
------------------------------
Date: Thu, 29 Oct 2009 07:10:57 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: software engineering, program construction
Message-Id: <io7je59celeo2mgv3gbi7u55rupnr2ddmg@4ax.com>
ccc31807 <cartercc@gmail.com> wrote:
>On Oct 28, 5:03 pm, Jürgen Exner <jurge...@hotmail.com> wrote:
>Then, you initialize the variable by calling a function.
>> %data = get_data();
>
>Here is where we differ. Suppose %data is a very large hash. What do
>you gain by passing a copy to the function? And if you pass a
>reference to the function, you have to dereference it in the function.
>To me, it just seems easier to modify the top level variable in the
>function and protect yourself my giving the function a descriptive
>name.
>> sort_data(%data);
>
>Same comment as above. Why create an extra copy of the data structure,
>and why worry about dereferencing a reference?
No, you don't get it. The difference is that I can see what data items
the function is consuming and producing by simply looking at the
function call. I do not have to dig in some obscure documentation that
is not in sync with the actual code, I do not have to maintain comments
"Reads global variable X and Y, modifies Y and Z" which we all know are
never correct anyway, I do not have to inspect the code of the function
to know what data items it is using.
Instead the documentation of the input and output of that function is
right there in front of my eyes at each and every function call in form
of parameters. It's simply part of writing self-documenting code.
>As Uri pointed out, you have to smart rather than consistent.
And I agree with his comments. But those justified uses of global
variable are rare. Your globals do not belong in that category.
>If I
>have repeated code, then I put it into a function and pass an argument
>to the function. That way, I localize the logic and create a modular
>structure.
At that point the benefits of parameters become obvious to you, too, but
they start way earlier. You just haven't seen the light yet.
jue
------------------------------
Date: Wed, 28 Oct 2009 20:27:09 -0600
From: "Robin" <robin1@cnsp.com>
Subject: tk window resizing
Message-Id: <hcaum4$2j64$1@adenine.netfront.net>
How, when programming in perl/tk can I make a mainwindow not resizable.
Thanks in advance,
--
Robin
--
robin1@cnsp.com
------------------------------
Date: Thu, 29 Oct 2009 03:03:45 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: tk window resizing
Message-Id: <h9dnr6-gnu2.ln1@osiris.mauzo.dyndns.org>
Quoth "Robin" <robin1@cnsp.com>:
> How, when programming in perl/tk can I make a mainwindow not resizable.
See ->resizable in Tk::Wm.
Ben
------------------------------
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 2658
***************************************