Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
more useful options
 
PerlMonks

Meditations

 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

( #480=superdoc: print w/ replies, xml ) Need Help??

If you've discovered something amazing about Perl that you just need to share with everyone, this is the right place.

This section is also used for non-question discussions about Perl, and for any discussions that are not specifically programming related. For example, if you want to share or discuss opinions on hacker culture, the job market, or Perl6 development, this is the place. (Note, however, that discussions about PerlMonks belong in Perl Monks Discussion.)

Meditations is sometimes used as a sounding-board — a place to post initial drafts of perl tutorials, code modules, book reviews, articles, quizzes, etc. — so that the author can benefit from the collective insight of the monks before publishing the finished item to its proper place (be it Tutorials, Code Catacombs, Reviews, or whatever). If you do this, it is generally considered appropriate to prefix your node title with "RFC:" (for "request for comments").

User Meditations
The Perl 6 Threat Revealed
on Nov 18, 2009 at 15:16
5 direct replies by kyle

    Bretheren of Perl,

    When I first learned Perl, it was Perl 4. Perl 5 was available, but it was new, and the company I was working for didn't trust it yet. That was many years ago, at the first job I had after college. In the time since, I've used Perl to varying degrees at every job I've ever had. I still work with Perl 5 today, and I expect this will be how I continue to make my living for years to come.

    Some months ago, I shifted my free time expenditures from the Monastery to contributing to Perl 6. At the time, I'd heard there were some Perl 5 folks none too happy with Perl 6, but I was really only dimly aware of this and even further oblivious to the reasons involved.

    I found the Perl 6 community to be friendly, welcoming, helpful, and all the other wonderful things I'd come to expect from my experience here at the Monastery. I basically forgot about any rift between the Perl 5 and Perl 6 communities. As I had experienced them, I saw no reason for them not to get along.

    My eyes have been opened by masak and mst, who had a long private conversation on this topic and revealed the results of it in two articles.

    I think the Perl community would benefit from taking these to heart. If you find yourself lacking the time to read the illuminations within them, mst has prepared a very brief summary, which I will summarize further.

    Perl 5 and Perl 6 are two separate languages in the same family. I can tell you from my own experience that the fine folks working on Perl 6 do not plot the demise of Perl 5. I can also tell you that Perl 6 is clearly a different language from Perl 5 and just as clearly came from the same place. They were made with the same ideas in mind. Long may they live with our love and dedication.

    Peace to you, Perl users everywhere.


[Offer your reply]
Another Gearman perl module?
on Nov 16, 2009 at 15:58
3 direct replies by oha

    Hi,

    i was trying http://gearman.org and i've therefor got from cpan Gearman::Client and Gearman::Client::Async but, for what i need, i felt like they weren't designed as i need, and i've tried to write another which suits best my needs.

    Having not wrote any documentation, i'll just show you the usage:

    use Gearman::OO::Client; my $client = Gearman::OO::Client->new( servers => ['127.0.0.1:4730'], max_queue => 4, ); logit("connected"); foreach my $id (1..8) { $client->submit( func=>'test', req=>"Data for $id", complete => sub { logit("completed #$id: @_"); }, error => sub { logit("ERROR #$id! @_"); }, ); logit("added #$id"); } logit("sync..."); $client->sync(); logit("done.");

    and here the output with 2 workers which just sleep for 1 second and respond with an empty string1:

    00:00:00 connected 00:00:00 added #1 00:00:00 added #2 00:00:00 added #3 00:00:00 added #4 00:00:01 completed #1: 00:00:01 completed #2: 00:00:01 added #5 00:00:01 added #6 00:00:02 completed #3: 00:00:02 completed #4: 00:00:02 added #7 00:00:02 added #8 00:00:02 sync... 00:00:03 completed #5: 00:00:03 completed #6: 00:00:04 completed #7: 00:00:04 completed #8: 00:00:04 done.

    As you can see, some jobs are added early up to the max_queue limit, then it will block until any of the previous job complete to continue.

    Also you can use a completely OO Job object:

    package MyJob; use base 'Gearman::OO::Job'; sub new { my ($class, %args) = @_; bless \%args, $class; } sub function { 'test'; } sub complete { my ($self, $data) = @_; logit("$self completed($data)") +; } sub req_data { my ($self) = @_; return $self->{req}; } use Gearman::OO::Client; my $client = Gearman::OO::Client->new( servers => ['127.0.0.1:4730'], max_queue => 4, ); logit("connected"); foreach my $id (1..8) { $client->submit_job(MyJob->new(req => "OO #$id")); logit("added #$id"); } logit("sync..."); $client->sync(); logit("done.");

    and now the meditation: anyone think this could be usefull? and if so, which name should i use? does it merit to be on CPAN?


    1 -- on twp distinct shells: gearman -w -f test sleep 1

[Offer your reply]
OO automatic accessor generation
on Nov 11, 2009 at 08:21
4 direct replies by Neighbour
    So I've been reading up on perltoot and perltooc and have been trying to create my very first perl class.. It started like this:
    package DataTable; sub new { my $class = shift; my $self = {}; $self->{tablename} = undef; $self->{columns} = []; $self->{indices} = {}; bless($self, $class); return $self; } sub tablename { my $self = shift; if (@_) { $self->{tablename} = shift; } return $self->{tablename}; } sub columns { my $self = shift; if (@_) { @{$self->{columns}} = @_; } return @{$self->{columns}}; } sub indices { my $self = shift; if (@_) { %{$self->{indices}} = @_; } return %{$self->{indices}}; } 1;
    But then, after having to add more array properties (datatypes, lengths, decimals, allownull, default) and copypasting the accessor subs, something itched. So I revised it to..
    package DataTable; sub new { my $class = shift; my $self = {}; $self->{tablename} = undef; $self->{columns} = []; $self->{indices} = {}; $self->{datatypes} = []; $self->{lengths} = []; $self->{decimals} = []; $self->{signed} = []; $self->{allownull} = []; $self->{default} = []; $self->{usequote} = []; } bless($self, $class); return $self; } # Accessor methods sub ArrayAccessor { my $arrayname = shift; my $self = shift; if (@_) { @{$self->{$arrayname}} = @_; } return @{$self->{$arrayname}}; } sub HashAccessor { my $hashname = shift; my $self = shift; if (@_) { %{$self->{$hashname}} = @_; } return %{$self->{$hashname}}; } sub ScalarAccessor { my $scalarname = shift; my $self = shift; if (@_) { $self->{$scalarname} = shift; } return $self->{$scalarname}; } sub tablename { return ScalarAccessor("tablename", @_); } sub columns { return ArrayAccessor("columns", @_); } sub indices { return HashAccessor("indices", @_); } sub datatypes { return ArrayAccessor("datatypes", @_); } sub lengths { return ArrayAccessor("lengths", @_); } sub decimals { return ArrayAccessor("decimals", @_); } sub signed { return ArrayAccessor("signed", @_); } sub allownull { return ArrayAccessor("allownull", @_); } sub default { return ArrayAccessor("default", @_); } sub usequote { return ArrayAccessor("usequote", @_); } 1;
    However, seeing the long list of accessor function calls, I still found the need for even more abstraction:
    package DataTable; sub new { my $class = shift; my $self = {}; $self->{tablename} = undef; $self->{columns} = []; $self->{indices} = {}; $self->{datatypes} = []; $self->{lengths} = []; $self->{decimals} = []; $self->{signed} = []; $self->{allownull} = []; $self->{default} = []; $self->{usequote} = []; # Automatically create Accessor methods foreach (keys(%{$self})) { my ($type, $prefix, $suffix) = (ref($self->{$_}), "", ""); if ($type eq "ARRAY") { $prefix = '@{'; $suffix = '}'; } if ($type eq "HASH") { $prefix = '%{'; $suffix = '}'; } eval("sub $_ { " . 'my $self = shift; if (@_) { ' . $prefix . '$self->{' . $_ . '}' . $suffix . +' = @_; } return ' . $prefix . '$self->{' . $_ . '}' . $suffix . '; }'); } bless($self, $class); return $self; } 1;
    However, since this is my first attempt at OO-perl, I humbly submit this to the monks for comments :)

