[31904] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3167 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 11 00:09:28 2010

Date: Sun, 10 Oct 2010 21:09:10 -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           Sun, 10 Oct 2010     Volume: 11 Number: 3167

Today's topics:
        Date difference in days <paul@pstech-inc.com>
    Re: Date difference in days <paul@pstech-inc.com>
    Re: Date difference in days <rvtol+usenet@xs4all.nl>
    Re: Date difference in days <paul@pstech-inc.com>
    Re: FAQ 4.38 Why don't my <<HERE documents work? <john@example.invalid>
    Re: FAQ 4.38 Why don't my <<HERE documents work? <tadmc@seesig.invalid>
    Re: FAQ 4.38 Why don't my <<HERE documents work? <tadmc@seesig.invalid>
    Re: FAQ 4.38 Why don't my <<HERE documents work? <ben@morrow.me.uk>
    Re: FAQ 4.38 Why don't my <<HERE documents work? sln@netherlands.com
    Re: FAQ 4.38 Why don't my <<HERE documents work? <brian.d.foy@gmail.com>
    Re: FAQ 4.38 Why don't my <<HERE documents work? <tadmc@seesig.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 10 Oct 2010 04:48:32 -0400
From: "Paul E. Schoen" <paul@pstech-inc.com>
Subject: Date difference in days
Message-Id: <L5fso.2011$0R7.309@newsfe09.iad>

I have been trying to get the number of days from 2010-09-13 to today, and 
I'm getting strange results. I have consulted the FAQ and the Date::Calc 
qw(Delta_Days) and Time::localtime modules, as well as 
http://docstore.mik.ua/orelly/perl/cookbook/ch03_04.htm and 
http://docstore.mik.ua/orelly/perl/cookbook/ch03_06.htm.

Here is the pertinent code:

  #!/usr/bin/perl
  use Date::Calc qw(Delta_Days);
  use Time::localtime;

  my $tm = localtime($time);
  my $yr = $tm->year+1900;
  my $mo = $tm->mon+1;
  my $dy = $tm->mday;

  my @dtStart = (2010, 9, 13);
  my @dtNow  = ($yr, $mo, $dy);

  my $difference = Delta_Days(@dtStart, @dtNow);

  my $hitRate = $count/$difference;    #count is number of hits
  print "Content-type: text/html\n\n";
  print "Visitors in $difference days since Sept 13, 2010:
    $count or $hitRate per day $yr, $mo, $dy";

The text in the website that uses this reads:

  Visitors in -14866 days since Sept 13, 2010: 00359 or
    -0.0241490649804924 per day 1969, 12, 31

I've tried so many different things that I'm totally confused. There seem to 
be many ways to do this and it should be a simple task.

TIA,

Paul



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

Date: Sun, 10 Oct 2010 04:58:23 -0400
From: "Paul E. Schoen" <paul@pstech-inc.com>
Subject: Re: Date difference in days
Message-Id: <_efso.2012$0R7.1310@newsfe09.iad>

Never mind. I knew I'd fix it as soon as I posted this. I just used:

  my $tm = localtime();

But I copied the "my $tm = localtime($time);" from this:
http://docstore.mik.ua/orelly/perl/cookbook/ch03_04.htm

Perl can be frustrating, but then it feels so good when it finally works!

Paul


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

Date: Sun, 10 Oct 2010 11:14:47 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Date difference in days
Message-Id: <4cb18401$0$81475$e4fe514c@news.xs4all.nl>

On 2010-10-10 10:58, Paul E. Schoen wrote:

> Perl can be frustrating

Easily undone again by putting

use strict;
use warnings;

in the top of your script.

-- 
Ruud


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

Date: Sun, 10 Oct 2010 05:41:59 -0400
From: "Paul E. Schoen" <paul@pstech-inc.com>
Subject: Re: Date difference in days
Message-Id: <TTfso.15857$Ib3.10013@newsfe21.iad>


