[31568] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2827 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Feb 20 21:09:27 2010

Date: Sat, 20 Feb 2010 18:09:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 20 Feb 2010     Volume: 11 Number: 2827

Today's topics:
    Re: (lack of) Helpfulness of Perl error messages. <sreservoir@gmail.com>
    Re: a defense of ad hoc software development <hjp-usenet2@hjp.at>
    Re: benchmarks for perl? <brian.d.foy@gmail.com>
    Re: benchmarks for perl? <bugbear@trim_papermule.co.uk_trim>
    Re: benchmarks for perl? <ben@morrow.me.uk>
    Re: File Position <hjp-usenet2@hjp.at>
        highlight words by regex in pdf files using perl <pengyu.ut@gmail.com>
    Re: highlight words by regex in pdf files using perl <ben@morrow.me.uk>
        One-Liner Mania <usenet@larseighner.com>
    Re: One-Liner Mania <marc.girod@gmail.com>
    Re: One-Liner Mania <ben@morrow.me.uk>
    Re: One-Liner Mania <ben@morrow.me.uk>
    Re: weird behavior when passing hashes to subroutine (Jens Thoms Toerring)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 19 Feb 2010 10:26:52 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: (lack of) Helpfulness of Perl error messages.
Message-Id: <hlmakb$rnt$1@speranza.aioe.org>

On 2/19/2010 8:10 AM, Justin C wrote:
> On 2010-02-18, Ben Morrow<ben@morrow.me.uk>  wrote:
>>
>> Quoth Justin C<justin.0911@purestblue.com>:
>>> As some of you are probably aware I often have difficulty with Perl and
>>> I ask for help here usually about once a month. I try to help myself
>>> where I can, and sometimes, when I'm preparing a minimal example for
>>> posting here, I find the solution to my problem. That's just happened
>>> again, but only due to an error message I received from my text editor
>>> and not Perl directly.
>>>
>>> I'm a Mac user, and code using TextMate<URL:http://macromates.com/>, it
>>> has the ability to run code, I don't know how it does it, though, and
>>> it's error message gave me the clue to the solution of my problem. The
>>> error message on my Debian box was non-helpful.
>>>
>>> What is likely to be the cause of the non-helpful error message? Is the
>>> version of Perl on my Mac likely more recent - I've just checked and on
>>> my Debian box it's 5.10.0, while on the Mac it's 5.8.8. Does TextMate
>>> have a built in Perl interpreter that gives better error messages?
>>
>> You could have answered this easily by running the code from the
>> command-line on your Mac. (You would have got the same error message as
>> from TextMate.)
>>
>>> The error I saw on Debian:
>>> Not a HASH reference at ... line...
>>>
>>> The error I saw from TextMate:
>>> Can't coerce array into hash at ... line...
>>
>> The change in error message is due to the different perl versions. The
>> 'Can't coerce...' message comes from the pseudohash code, which was
>> removed in 5.10, whereas the 'Not a %s reference' is a generic message
>> for situations where a value holds the wrong kind of ref.
>>
>> Before you start thinking this change was a bad idea, try running
>>
>>      perl -le'my $x = [{a =>  1}, "b"]; print $x->{a}'
>>
>> on both machines and consider how hard *that* would have been to debug
>> with the 5.8 behaviour :).
>
> That looks a bit circular, I suppose that that is your point. Under
> 5.8.8 I get 'b' and under 5.10.0 I get 'Not a HASH reference...', how
> $x->{a} gets to be 'b' is the circular part. If you were expecting '1'
> then, yes I can see how debugging that would be tough - especially as
> "it worked", quite a shortage of warnings!

pseudohashes are a sort of magic where
	$t = [ { key => index, ... }, ... ];
causes
	$t->{key}
to be the same as
	$t->[index]

now forget I ever told you that, because it's obsolete and nobody ever
actually used it.