[Offer your reply]
Thoughts on "Go"?
on Nov 10, 2009 at 21:57
8 direct replies by punkish
    Google announced a new programming language called "Go" see http://golang.org/

    It is described as "expressive, concurrent, garbage-collected," which, to me, not only doesn't say much, but whatever it does say, sounds like a language that you and I already know.

    Since languages are cultural, and are always evolving (and those that are not busy evolving are busy dying, to paraphrase our national bard), I seek your comments, musings, rants on this new language. Maybe we will even see an Inline::Go sooner or later.

    Let's go.

    Update: Names behind "Go" -- Robert Griesemer, Rob Pike, Ken Thompson, Ian Taylor, Russ Cox, Jini Kim and Adam Langley (see http://google-opensource.blogspot.com/2009/11/hey-ho-lets-go.html. Very impressive.

    --

    when small people start casting long shadows, it is time to go to bed

[Offer your reply]
pi and some continued fractions
on Nov 08, 2009 at 13:09
4 direct replies by spx2

    Today I thought I'd implement a way to compute pi using continued fractions

    I was trying to get 1000 decimals for codegolf.com , but I was only able to get 10 correct decimals(yes, I know..pathetic) in 6 seconds(codegolf limits this to 4 seconds).

    I used the formula here(the one in the middle),and wrote code that weighs at about 140 bytes , but the 1st entry in codegolf.com for this problem has 54 bytes or so.

    Anyone care to divulge their (partial) solution to this ?

    Maybe they treat the problem as one of compressing those 1000 digits rather than computing them? And if so, I think there were some theoretical bounds to compressing data, I do not remember what those were ... I'm pretty sure codegolf doesn't allow use of modules and implementing your own compression algo would jump over 54bytes for sure , or not ?

    use bignum qw/p -20/; my ($pi,$n) = ('3+1/(X)',1500); $pi =~ s{X}{ '6'. ( $_!=$n ?'+'.((2*$_+1)**2).'/(X)' :'' ) }e for(1..$n); print eval $pi;

[Offer your reply]
Hard Problems
on Nov 07, 2009 at 19:16
3 direct replies by Anonymous Monk
    As I see, with knowledge of Perl and CPAN Modules and some sort of expertness, solving any problem is just an engineering problem, where you just have to put the pieces together and create a flow. But often times, I see some hard problems. What are those according to you?
    • Huge Amount of Data
    • Large Processing Time
    • Different versions of Perl/OS
    • Non-centralized data
    • Unknown hardware failure
    • Segmentation fault (out of memory)
    • Lost code :-)
    • Lack of code maintenance due to a left developer
    • Unmaintainable code due to spaghetti nature