"Dr.Ruud" <rvtol+usenet@xs4all.nl> wrote in message 
news:4cb18401$0$81475$e4fe514c@news.xs4all.nl...
> On 2010-10-10 10:58, Paul E. Schoen wrote:
>
>> Perl can be frustrating
>
> Easily undone again by putting
>
> use strict;
> use warnings;
>
> in the top of your script.

Yes, I forgot about that. This script was borrowed from one I found. I had 
to use "my $xxx" to make it pass muster. Thanks for the reminder.

The script can be seen in action as the hit counter in:
http://www.pauleschoen.com/SierraClubBaltimore/index.shtml

Paul 



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

Date: Sat, 09 Oct 2010 21:08:33 -0600
From: John Smith <john@example.invalid>
Subject: Re: FAQ 4.38 Why don't my <<HERE documents work?
Message-Id: <2NudnVyP6cWvsyzRnZ2dnUVZ5rSdnZ2d@giganews.com>

PerlFAQ Server wrote:

>     A nice general-purpose fixer-upper function for indented here documents
>     follows. It expects to be called with a here document as its argument.
>     It looks to see whether each line begins with a common substring, and if
>     so, strips that substring off. Otherwise, it takes the amount of leading
>     whitespace found on the first line and removes that much off each
>     subsequent line.

This faq is dead on what I'm studying in particular tonight, so I 
thought I would give this a whirl.  I haven't quoted much of the 
original post for the sake of brevity.  I did copy the fix() function in 
without changes.  I think I'm calling it correctly too, but I think I've 
got a message coming out on stderr and output that doesn't behave:

$ perl l5.pl
 ...we will have peace, when you and all your works have
perished--and the works of your dark master to whom you
would deliver us. You are a liar, Saruman, and a corrupter
of men's hearts.  --Theoden in /usr/src/perl/taint.c

                 ...we will have peace, when you and all your works have
                 perished--and the works of your dark master to whom you
                 would deliver us. You are a liar, Saruman, and a corrupter
                 of men's hearts.
--Theoden in /usr/src/perl/taint.
Here's a line
	and here's another.
Use of uninitialized value $leader in regexp compilation at l5.pl line 30.
usr/local/
usr/local/doc
usr
$ cat l5.pl
#!/usr/bin/perl
use strict;
use warnings;

         (my $quote = <<'FINIS') =~ s/^\s+//gm;
                 ...we will have peace, when you and all your works have
                 perished--and the works of your dark master to whom you
                 would deliver us. You are a liar, Saruman, and a corrupter
                 of men's hearts.  --Theoden in /usr/src/perl/taint.c
FINIS
print $quote;

         ($quote = <<'FINISH') =~ s/\s+--/\n--/;

                 ...we will have peace, when you and all your works have
                 perished--and the works of your dark master to whom you
                 would deliver us. You are a liar, Saruman, and a corrupter
                 of men's hearts.  --Theoden in /usr/src/perl/taint.
FINISH
print $quote;

         sub fix {
             local $_ = shift;
             my ($white, $leader);  # common whitespace and common 
leading string
             if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\g1\g2?.*\n)+$/) {
                 ($white, $leader) = ($2, quotemeta($1));
             } else {
                 ($white, $leader) = (/^(\s+)/, '');
             }
             s/^\s*?$leader(?:$white)?//gm;
             return $_;
         }

print fix(<<THAT);
   Here's a line
		and here's another.
THAT
print fix(<<THIS);
usr/local/
usr/local/doc
usr
THIS
# perl l5.pl
$

-- 
John Smith


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

Date: Sun, 10 Oct 2010 08:53:21 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: FAQ 4.38 Why don't my <<HERE documents work?
Message-Id: <slrnib3hfm.d3c.tadmc@tadbox.sbcglobal.net>

John Smith <john@example.invalid> wrote:

> I think I've 
> got a message coming out on stderr and output that doesn't behave:


Hmmm, I wonder what "doesn't behave" means when you say it?

