Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Performance problems on splitting long strings

by AnomalousMonk (Archbishop)
on Feb 01, 2014 at 19:55 UTC ( [id://1072989]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Performance problems on splitting long strings
in thread Performance problems on splitting long strings

... I think I first did something like:
regex1  => $regex1->(),
which gave compile errors.

You may know this already, but  $regex1->() is a function invocation. The  { ... } anonymous hash reference constructor tries to treat the first item returned by this function call as a value to be paired with, in this case, the key 'regex1'. If the number of items in the list consisting in the grand total of all such keys and invocations is odd, the constructor will fail with an "Odd number of elements in anonymous hash ..." error. If it is even, the hash constructed will be a meaningless mish-mosh unless the referenced functions are designed to return valid hash elements, which in this case they are not.

The expression  sub {$regex1->()} produces a single code reference, which pairs as a value quite happily with any key string. It is redundant in that it simply wraps the invocation of another code reference, but this point has already been covered.

Replies are listed 'Best First'.
Re^5: Performance problems on splitting long strings
by Laurent_R (Canon) on Feb 01, 2014 at 23:24 UTC

    Thank you, AnomalousMonk, for your very useful input.

    Yes, looking at it retrospectively, I clearly realize that it was a bit silly on my part to try to pass a function invocation, rather than a simple code reference, to the benchmark module. I am definitely not proud of that stupid error, but I thought it might be useful to others for me to explain what I originally did wrong. I wrote a couple of tutorials in French on some specific uses of Perl, and, in them, I am quite often proposing an initial wrong answer before giving out the right one, as I think trying something wrong is at least as useful as getting the right one from the start. Your explanation on my original error serves the same purpose, thank you for that (and, let me be clear on that, I am not trying to claim that it will be useful to others only, my description of my original error was hopefully for the others' benefit, your explanation is useful to others, I think, AND to me as well).

    My only excuse, if any, is that it was very late at night and I was tired, so that my ideas were a little bit blurred by tiredness (not to mention the couple of wine glasses I had earlier for a small celebration at my photo club). Then, as I already said, I was in a bit of a hurry to try to get the results I was looking for and wrapping the invocation in a sub {} construct was the fastest solution I found to get a clean compile and usable results. Thank you again for the time you took on this post, as well as on your earlier post on the same topic. Cheers.

      ... trying something wrong is at least as useful as getting the right one from the start.

      I often see questions on this site in the form "What happens if I do X?" and the answer I am often tempted to give is "Well, what happened when you did X?" Unfortunately, this answer is usually a bit too brusque to offer a novice and one tends to be more circumspect. I agree that play is always valuable for learning, and Perl makes playful interaction fairly easy.

      BTW: I didn't mean by my reply to offer you instruction for I do not suppose you need it, but only to offer a general explanation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1072989]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-18 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found