[Offer your reply]
RFC: Creating unicursal stars
on Nov 06, 2009 at 09:17
3 direct replies by Fox
    All started a week ago when I saw a unicursal hexagram and I started wondering the logic behind drawing a n-points unicursal star, so I gone thorough some wikipedia articles and finally created a method that can draw a unicursal star polygon of n points for any natural* number.

    For those wondering, any non-convex polygon in star form could be considered a star polygon, but only those where the lines can be draw starting and ending at the same point, passing through all the other points exactly one time are unicursal star polygons

    Although my code works like I wanted, I still don't like how it does it, so I would like to hear what the perlmonks have to say about it.


    And here is the code:

    As you can see, it actually outputs a SVG image, you can run it like:
    $perl draw_star.pl N > img.svg ,where N is the points number
    and open the result in your favorite browser/image viewer.

    The main problem is finding the $mod number, this is how much points it will 'jump' from the current point to draw the next line.
    Notice that this is where things get dirty, calcMagic was called this ways because I failed in understand the logic(if any) behind it.

    There is also a side problem, take a look again in the unicursal hexagram and you will notice that it's irregular, which means the $mod number changes depending on the current point.

    ..and, although I was a Anonymous Monk for quite a time, I sill not sure if this is the right place for this, even after reading Where should I post X?, so I'm sorry if it isn't.

