Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Syntactic Confectionary Delight
 
PerlMonks

The Monastery Gates

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

( #131=superdoc: print w/ replies, xml ) Need Help??
Donations gladly accepted

Users, please read the following important update:

Status of Recent User Information Leak

If you're new here please read PerlMonks FAQ
and Create a new user.

Want Mega XP? Prepare to have your hopes dashed, join in on the: poll ideas quest 2009  (Don't worry; you've got plenty of time.)

New Questions
Embedding tests?
on Nov 20, 2009 at 11:14
3 direct replies by LanX
    Hi

    I'm trying to get an overview about the possibilities to integrate the tests for a sub/method right away after defining them, much like POD embeds documentation right away in the code.

    Till now I found Test::Pod::Snippets and Pod::Tests which use POD-parsers.

    I slightly remember a discussion about a more functional than POD approach to do it.

    UPDATE: Or code filter approach, like Smart::Comments

    Anything more to be considered?

    Any idea is appreciated...

    Cheers Rolf


[Offer your reply]
Algorithm For Uninstalling Dependencies
on Nov 20, 2009 at 09:57
5 direct replies by Limbic~Region
    All,
    A co-worker has posed a problem to me. I haven't had time to work on it myself, but I think it is interesting and thought I would share. Given a dependency tree and a selected item, generate the ordered list of items that must be removed in order to remove the selected item. The rules are easy:
    • All dependencies must be removed first
    • Any item that depends on a removed item, must be removed after that item is removed

    Consider the data below. The left most token is the item and all tokens to the right of it are the dependencies.

    FOO BAR BLAH ASDF BAR LA BLAH XYZ ASDF OOOOO LA XYZ LA OOOOO

    Let's say we want to remove BLAH. The following is my own mental representation of the process.

    Simple path down: BLAH -> XYZ -> LA But now we look back up: LA <- BAR <- FOO XYZ <- BLAH <- FOO BLAH <- FOO And now we look back down again FOO -> ASDF -> OOOOO And finally back up ASDF <- FOO

    So to remove 'BLAH', the following items must be removed:

    BLAH XYZ LA BAR FOO ASDF OOOOO
    In what order? I believe any of the following are viable.
    LA -> XYZ -> BLAH -> BAR -> OOOOO -> ASDF -> FOO LA -> XYZ -> BLAH -> OOOOO -> ASDF -> BAR -> FOO LA -> XYZ -> BLAH -> OOOOO -> BAR -> ASDF -> FOO

    I have looked at Algorithm::Dependency::Ordered but it doesn't do the right thing. Now assuming you have a valid tree (doesn't have an item that must be removed both before and after another item), can you come up with an algorithm that solves the problem?

    UPDATE: It turns out these are the wrong requirements (though it is an interesting problem). For the actual (more boring) - see Re^4: Algorithm For Uninstalling Dependencies.

    Cheers - L~R


[Offer your reply]
Append to file or create file depending on input filename.
on Nov 20, 2009 at 08:13
5 direct replies by markuhs
    Hi Monks,

    I learned about three-way open and wanted to use it. Everything seemed OK. Then I encountered problems with one particular script. It opens a file and writes to it. Normally no problem. The usage, however, caused some problems.

    The script is used in the following two ways:
    C:\>perl script.pl outfile C:\>perl script.pl ">outfile"
    The second is used to append the data produced by the script to the already existing data.

    Hmm... Three-way open brought me from open $filehandle ">$outfile" to open $filehandle ">", $outfile" which causes the trick not to work anymore.


    First take on it:
    my $openmode = '>'; if($outfile =~ m/\A>/xms) { $openmode = '>>'; $outfile =~ s/\A>//xms; } open($filehandle, $openmode, $outfile);
    Any suggestions how to do this more elegant?

    UPDATE: as per comment of bellaire (>outfile changed to ">outfile").

[Offer your reply]
How to creater your own Perl modules/libraries?
on Nov 20, 2009 at 01:24
2 direct replies by newbie01.perl

    Hello to the Perl Highness ... :-)

    Am not really sure whether I want modules or libraries. Am very confused so hopefully I can find the answers here.

    I have several Perl scripts that are currently running as cron jobs. I want to be able to combine them as some sort of module or library or package instead that I can just call from another Perl script.

    That is, for example, if I have a script script01 that remove old files and script script02 that monitors filesystem space. I want to be able to pout these two scripts into some sort of module or library that I can just invoke from another script that make require them. Is this possible or am I talking about senseless stuff here.

    Your guidance will be very much appreciated