Have you seen the Posting Guidelines that are posted here frequently?

    Beware of saying "doesn't work"
        This is a "red flag" phrase. If you find yourself writing that,
        pause and see if you can't describe what is not working without
        saying "doesn't work". That is, describe how it is not what you
        want.

All of the output looks exactly as it is expected to, if you expected
it to look different, then you should tell us what your expectation are.


> Here's a line
> 	and here's another.
> Use of uninitialized value $leader in regexp compilation at l5.pl line 30.


> print fix(<<THAT);
>    Here's a line
> 		and here's another.
 ^^^^^^^
 ^^^^^^^
you have tabs rather than spaces there.


> THAT
> print fix(<<THIS);
> usr/local/
> usr/local/doc
> usr
> THIS


It appears that fix() was not written to expect an argument
that does not need any fixing.

You can fix fix() (heh) by allowing zero leading spaces in the 
else clause rather than requiring at least one leading space:

    ($white, $leader) = (/^(\s*)/, '');
                              ^
                              ^

-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Sun, 10 Oct 2010 08:56:28 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: FAQ 4.38 Why don't my <<HERE documents work?
Message-Id: <slrnib3hlh.d3c.tadmc@tadbox.sbcglobal.net>

John Smith <john@example.invalid> wrote:

> I did copy the fix() function in 
> without changes.