> I have added "use diagnostics" to my "tesp.pl" file, where I put problem
> code reduced to it's minimum parts so that I can work through the
> problem. I'm in that file often enough that I'll see it and add it to
> anything I'm working on that's giving me grief.

as long as you remember that diagnostics is meant for testing and such,
not production code.

-- 

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


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

Date: Sat, 20 Feb 2010 18:25:26 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: a defense of ad hoc software development
Message-Id: <slrnho06o8.g8e.hjp-usenet2@hrunkner.hjp.at>

On 2010-02-17 19:28, Ron Garret <rNOSPAMon@flownet.com> wrote:
> In article <slrnhno94r.num.hjp-usenet2@hrunkner.hjp.at>,
>  "Peter J. Holzer" <hjp-usenet2@hjp.at> wrote:
>
>> On 2010-02-16 22:00, dan@telent.net <dan@telent.net> wrote:
>> > Mark Tarver <dr.mtarver@ukonline.co.uk> writes:
>> >> QUOTE
>> >> You can't write specifications that say exactly what you want. If the
>> >> specification were that precise, then they would be the program.
>> >> UNQUOTE
>> >>
>> >> That's a very clever and profound observation from Paul Graham. The
>> >> formal methods people might disagree though.
>> >
>> > So would I.
>> >
>> > I would like a program that calculates the number x for which x*x=52
>> >
>> > That specification says exactly what I want,
>> 
>> No.
[...]
>> 
>> > but it's of no help at all in creating an algorithm for how to get it.
>> 
>> It is if your programming language includes a primitive or library
>> function for solving quadratic equations.
>> 
>> I would like to change one word in PG's quip:
>> 
>> You can't write specifications that say exactly what you want. If the
>> specification were that precise, then they would be *a* program.
>>
>> If the specification doesn't say how to solve a small, well-understood
>> subproblem, that's not very much different from calling a library
>> function in a program. As a different example, consider sorting: The
>> specification may specify the intended sort order but not the sorting
>> algorithm. In a similar way, many programming languages provide a sort
>> function. The programmer using this function typically doesn't care
>> whether that sort function implements a quickersort, heapsort or
>> mergesort, or some hybrid scheme. He just specifies an order (often in
>> the form of a comparison function) and leaves the details to the
>> implementation.
>> 
>> The more I think about this the more I think PG has stumbled upon a
>> truth here: A specification really is a program. But the "programming
>> language" in which it is written is almost always declarative and not 
>> imperative. There usually isn't any interpreter for the language except
>> the brains of the audience (although parts of the specification/program
>> may be written in a formal language).
>
> Ironically, it's PG's own lack of precision in specifying what he meant 
> that is leading to confusion here.
>
> There are two fundamentally different kinds of specifications, those 
> that specify WHAT the result should be, and those that specify HOW the 
> result is to be obtained.  Both of these can be either vague or 
> specific.  Notwithstanding PG's use of the phrase "what you want" it 
> appears that he was actually talking about specifying how, not what.

Since I haven't read PG's book I can't comment on that.


> The issue PG was addressing (and that this thread seems to be about) is 
> the perennial debate about how specific a HOW spec should be before you 
> make your first attempt to render it into a form that is suitable as 
> input to a compiler.  The right answer has a lot to do with how 
> expensive it is to run your compiler.  If compilation is expensive it 
> makes sense to put more effort into making sure you've thought things 
> through before you try to compile.  If compilation (and more to the 
> point, recompilation) is cheap then it makes less sense.

Compilation time is only one factor in this. You also need to consider
the cost of testing, the cost of rewriting code, the cost of changing
interfaces, the cost of missing deadlines, etc.

And most importantly it misses the fact that the development of your HOW
spec doesn't stop when "you make your first attempt to render it into a
form that is suitable as input to a compiler", and the development of
your WHAT spec doesn't stop when you start developing your HOW spec.
Users often have only a very vague notion of what they need and they
have poor imagination and abstraction facilities. When you show them
your first prototype they will tell you that they wanted something
completely different.