[Offer your reply]
Auto-incrementing strings in for loops
on Nov 19, 2009 at 22:59
1 direct reply by JadeNB

    It seems to me that the first and second code paragraphs below should behave identically, and the third should behave nearly identically (except that it increments only once). As shown, the behaviour is quite different (on Perl 5.10.1 and, with the obvious modification, Perl 5.8.9). Am I missing something obvious?

    $_ = 'a'; $_++ and say; # => 'b' $_++ and say; # => 'c' $_ = 'a'; $_++ and say for 1..2; # => '2', '3' $_ = 'a'; $_++ and say for 1; # => Modification of a read-only value attempted

    UPDATE: Oops, for aliases $_, so I'm not aliasing incrementing the $_ that I think I'm aliasing incrementing. Sigh, sorry ….


[Offer your reply]
Perl to PDF
on Nov 19, 2009 at 17:37
2 direct replies by CColin
    Hi, Am about to try and write some output to PDF for the first time. I see a bewildering array of modules, with lots of kudos for PDF::API2 but it seems only for those who already know how to use it rather than learn it. I've also looked at PDF::Create - where I'm struggling with all are some decent code examples that show how to write lots of text output. Can't writing a PDF be like writing a file? Is there not some example code that shows how to do this? Thanks for any pointers

[Offer your reply]
Building perl for appliances
on Nov 19, 2009 at 13:42
1 direct reply by Anonymous Monk
    Hi, would appreciate your help on this.

    We're building a Linux-based appliance with perl, all the software (linux kernel, perl, applications) are build from source, into an staging area, then packaged together. We managed to get perl into the build process. My question is how to include extra packages (e.g., DBI, Crypt, etc.)

    (1) When/where to include a specific package (XS extensions)? I can add the package source into the source "ext" directory, and build as a part of the base perl; or adding later when the base perl has been built (the usual Makefile.pl/make/ processes.

    (2) The second question is how to resolve all dependencies, do I need to add one package at a time, i.e., download the source, put into the build, and figure out what it depends on. When I use cpan, some of these dependencies can create lots of other dependencies, I certainly don't want them all. Is there an easier way?

    Thanks.

[Offer your reply]
given-when last time through loop?
on Nov 19, 2009 at 08:45
1 direct reply by 7stud

    Dear Monks,

    I'm using given-when in a for loop:

    use strict; use warnings; use 5.010; my @arr = (1, 2, 4); for (@arr) { print "$_: "; when (1) {print "1"; continue} when ($_ % 2 == 0) {print "divisible by 2"; continue} when ($_ % 4 == 0) {print ", divisible by 4"} say "\n------end this iteration"; }

    Every time through the loop the last say statement executes--except the last time through the loop. Why? If I add a continue statement to the last when block, then the last line will execute for the last iteration of the loop.

    Also, "Learning Perl 5th" says there is an implicit break at the end of each when block, but I get errors if I do this:

    use strict; use warnings; use 5.010; my @arr = (1, 2, 4); for (@arr) { print "$_: "; when (1) {print "1"; break} when ($_ % 2 == 0) {print "divisible by 2"; break} when ($_ % 4 == 0) {print ", divisible by 4"; break} say "\n------end this iteration"; } --output:-- Can't "break" in a loop topicalizer at 1perl.pl line 10. 1: 1

[Offer your reply]
generating merged data from matching id in 2 files
on Nov 19, 2009 at 05:01
2 direct replies by ramouz87
    Hi, I'm new in perl and would be intrested to have suggestion from experienced people on how to write optimized code for the following task:
    I work with genomic data filesize higher than 1Go and have 2 files with lines that looks like this:
    ** A line from file1:
    HWI-EA332_91026_1_1_7_586#TCTTAT/1 + Chr3 67121130 TATTNTAAGTCTATGTTGGGGGGGTGGTCATTGAATGTAAGNTGGGTCTC
    ** A matching line from file2:
    HWI-EA332_91026_1_1_7_586#ACATAA/2 - Chr7 127074854 AAAATAAAGCTNATCTGGAAGCAACAGTANGAAGCAGAAGACTGNACACC
    The id is the subsring EA332_91026_1_1_6_683 identified by  my @token = split('\-|\#', $line);
    What i want to do is:
    for each id in file 1 look if same id exist in file 2 and when there's a match:
    * I add the matching line from file 1 followed by the matching line from file 2 to a new file (matched_pair)
    * based on a tab delimited split of the line take column 2/3 from the matching line in file1 and file2 and the absolute value of the difference between column 4 in file1/file2 (gap_dist) and the id.
    Based on the example above we expect to have this line :
    EA332_91026_1_1_7_586 + Chr3 - Chr7 59953724
    Thanks in advance for your kind help.
    Regards,
    Ramzi

[Offer your reply]
How to get a programs execution flow
on Nov 19, 2009 at 02:10
1 direct reply by klon_u
    Hi, Would like to know in perl, how to get the flow of the perl script with each executed line (command) in expanded form, as in shell scripts we can get the flow using -x and -v flags For eg. sh -x -v scriptname >script.out 2>&1

[Offer your reply]
Password Regex extended
on Nov 18, 2009 at 13:47
5 direct replies by 3dbc
    Hello perlmonks, I'm trying to extend this regex to match 3 of 4 submatches.
    ^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$
    Not 4 of 4 like it's currently doing.
    Update 3:
    $1 is a requirement, I.E. $2, $3, $4, $5 - if any three {3,4} or more match then it's a match.

    Update: I enjoy perl, but also enjoy perlre and If possible would prefer a regex centric solution. I.E. is there a way to due this with one line of regex or do we need perl to do the 3 of 4 logic?

    Update 2: Here's a description of what the current regex should do:
    * Must be at least 10 characters
    * Must contain at least one one lower case letter, one upper case letter, one digit and one special character (this should be 3 or more...)
    * Valid special characters are - @#$%^&+=
    All advice is appreciated. Thanks,

[Offer your reply]
require different modules per OS?
on Nov 18, 2009 at 09:40
6 direct replies by Anonymous Monk
    I have a script that needs to run on both mac and win32 but I really don't like keeping two copies of the script so instead I would like to load only the required modules for win32 and vice versa. Here is my example:
    if ( $os =~ /win32/i ) { use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>0 );} }
    It seems that this block of code is evaluated despite the fact that $os isn't win32 and errors out requiring the Win32::TieRegistry even though I'm running it on a mac.

    Any thoughts?