[Offer your reply]
Fun with floating points
on Nov 06, 2009 at 08:36
2 direct replies by kikuchiyo
    I've got bitten by a peculiarity of floating point handling recently.

    The phenomenon is best illustrated with the following snippet:
    $ perl -le'$x=0.05;$y=sqrt($x+$x+$x);$g="$y";print "Is $y equal to $g? +";print $y==$g?"Yes":"No"' Is 0.387298334620742 equal to 0.387298334620742? No

    Of course there is nothing surprising here after all. It should just serve as a reminder that floating point numbers are nasty beasts. It is dangerous to have certain assumptions about them. Also, when you write code that processes a lot of numbers then forms decisions based on comparisons ofthem, it is important to check that the code really does what you think it does. Extreme cases, loss of accuracy and so on.

[Offer your reply]
Updating my confidence in my Perl knowledge
on Nov 04, 2009 at 10:27
10 direct replies by Anonymous Monk
    I find new things constantly on this website and other websites and books related to Perl. I enjoy learning from it. When I discover new things, that may be a fundamental to possibly large number of people, my confidence level drops. It means that I have to update myself tremendously to bring myself to the par level. It is sort of knowledge race, that I am trying to run constantly.

    So, dear monks, how do you see yourself in reference to the race I mentioned, now and in forthcoming years? I am somewhat exhausted, but my enthusiasm continues. I am worrying about the confidence level, because when I am looking for job, I need higher confidence.


[Offer your reply]
use common::sense;
on Nov 03, 2009 at 08:34
2 direct replies by Anonymous Monk

[Offer your reply]
Different meanings of $0 under the same operating system
on Nov 03, 2009 at 05:27
6 direct replies by Jorge_de_Burgos
    I am using a Linux system (Ubuntu 9.10). It took me several hours of frustration to realise something about $0. This built-in variable is defined in perlvar as "the name of the program being executed". Perlvar doesn't mention that its meaning can change, even inside the same program running under the same system, according to the way the program is run. You have this code:
    #!/usr/bin/perl -w use UI::Dialog; # sudo apt-get install libui-dialog-perl my $statement = "My name is $0...\n"; my $d1 = new UI::Dialog(title => 'I know my name'); $d1->msgbox(text => $statement); print $statement; exit;
    You save it into a file named "knowthyself" in your Desktop. You give the file executable permissions (chmod 755 knowthyname). Then you run the program 3 times:
    1. The first time you open a terminal and type "perl knowthyself". You get this output: "My name is knowthyself..."
    2. Then you use the terminal and type "./knowthyself". You get this output: "My name is ./knowthyself..."
    3. Finally you go to your file manager (Nautilus) and click on the file. You are asked what to do: Do you want to run it in a terminal, do you want to show its contents, do you want to cancel the action, or do you want to just run it? Of course you choose this last action (run the program without opening a terminal). You get this output (in a window): My name is /home/user/Desktop/knowthyself...
    Moral: even if you are running your program under one and the same system, even if it supports $0, you have to be very careful before relying on what $0 returns.

[Offer your reply]
SQL Library modules - DBIx::QueryByName
on Oct 30, 2009 at 09:42
1 direct reply by metaperl
    This is more of a quick note than comprehensive overview. I've looked over the various offerings for creating libraries of SQL and one new one hit CPAN today - DBIx::QueryByName.

    In looking at it, the one thing I notice about the XML File Syntax is that there is no way for parameters to have default values if not specified.

    Another thing is that XML File Syntax makes it difficult for a SQL mode in emacs to highlight and indent the code unless you do something with mixed mode so that it handles the XML and SQL gracefully.

    The final thing is that the session management features of this module appear to be a violation of orthogonal integration of concerns.