> Then there is the completely orthogonal issue of how to render WHAT 
> specs into HOW specs, which is a fundamentally difficult problem.  It 
> has to be.  If it weren't you could just take the following WHAT spec:
>
> * A program that takes a WHAT spec and renders it as a HOW spec

That's not a WHAT spec. It neither specifies the input nor the output.


> and render *that* as a how spec and you'd never again have to write any 
> code.

I think you missed my point: A formal WHAT spec *is* code. So if you
have that translator you just write code in your declarative WHAT
language instead of your imperative HOW language. And as you write
yourself:


> In fact, just *writing down* a WHAT spec (let alone finding a 
> corresponding HOW spec) is a fundamentally difficult problem.

So you may have solved the simpler part of the problem and be left with
the harder part. (this is not true in all cases - obviously there are
problems which are simple to describe and hard to solve, but in my
experience most of the time the really hard part is the WHAT, not the
HOW).

[rest deleted as it only repeats what I wrote before]

	hp



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

Date: Fri, 19 Feb 2010 16:47:33 -0800
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: benchmarks for perl?
Message-Id: <190220101647337085%brian.d.foy@gmail.com>

In article <j8OdnWeEAuNg9ODWnZ2dnUVZ8nhi4p2d@brightview.co.uk>, bugbear
<bugbear@trim_papermule.co.uk_trim> wrote:


> Is there a standard benchmark, used (perhaps)
> when people port Perl to different CPUs/OS/compilers?

Use perlbench. It's a bit underdocumented, but I show some examples
in Mastering Perl. :)


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

Date: Sat, 20 Feb 2010 08:40:49 +0000
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: benchmarks for perl?
Message-Id: <29udnbY9VdqPPeLWnZ2dnUVZ8h-dnZ2d@brightview.co.uk>

brian d foy wrote:
> In article <j8OdnWeEAuNg9ODWnZ2dnUVZ8nhi4p2d@brightview.co.uk>, bugbear
> <bugbear@trim_papermule.co.uk_trim> wrote:
> 
> 
>> Is there a standard benchmark, used (perhaps)
>> when people port Perl to different CPUs/OS/compilers?
> 
> Use perlbench. It's a bit underdocumented, but I show some examples
> in Mastering Perl. :)

I installed perlbench, but it seems to have
a frankly tiny test set (6 tests), as I posted
in this thread.

I'm looking for actual benchmarks, not a benchmark
"framework".

For the same reason the CPAN "Benchmark" is
not what I want.

    BugBear


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

Date: Sat, 20 Feb 2010 11:39:48 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: benchmarks for perl?
Message-Id: <49u457-bqt.ln1@osiris.mauzo.dyndns.org>


Quoth bugbear <bugbear@trim_papermule.co.uk_trim>:
> Ben Morrow wrote:
> > 
> > There is perlbench on CPAN, but I believe it's not considered to be as
> > representative as it used to be.
> 
> I downloaded this (shortly after my first post).
> I must be missing something - it only runs 8 tests, none
> of which look very "general";
> perl-5.10.0 (DEBUGGING)
>      @bugbear-dell
>      app/podhtml                         674.190 ms �-11.429
>      startup/noprog                      2.484 ms �-2.098
>      statement/func-call-empty-5arg      1.058 �s �-0.020
>      statement/func-call-empty-2arg      670.138 ns �-20.012
>      statement/func-call-empty-1arg      397.798 ns �-7.001
>      statement/func-call-empty-0arg      215.374 ns �-2.893
>      statement/assign-int                68.174 ns �-1.229
>      statement/inc                       46.290 ns �-1.217
> 
> is there a separate Module of tests, with perlbench being
> just a harness?