[Offer your reply]
New 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]
New Cool Uses for Perl
Using Icons on Wx::Notebook
on Nov 18, 2009 at 12:09
0 direct replies by Steve_BZ

    Here a small piece of code showing how to create a Notebook with icons at the side (think MSN). In this example you need two icons called "users.png" and "schedule.png" 16 x 16 pixels in directory "C:\Documents and Settings\Steve\My Documents\Projects\wx" and set the $loc_windows_linux flag to "l" or "w"


[Offer your reply]
Passing negative numbers on the command line with Getopt::Long
on Nov 18, 2009 at 09:34
2 direct replies by toolic
    If you are a fan of Getopt::Long and you need to pass negative numbers to your script from the command line, here is a code snippet which can help.

    By default Getopt::Long reacts to command line arguments with a dash (-). Thus, if you want to pass -5 on the command line, Getopt::Long will strip off the minus sign and treat that as the option '5'. That will result in an error if you are checking the results of GetOptions.

    You can change the default behavior by setting the prefix to something other than '-'. For example, in the code snippet below, prefix is set to the plus sign (+). Refer to Configuring Getopt::Long. Naturally, you should make sure this is well-documented in your usage POD.

    use strict; use warnings; use Getopt::Long; Getopt::Long::Configure('prefix=+'); my %opt; GetOptions(\%opt, qw(help offset=i)) or die "error...\n"; $opt{offset} = 3 unless exists $opt{offset}; my $value = shift; print ' result = ', $value + $opt{offset}, "\n";

    Here is how it looks from the command line (I called the script add.pl):

    $ add.pl 5 result = 8 $ add.pl -5 result = -2 $ add.pl +offset -2 12 result = 10

    I recently had the need to do such a thing, but I could not find any examples of using this feature. Perhaps this will be of use to someone in the future. I uploaded my actual script to CPAN: convert_eng


[Offer your reply]
New Monk Discussion
[mod://] shortcut with named anchors
on Nov 16, 2009 at 12:17
1 direct reply by almut

    When linking to resources I prefer to use named anchors (if available), so people don't have to go searching for what exactly I'm referring to on the linked page.  With the shortcut [mod://], however, this doesn't seem to be possible, because special characters like # or % are being URI-escaped, which produces hrefs that don't work (example).

    I typically end up using a generic [http://...] with a URI like what would be created by [mod://]:

    [http://search.cpan.org/perldoc?pp#-a,_--addfile=FILE%7cDIR|my link +text]

    (which doesn't have the problem), but it would be nice to have it working directly with the shortcut in the first place, i.e.

    [mod://pp#named-anchor|my link text]

    (as it is, it typically takes two steps for me, because I don't seem to be able to remember the exact URI that's being generated by the shortcut...)

    So I'm wondering if there is a way to somehow quote/escape special characters (such that they aren't URI-escaped by PM), or what's the proper way to link to anchors.  Or is this maybe a browser issue? (I'm using Firefox)

    (My apologies in case this has already been discussed — a quick search turned up a number of nodes, but none of those did really apply to this specific issue.)


[Offer your reply]
Login:
Password
remember me
What's my password?
Create A New User

Community Ads
Chatterbox
and all is quiet...

How do I use this? | Other CB clients
Other Users
Others meditating upon the Monastery: (14)
GrandFather
graff
planetscape
Your Mother
atcroft
herveus
thezip
MidLifeXis
Eyck
davies
$self
ssandv
gnosti
im2
As of 2009-11-20 23:35 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 (725 votes), past polls