[Offer your reply]
Mini-Tutorial: Perl's Memory Management
on Oct 27, 2009 at 16:25
3 direct replies by ikegami

    This post answers a few common questions and misconceptions about Perl's memory management.

    Lexical variables don't get freed at end of scope

    Lexical variables don't get freed at end of scope. They are cleared on scope exit to give you the apperance of a fresh variable. This is an optimisation that should be transparent to you.

    On scope exit, if anything prevents the variable from being resused (e.g. if there are remaining references to the variable), the existing variable becomes anonymous and a new variable is allocated.

    Clearing a variable does not free the memory it uses

    It was previously mentioned that variables on cleared on scope exit. Variables can also be cleared as follows:

    $s = undef; @a = (); %h = ();

    Clearing a variable frees no memory directly. The buffer of a string is marked as being unused, but it remains allocated for later use. The underlying array of pointers in arrays and hashes is cleared, but it remains allocated for later use. This is an optimisation that should be transparent to you.

    As a result of a variable being cleared, referenced variables (incl array elements) have their refcount lowered, and that may free those variables. But not the variable being cleared.

    undef does not free a variable

    undef can be used to clear a variable.

    undef $s; undef @a; undef %h;

    undef does do more than clear the variable. It also frees any buffers the variable has.

    $ perl -MDevel::Peek -e' $s = "abc"; Dump $s; $s = undef; Dump $s; undef $s; Dump $s; ' SV = PV(0x814fb00) at 0x814f69c REFCNT = 1 FLAGS = (POK,pPOK) <-- Var contains a string PV = 0x81651b8 "abc"\0 CUR = 3 LEN = 4 SV = PV(0x814fb00) at 0x814f69c REFCNT = 1 FLAGS = () <-- Var is undefined PV = 0x81651b8 "abc"\0 <-- String buffer still CUR = 3 allocated after clearing LEN = 4 SV = PV(0x814fb00) at 0x814f69c REFCNT = 1 FLAGS = () <-- Var is undefined PV = 0 <-- undef freed the buffer

    This function is rarely useful.

    However, undef will never free a variable. Clearing the variable may result in the reduction of the ref counts of other variables, which may free those variables. But not the variable that was passed to undef.

    Freeing variables may result in no change in available system memory

    You can't rely on memory being returned to the system, but it can happen.

    If and when memory is returned to the OS is dependant on the memory allocation mechanism perl was compiled to use. You may also see differences between system using the same memory allocation mechanism. It's also possible that a mechanism will only ever release the large blocks it allocated.

    The default memory allocation mechanism varies by OS. You are more likely to see memory being released to the OS on Windows.


[Offer your reply]
Dirge (Retrograde Amnesia)
on Oct 22, 2009 at 21:51
5 direct replies by biohisham
    Yesterday I was caught in a fix, a dreadful situation that I am certain many have come across but my certainty doesn't provide for ways to get out of or alleviate its impact nor doest it tell me of individual ways to see-it-coming and halt it right there, neither. I may sound alarmed, but justification is what follows and a requirement for useful tips and directions is what I seek.

    This particularly involves the monks among you who use more than one programming languages in their work. I was introduced to SAS sometime before Perl, I reached a nice level of understanding its different language structures and concepts and used to work my way through the many procedures it had to achieve something purposeful, it was nice and fun. Today I don't know if it is mastery anymore that I thought I possessed, I haven't used SAS, however, for ~4 months. Back to yesterday again, I heard of a new SAS community that holds the sort of interactions the likes of StackOverFlow, I happily joined in, and then tried to answer a simple post, to my consternation, my fingers just couldn't type and my mind stayed blank,and dude(tte) I wasn't caught in the moment and it seems like I forgot the syntax!.

    What betook me then was enormous to describe, and in the inside of my mind I have been thinking about this; All the love I have for Perl, every time I check a Monk's node and find that that Monk hasn't been around here for 2 or 5 or 7 years and I'd sadly wonder, where they are and what has become of their Perl programming skills and if I would get to walk down that path ever would I forget the syntax, would it rust with length of unuse, and I feel while I am in the middle of such thoughts that it would be a darn world outside the confines of the Monastery, so I stop thinking and haste back to try to reply to a freshly submitted post or get to contemplate the Monastery.

    It is a frustration to not be able to retain information after all the dedication and enthusiasm spent picking it, I don't wanna say I forgot SAS (Retrograde Forgetfulness), but I would wanna know how do you manage to transit between two disparate programming languages and how do you strive to keep abreast without sacrificing one for the other.

    Would ye revered monks be caught in such a situation at times?, how would you cope to retain and compartmentalize your programming tools and how would you push the refresh button on a programming language you haven't probably used in a while in response to an immediate stimulus like the one I had yesterday and not fail like I did?



    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