You're running perlbench-runtests. Try perlbench-run instead, passing it
paths to several different perl binaries. Obviously this means you have
to have all the perls you want to compare on the same machine, so you
can't easily compare, say, perl-on-linux and perl-on-win32. It would be
very hard to get a meaningful comparison in that case anyway.

Ben



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

Date: Fri, 19 Feb 2010 21:44:33 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: File Position
Message-Id: <slrnhntu1i.19b.hjp-usenet2@hrunkner.hjp.at>

On 2010-02-18 08:08, Jürgen Exner <jurgenex@hotmail.com> wrote:
> mud_saisem <mud_saisem@hotmail.com> wrote:
>>so that I can use the seek function 
>
> Now, that is the critical clue. seek() is based on bytes, so you need a
> position in bytes in order to use seek().
> Position in characters would do you no good and therefore my suggestion
> with index() wouldn't do you any good, either, because it returns the
> position in characters.

Only if you use index() on character strings - if you use index on byte
strings it returns a byte position. So just read the file in binary,
convert your search string to the same encoding and invoke index().

Caveat: Some encodings are ambiguous: The same character sequence may be
represented by different byte sequences. For those encodings, index
won't work.

	hp



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

Date: Sat, 20 Feb 2010 14:19:46 -0800 (PST)
From: Peng Yu <pengyu.ut@gmail.com>
Subject: highlight words by regex in pdf files using perl
Message-Id: <5875452a-8784-4e14-8135-8cf4df286572@l26g2000yqd.googlegroups.com>

There are some perl modules (e.g., the following one) can be used for
editing pdf files. But I'm interested in more sophisticated tasks. For
example, I want to automatically highlight certain words (using regex)
in a pdf. Could somebody let me know if there is an easy way to do so
in perl?

http://www.perl.com/pub/a/2007/09/20/pdf-processing-with-perl.html


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

Date: Sat, 20 Feb 2010 23:55:19 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: highlight words by regex in pdf files using perl
Message-Id: <7c9657-r571.ln1@osiris.mauzo.dyndns.org>


Quoth Peng Yu <pengyu.ut@gmail.com>:
> There are some perl modules (e.g., the following one) can be used for
> editing pdf files. But I'm interested in more sophisticated tasks. For
> example, I want to automatically highlight certain words (using regex)
> in a pdf. Could somebody let me know if there is an easy way to do so
> in perl?
> 
> http://www.perl.com/pub/a/2007/09/20/pdf-processing-with-perl.html

That article mentions PDF::API2. I believe that anything which can be
done to a PDF can be done using that module, but it may not be terribly
easy.

Ben



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

Date: Sat, 20 Feb 2010 22:10:39 +0000 (UTC)
From: Lars Eighner <usenet@larseighner.com>
Subject: One-Liner Mania
Message-Id: <slrnho0ne4.bvs.usenet@debranded.larseighner.com>

Can I capture a group of digits, perform integer arithmetic on them,
and use them in the replacement?

Say I have a group of files named Fife_Ch01.html, Fife_Ch02.html
and so forth. The files all have a line with their own name.  I want
to replace the file name with the next number and do not care about
leading zeros.

So,

Fife_Ch01.html is replaced with 2
Fife_Ch02.html is replaced with 3
    and so forth.

Can I do this with a one-liner?

it would look something like this:

#perl -pi -e 's/Fife_Ch([0-9]*)\.html{a miracle happens}/\1/' Fife_Ch*.html




-- 
  Lars Eighner <http://larseighner.com/>      Warbama's Afghaninam day: 80
            1941.0 hours since Warbama declared Viet Nam II.
     Warbama: An LBJ for the Twenty-First century.  No hope.  No change.


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

Date: Sat, 20 Feb 2010 15:58:21 -0800 (PST)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: One-Liner Mania
Message-Id: <5d6f6495-f7ae-4921-807e-086fca96d48f@q21g2000yqm.googlegroups.com>

On Feb 20, 10:10=A0pm, Lars Eighner <use...@larseighner.com> wrote:

> Can I do this with a one-liner?

perl -pi -e '($b)=3D$ARGV=3D~/^Fife_Ch(\d+)\.html$/;close ARGV if s/$ARGV/
$b/' Fife_Ch*.html

On my home box (cygwin), this leaves me with .bak files which I didn't
expect.

Marc


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

Date: Sat, 20 Feb 2010 23:52:02 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: One-Liner Mania
Message-Id: <269657-r571.ln1@osiris.mauzo.dyndns.org>


Quoth Lars Eighner <usenet@larseighner.com>:
> Can I capture a group of digits, perform integer arithmetic on them,
> and use them in the replacement?

You want the /e switch.

> Say I have a group of files named Fife_Ch01.html, Fife_Ch02.html
> and so forth. The files all have a line with their own name.  I want
> to replace the file name with the next number and do not care about
> leading zeros.
> 
> So,
> 
> Fife_Ch01.html is replaced with 2
> Fife_Ch02.html is replaced with 3
>     and so forth.
> 
> Can I do this with a one-liner?
> 
> it would look something like this:
> 
> #perl -pi -e 's/Fife_Ch([0-9]*)\.html{a miracle happens}/\1/' Fife_Ch*.html

Don't use \1 on the replacement side of s///. It was a sop to sed users
put into perl 1 that has never been removed.

    perl -pi -e's/Fife_Ch(\d*)\.html/$1 + 1/e' Fife_Ch*.html

Ben



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

Date: Sun, 21 Feb 2010 00:39:54 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: One-Liner Mania
Message-Id: <qvb657-md71.ln1@osiris.mauzo.dyndns.org>


Quoth Marc Girod <marc.girod@gmail.com>:
> On Feb 20, 10:10 pm, Lars Eighner <use...@larseighner.com> wrote:
> 
> > Can I do this with a one-liner?
> 
> perl -pi -e '($b)=$ARGV=~/^Fife_Ch(\d+)\.html$/;close ARGV if s/$ARGV/
> $b/' Fife_Ch*.html
> 
> On my home box (cygwin), this leaves me with .bak files which I didn't
> expect.

Win32 can't use the Unix trick of deleting a file while it's open, so -i
always makes a backup. (Arguably perl should clean them up afterwards,
but it doesn't.)

Ben



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

Date: 19 Feb 2010 14:38:09 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: weird behavior when passing hashes to subroutine
Message-Id: <7u7m2hFh1vU1@mid.uni-berlin.de>

Justin C <justin.0911@purestblue.com> wrote:
> On 2010-02-19, Patrick Hartman <patrickh@gmail.com> wrote:
> > Hi all, I was working on a program today and made a sub routine that I
> > was passing a few scalars and also a hash to as arguments. This is
> > what I initially tried:
> >
> > my %foo = qw(pepsi cola doctor pepper seven up);
> > my $bar = 'soda is yummy!';
> >
> > &drink(%foo,$bar);

> see 'Pass by Reference' in perdoc perlsub 

> The scalar isn't a problem, it's the hash so I'd do:

> &drink(\%foo, $bar);  # passes a reference to the hash, not the hash

But there's one important difference that the OP should keep in
mind: The original call

drink(%foo,$bar);

passes copies of the hash keys and values to the subroutine, thus
any changes done to them are local to that subroutine and don't
change anything about the original hash. But when one passes a hash
by reference then what the function receives is just a pointer to
that hash and thus all changes done to the hash via the reference
are done to the original hash in the calling function. So, if the
hash isn't changed in the function then passing a reference is fine
(and probably faster since the elements don't have to be copied),
but if that's not the case then passing a hash reference instead of
a list of its elements may lead to unexpected effects, i.e. the 
%foo' hash suddenly being changed after a call of drink(). To
avoid that a copy of the original hash would have to be made and
a reference to that copy be passed to the function. 

                               Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de


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

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


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