>              if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\g1\g2?.*\n)+$/) {
               if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) {


One of these things is not like the other, one of these things just
doesn't belong...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Sun, 10 Oct 2010 19:17:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: FAQ 4.38 Why don't my <<HERE documents work?
Message-Id: <2ic9o7-m472.ln1@osiris.mauzo.dyndns.org>


Quoth Tad McClellan <tadmc@seesig.invalid>:
> John Smith <john@example.invalid> wrote:
> 
> > I did copy the fix() function in 
> > without changes.
> 
> >              if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\g1\g2?.*\n)+$/) {
>                if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) {
> 
> 
> One of these things is not like the other, one of these things just
> doesn't belong...

\g is a new (5.12?) means of unambiguously making a backref, without
risking confusion with octal interpolation:

    ~% perl -E'say qr/\g10/'
    Reference to nonexistent group in regex; marked by <-- HERE in
    m/\g10 <-- HERE / at -e line 1.
    ~% perl -E'say qr/\10/'
    (?-xism:\10)

Ben



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

Date: Sun, 10 Oct 2010 17:34:45 -0700
From: sln@netherlands.com
Subject: Re: FAQ 4.38 Why don't my <<HERE documents work?
Message-Id: <vdk4b6ht8q9rvbgfsl9nbgc60dammnd2uk@4ax.com>

On Sat, 09 Oct 2010 21:08:33 -0600, John Smith <john@example.invalid> wrote:

>PerlFAQ Server wrote:
>
>>     A nice general-purpose fixer-upper function for indented here documents
>>     follows. It expects to be called with a here document as its argument.
>>     It looks to see whether each line begins with a common substring, and if
>>     so, strips that substring off. Otherwise, it takes the amount of leading
>>     whitespace found on the first line and removes that much off each
>>     subsequent line.
>
>This faq is dead on what I'm studying in particular tonight, so I 
>thought I would give this a whirl.  I haven't quoted much of the 
>original post for the sake of brevity.  I did copy the fix() function in 
>without changes.  I think I'm calling it correctly too, but I think I've 
>got a message coming out on stderr and output that doesn't behave:
>
>$ perl l5.pl
>...we will have peace, when you and all your works have
>perished--and the works of your dark master to whom you
>would deliver us. You are a liar, Saruman, and a corrupter
>of men's hearts.  --Theoden in /usr/src/perl/taint.c
>
>                 ...we will have peace, when you and all your works have
>                 perished--and the works of your dark master to whom you
>                 would deliver us. You are a liar, Saruman, and a corrupter
>                 of men's hearts.
>--Theoden in /usr/src/perl/taint.
>Here's a line
>	and here's another.
=======================

>Use of uninitialized value $leader in regexp compilation at l5.pl line 30.

=======================
This message?

>usr/local/
>usr/local/doc
>usr
>$ cat l5.pl
>#!/usr/bin/perl
>use strict;
>use warnings;
>
>         (my $quote = <<'FINIS') =~ s/^\s+//gm;
>                 ...we will have peace, when you and all your works have
>                 perished--and the works of your dark master to whom you
>                 would deliver us. You are a liar, Saruman, and a corrupter
>                 of men's hearts.  --Theoden in /usr/src/perl/taint.c
>FINIS
>print $quote;
>
>         ($quote = <<'FINISH') =~ s/\s+--/\n--/;
>
>                 ...we will have peace, when you and all your works have
>                 perished--and the works of your dark master to whom you
>                 would deliver us. You are a liar, Saruman, and a corrupter
>                 of men's hearts.  --Theoden in /usr/src/perl/taint.
>FINISH
>print $quote;
>
>         sub fix {
>             local $_ = shift;
>             my ($white, $leader);  # common whitespace and common 
>leading string
>             if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\g1\g2?.*\n)+$/) {
>                 ($white, $leader) = ($2, quotemeta($1));
>             } else {
>                 ($white, $leader) = (/^(\s+)/, '');
                                      ^^^^^^^^^^^^^^
This is a list, the first item is a regex with capture group(s),
the second  item is the empty string ''.
So the capture group(s) will be flattend out and the  '' added
at the end.

The first item, the regex, is interpreted in list context.
In this context, the regex must susceed before the capture group(s)
data(s) are added to the list.

But IF the regex failed, that which is creating the list, interprets
the element as just another empty list ().

So what happenes in the case that the list is expecting another
list from the regex (it is  in this case), is that an empty list ()
is inserted. The effect of embedded an empty list in another list is
that nothing is inserted. Everything to the right shifts left one position
in the list.

So,  when the regex failed, ($white, $leader) = (/^(\s+)/, '');
was interpreted as
 ($white, $leader) = ( (), 'I am leader' );
or really
 ($white, $leader) = ( 'I am leader' );

$white now equals 'I am leader' (the '' really)
$leader now equals undef;

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

Its really, really bad to insert regex's into lists this way.
The empty list () will disturb the order.
And there is actually two problems, one is an undef message because
of the list not being in the expected order, and an element being
undef that is not being checked.

You can overcome the element shift by doing this:

  ($white, $leader) = (/^(\s+)/ ? $1 : '', '');

The defined'ness of $1 is a factor too if capture groups are inserted
with alternations like:

   (/^(\s+)|(\s*)/ ? .. : .., '')

But thats another issue. If it gets that complex, it should be moved out 
from within the list creation syntax, and assignd a variable to be inserted
in the list.

Some would just fix up the regular expression (\s*), but it is still a bad idea,
imo, to do it from within list creation.

-sln



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

Date: Sun, 10 Oct 2010 21:37:58 -0400
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 4.38 Why don't my <<HERE documents work?
Message-Id: <101020102137589407%brian.d.foy@gmail.com>

In article <2ic9o7-m472.ln1@osiris.mauzo.dyndns.org>, Ben Morrow
<ben@morrow.me.uk> wrote:

> \g is a new (5.12?) means of unambiguously making a backref, without
> risking confusion with octal interpolation:

It's in 5.010. It's even in Learning Perl. :)


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

Date: Sun, 10 Oct 2010 21:47:32 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: FAQ 4.38 Why don't my <<HERE documents work?
Message-Id: <slrnib4ure.fdc.tadmc@tadbox.sbcglobal.net>

brian d foy <brian.d.foy@gmail.com> wrote:
> In article <2ic9o7-m472.ln1@osiris.mauzo.dyndns.org>, Ben Morrow
><ben@morrow.me.uk> wrote:
>
>> \g is a new (5.12?) means of unambiguously making a backref, without
>> risking confusion with octal interpolation:
>
> It's in 5.010. It's even in Learning Perl. :)


I ought to read that book again sometime...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

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


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