[Offer your reply]
Perl Test
on Oct 22, 2009 at 03:53
12 direct replies by Faoahy

    I decided to post this question here since this section is for discussions of non-programming related issues. But to tell you the truth, I really dont know if this is the right place to post this question but here it goes.

    I was tasked by my superior to check the knowledge level of our developers on Perl. Previously, the developers were asked to learn Perl by self-study. Time was the only thing provided (no training). After 3 months, the developers are saying that they have learned a lot and have used it on their work. But we dont have any idea on how to gauge their knowledge gained.

    Will you guys suggest on how to do this? how to gauge/check their knowledge so that we can have an idea which of our developers has potential or whom needs more training or time to learn.

    We noticed that they are using perl on their work and most of their scripts are working. But how are we going to know if what they did is the best practice or the best way to do that. Note that we dont have a senior level or an experience developer to check their code.

    Any suggestion, comments are welcome. And if you think that this question is not for this section, you can remove it but please tell me the section where I should post this.

    Thanks in advance!


[Offer your reply]
 (1-15) of 300 Next entries--> 

Add your Meditation
Title:
Meditation


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Login:
    Password
    remember me
    What's my password?
    Create A New User

    Community Ads
    Chatterbox
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users
    Others studying the Monastery: (11)
    Corion
    GrandFather
    wfsp
    atcroft
    herveus
    Eyck
    clinton
    Co-Rion
    gnosti
    im2
    darkman0101
    As of 2009-11-21 09:50 GMT
    Sections
    The Monastery Gates
    Seekers of Perl Wisdom
    Meditations
    PerlMonks Discussion
    Categorized Q&A
    Tutorials
    Obfuscated Code
    Perl Poetry
    Cool Uses for Perl
    Perl News
    Information
    PerlMonks FAQ
    Guide to the Monastery
    What's New at PerlMonks
    Voting/Experience System
    Tutorials
    Reviews
    Library
    Perl FAQs
    Other Info Sources
    Find Nodes
    Nodes You Wrote
    Super Search
    List Nodes By Users
    Newest Nodes
    Recently Active Threads
    Selected Best Nodes
    Best Nodes
    Worst Nodes
    Saints in our Book
    Leftovers
    The St. Larry Wall Shrine
    Offering Plate
    Awards
    Craft
    Snippets Section
    Code Catacombs
    Quests
    Editor Requests
    Buy PerlMonks Gear
    PerlMonks Merchandise
    Planet Perl
    Perlsphere
    Use Perl
    Perl.com
    Perl 5 Wiki
    Perl Jobs
    Perl Mongers
    Perl Directory
    Perl documentation
    CPAN
    Random Node
    Voting Booth

    Future historians will find that the material characteristic of the current era is...

    Aluminium
    Plastic
    Oil
    Water
    Carbon dioxide
    Copper
    Iron
    Silicon
    Salt
    Uranium
    Hydrogen
    Other

    Results (729 votes), past polls