Perl Monks - The Monastery Gates
Description: A community committed to sharing Perl knowledge and coding tips. The site contains questions and answers, useful snippets, and a library of code.
Keywords: perl, mod_perl, regular expressions, regexp, xp whoring, CGI, programming, learning, tutorials, questions, answers, examples, vroom, tim, node, experience, votes, code

| Perl Monks

### The Monastery Gates | | log Ctrl-z out | Ctrl-z | The Monastery Gates | Super Search | | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | Snippets | | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | | Code | Poetry | Recently Active Threads | Newest Nodes | Offering Plate |

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

Donations gladly accepted Want Mega XP? Prepare to have your hopes dashed, join in on the: poll ideas quest (11840 days remain)
New Questions
store/retrieve data structure in MySQL on Jul 31, 2005 at 16:25 | 2 replies | by BarMeister
`++` `--`
I was wondering if what I am attempting is even possible...I have seen a few references to it while searching but haven't found a definitive answer yet. I am trying to insert/update/retrieve a data structure in MySQL. My question is what do I pass in as the value when using bind_param? I am inserting it into a BLOB data type. The data structure is an array of objects (hash refs). So is it possible to do this or do I have to serialize/deserialze the structure first? Thanks, Kenny | [Offer your reply]
Differences between " " and ' '? on Jul 30, 2005 at 22:55 | 4 replies | by Ace128
`++` `--`
Hey Monks, This time I have a more easier question. Now, doing alot of string comparisons:
    my $text = "pomperipossa";
    if ($text eq "pomperipossa") { .. }
I was wondering if there is _any_ speed difference between doing:
    if ($text eq "pomperipossa") { .. }
and:
    if ($text eq 'pomperipossa') { .. }
? Would also be nice if you experts could elaborate more on this. If some cases are better with "" than with ''... (yea, I know about the difference when it comes to $variables! so it seems that ' ' would be faster! Here all is just outputed as it is, compared to " "! But is that the same for eq, ne etc?) Thanks, Ace | [Offer your reply]
choosing a CPAN namespace on Jul 30, 2005 at 10:18 | 3 replies | by rvosa
`++` `--`
Dear fellow monks, I am planning to submit a new module to CPAN. The intended audience consists of evolutionary biologists, who may find the module useful for parsing, analyzing and manipulating phylogenetic trees. The working title / temporary namespace has been Phylo::, but I gather there is some resistance to introducing new top-level namespaces. I have been trying to find out whether I can use Bio::Phylo, but I'm not sure whether the Bio:: namespace is reserved for BioPerl (which I'm not a part of). I have posted on the BioPerl mailing list trying to find out if that's the case, but I didn't get a response. Perhaps someone here knows whether I can use Bio:: without being a contributor to the open bioinformatics foundation? Thanks, Rutger | [Offer your reply]
Fixing suspect characters in HTML on Jul 30, 2005 at 08:02 | 1 reply | by wfsp
`++` `--`
Due to carelessness on my part I had a shed load of html containing suspect characters. The difficulty was having a possible combination of x80-x9F (frowned on by w3c), unicode and html entities (including numeric entities). The strategy I arrived at was to: decode any entities present convert x80-x9F to unicode equivalents encode 'unsafe' characters This will ensure, hopefully, consistant html and prevent problems during any future processing. What do you reckon? Read more... (1363 Bytes) | [Offer your reply]
Reading a MySQL table into an array on Jul 30, 2005 at 07:22 | 1 reply | by davies
`++` `--`
Dearly beloved brethren, In my personal pilgrimage to enlightenment, I am trying to convert a VB6 programme to Perl. This programme reads Excel files, writes them to MySQL, hacks the database around and produces more Excel files. Because Excel and VB6 use different connections to MySQL, I cannot make the final output tables (that are to be passed from MySQL to Excel) temporary files, because the Excel connection would be unable to see them. However, MessWare crashes far more often than I like. When it does this, it may leave tables on the database that are no use to monk or beast. I therefore want some code available that reads all the database tables, compares them to a list, and deletes them if they are not in the list. Now, I know how to read data from a MySQL table and put it into a Tk listbox. This was taken from one of the man pages or docs, and works perfectly in a variety of situations. My plan, therefore, was thus: 1) Create a table listing the tables that should exist (including itself!). 2) Read the list of tables that actually exist into an array. 3) Run through the array, comparing each table to the table of table names, deleting the table if it isn’t in the table of tables. (1) is easy and complete. But (2) has got me stumped. I get either one table name only, or the right number of tables, but all called 1. I have created a module called SQL, which gets called from any number of other modules. Strict, warnings and diagnostics are in every file. The relevant parts of the SQL module are: Read more... (2 kB) | [Offer your reply]
What is the problem of subroutine "make_file" in my code? on Jul 29, 2005 at 23:04 | 2 replies | by everwood
`++` `--`
Dear all, Sorry to bother you. I need some help on my code. I have an input file named "origin8.txt" which holds 200 short sequences of width 8. My code is to use each short sequence from "origin8.txt" as a template to generate 100 short sequences of the same width and store them in a txt file A (A stands for `short_sequences8_[$y].txt` in my code). Then the code will read 100 short sequences from the txt file A and 100 long sequences of width 200 from a txt file B , and then replaced a substring of each long sequence using each short sequence. This code will lead to two txt files C and D. File C will hold 100 replaced long sequences. In other words, I want to input "origin8.txt" to get 200 File C (C stands for `output8_[$y]2.txt` in my code). My code can generates 200 File D but each of them holds nothing. So I guess the problem is caused by a failure of passing the data to a subroutine named "make_file". Can anybody suggest me how to modify that? Thank you very much in advance! Sincerely, Alex My code: Read more... (5 kB) Edit by castaway - added code tags | [Offer your reply]
Table::Extract unblessed reference on Jul 29, 2005 at 18:49 | 3 replies | by thekestrel
`++` `--`
Hi Monks, I'm trying to parse some table content out of a HTML page and was having some trouble so I've whipped up a dummy table and script and I'm getting an error stating that I 'Can't call method "rows" on unblessed reference at ./s.pl line 25'. Here is the dummy html file...
    <html>
    <body>

    <table align="center">
    <tr><td>Name</td><td>Blah</td></tr>
    <tr><td>bill</td><td>fdagdfg</td></tr>
    <tr><td>ted</td><td>sdfsdf</td></tr>
    </table>

    </body>
    </html>
and the script....
    #!/usr/bin/perl

    use warnings;
    use strict;
    use HTML::TableExtract;

    #my $te = HTML::TableExtract->new ( depth => 1, count => 2 );
    my $te = HTML::TableExtract->new ( attribs => { align => 'center' } );

    print "Start\n";

    my $data;
    {
    local( $/, *FILE);
    open FILE, "blah.html" or die "Could not open file , $!\n";
    $data = <FILE>;
    close (FILE);
    }
    print $data;

    $te->parse($data);
    print "table stuff:\n";
    foreach my $ts ( $te->tables ) {
            print "Table\n";
            foreach my $row ( $ts->rows ) {
                    print "R        ", join(',', @$row), "\n";
            }
    }
I'm sure this is probably quite simple, but at this hour its eluding me so I was hoping someone might be able to explain what is going on. Regards Paul | [Offer your reply]
handlecount of win32 process on Jul 29, 2005 at 18:07 | 1 reply | by slloyd
`++` `--`
Does anyone know how to get the handlecount of a win32 process using Perl.. I cannot seem to find a Win32 API for it.. http://www.basgetti.com | [Offer your reply]
How much can a function know about the context in which it is called? on Jul 29, 2005 at 16:01 | 6 replies | by jacques
`++` `--`
I have a bunch of calls to the same function. The function ultimately prints to STDOUT. I want to change the function to return a string rather than print it. So that I can say:
    print function(); # prints the string that function returns.
Easy enough. But instead of putting the print before the funtion call, I would like to simply say:
    funtion(); # prints string since not assigned

    $value = funtion(); # Does not print. $value holds the return string.
In other words, I want function() to know the context in which it is called, so that it knows whether to print a string or to return one. And I don't want to use IO::Scalar. Is this possible? | [Offer your reply]
Re-Interpolating a Scalar (a string) on Jul 29, 2005 at 14:22 | 3 replies | by Nichodemus
`++` `--`
I am relatively new to Perl -- I figure there must already be a way to do this without writing my own custom subroutine. Here's my question: How can I force a scalar variable to be re-interpolated? For example, lets say that this line of code: `print $raw;` returns: `fred has $color eyes` What I want to be able to do is later set $color to equal "blue." And then have: `$parsed = # ??? something to re-interpolate $raw` `print $parsed; # I get the text "fred has blue eyes"` Anyone have any ideas? -Nich | [Offer your reply]
problems using SQL::Statement on Jul 29, 2005 at 09:25 | 2 replies | by reneeb
`++` `--`
Hi monks! I want to parse a CREATE-Statement, but that doesn't work. I've tried the following code:
    #!/usr/bin/perl

    use strict;
    use warnings;
    use SQL::Statement;

    my $statement = qq~CREATE TABLE test(
      ID int not null,
      Spalte1 varchar(255),
      Spalte2 int,
      Spalte3 int,
      Spalte4 int,
      Spalte5 blob,
      Spalte6 varchar(33),
      primary key(ID),
    )~;

    my $parser = SQL::Parser->new();
    my $stmt = SQL::Statement->new($statement,$parser);
But I've got the following error-message: `SQL ERROR: 'BLOB' is not a recognized data type!` So I've changed for testing the module this data type, then I got an other error-message: `SQL ERROR: Bad table or column name 'primary' is a SQL reserved word!` I'm working with perl5.8.6 and module's version 1.14 So, how can I use the module for SQL92-standard-statements? Would you use an other module? Thank you, Renee | [Offer your reply]
Match number >= 0 and <1 on Jul 29, 2005 at 09:19 | 7 replies | by lupey
`++` `--`
Hello, I'm looking for criticism of my regular expression that looks for a number to be >= 0 and < 1 (i.e. 0, 0.00001, and .99999 are OK but 1, -0.00001, and -1 are NOT OK). I'm using the RE to untaint a number from a CGI form. The regex seems to work and is based on a regex in PerlFaq4 that matches decimal numbers. Here is the code in my CGI script that handles the untainting. `my ($num) = $_num =~ /^(0(\.\d+)?|\.\d+)$/;` Please comment on the accuracy and if there is a better way to do it. Thank you, lupey <bold>Update</bold> Thank you everybody for your suggestions. I seem to like tlm's answer the best. I didn't realize that I could separate tainting from checking the numerical properties of a scalar. | [Offer your reply]
New Meditations
(OT) Pushing inductive systems to their limit on Jul 29, 2005 at 15:13 | 6 replies | by Ovid
`++` `--`
This is an expansion of something in my use.perl journal where I explained how logic programming works. What follows is a brief and over-simplified overview of what lies at the core of automated deduction and logical inference systems the world over. In short, it's at the heart of many AI projects. Even though it's one programming paradigm that's not natively supported by Perl, it's surprisingly simple. If the following interests you, check out AI::Prolog, Logic or Language::Prolog::Yaswi. If you'll be at OSCON, I'll be talking about the first module. I was looking at Luke Palmer's Logic module on the CPAN and it got me to thinking a bit about logic programming in general, as opposed to the specific implementation of Prolog I've been focusing on. To implement logic programming, you basically have to implement two things, backtracking and unification. You already understand backtracking from regular expressions. If you fail to find a match (in our case, if we fail to unify two data structures), back up to the last point in the string (stack) where we can make a different decision and try again. It's pretty straightforward. Unification is also fairly simple. You know it from makefiles or SQL, but we'll look at logic programming in particular. Imagine that you have two five element lists:
    ( 1, 2, undef, undef,     5 )
    ( 1, 2,     3,     4, undef )
Imagine that `undef` means "unknown". We can unify those two lists because every element that is known corresponds in the two lists. This leaves us with a list of the integers one through five. `( 1, 2, 3, 4, 5 )` However, what happens if the last element of the first list is unknown?
    ( 1, 2, undef, undef, undef )
    ( 1, 2,     3,     4, undef )
We can still unify the two lists. In this case, we get the same five element list, but the last item is unknown. `( 1, 2, 3, 4, unknown )` Logic programming works by pushing these lists onto a stack and walking through the stack and seeing if you can unify everything (sort of). But how to unify from one item to the next? We assign names to the unknown variables and see if we can unify them. When we get to the next item in the stack, we check to see if any named variables have been unified. If so, we unify the individual variables prior to trying to unify items with their counterparts in our lists. Thats a bad explanation, so here's how it works. Imagine the following "knowledge base" of lists: Read more... (5 kB) | [Offer your reply]
Properties, metadata and Perl celebrities on Jul 29, 2005 at 10:18 | 3 replies | by eyepopslikeamosquito
`++` `--`
A famous Portuguese Perl hacker, author of the recent mad Acme::AsciiArt2HtmlTable module and I have been pondering the best way to classify the shapes in (the also mad) Acme::EyeDrops module to make them more amenable to searching and selecting. You see, he wants, for example, to select from the many shapes available, only the faces of Perl celebrities. I'm struggling a bit, not having much experience in this field. My first instinct was to look at the web site of a famous British Perl hacker to learn how to search his magnificent photo collection. acme seems to use some sort of keyword search, but when I typed in "perl celebrity" I was told to "Please come back later" in eight different languages. My next instinct was to steal an idea I remember from Subversion, that of allowing arbitrary "properties" (key-value pairs), aka metadata, to be associated with a file. Read more... (2 kB) | [Offer your reply]
OSCON - sessions in other tracks on Jul 29, 2005 at 10:06 | 4 replies | by jimX11
`++` `--`
Recently I realized that I was only considering attending sessions in the Perl track for OSCON. What sessions, outside the Perl track, interest you? | [Offer your reply]
Autovivification for dummies (using Test::More) on Jul 28, 2005 at 14:51 | 1 reply | by tphyahoo
`++` `--`
Here's a little snip I put together messing around with autovivification. Perhaps some monks that feel queasy about this topic may find it useful. For a more in depth tutorial on the topic The Bad, the Ugly, and the Good of autovivification may be of interest. Cheers :)
    use strict;
    use warnings;
    use Test::More qw(no_plan);
    use Data::Dumper;

    my %hash = (a => 'a', b => 'b');

    ok( ! exists($hash{c}), "hash -- c doesn't exist");
    ok( ! exists($hash{c}), "hash -- c still doesn't exist");
    ok( ! defined($hash{c}), "hash -- c not defined either");

    my $hashref = { %hash };

    ok( ! exists($hashref->{c}), "hashref -- c doesn't exist");
    ok( ! exists($hashref->{c}), "hashref -- c still doesn't exist");

    ok( ! exists( $hashref->{c}->{d} ), "hashref, deeper -- c->{d} doesn't exist");
    ok( ! exists( $hashref->{c}->{d} ), "hashref -- c->{d} still doesn't exist");
    ok( ! exists($hashref->{c}), "hashref -- c still doesn't exist"); #fails. c does
    + exist.
    ok( ! defined($hashref->{c}), "hashref -- c still doesn't exist"); #fails. c is
    +an empty hashfref, which is something other than undef.
| [Offer your reply]
New Cool Uses for Perl
Utility to capture parameters and perform a task on Jul 28, 2005 at 22:24 | 1 reply | by GrandFather
`++` `--`
This code is a part way point in the development of a tool to perform some directory and file management. At this stage it captures information required for the management from the user. The information gathered to controlled by a configuration file. This seems a generally usefull task so the code is offered here for other monks to make what use they wish of it. The configuration files is comprised of lines containing a parameter name, and optional default value, and a label. For example: `_version_ "1_0" Version as it should appear in the project path (e.g. 1_0)` If the default string contains names of other parameters, the default value for the parameter will be generated at run time as the other parameters are updated. The work of the task goes in `sub CreateProject` (which is an appropriate name for my initial task :). Read more... the code (3 kB) Read more... sample template file and output (924 Bytes) Perl is Huffman encoded by design. | [Offer your reply]
iTunes and Windows and Perl == Bliss on Jul 25, 2005 at 14:21 | 3 replies | by mdog
`++` `--`
Brethern -- I have been waiting for quite some time for an API into iTunes so that I could control it from my keyboard or X10 remote controls and they released an SDK awhile back that I just discovered. I know that you Mac users have Mac::iTunes but Windows users can now control iTunes natively. James Craig has done some excellent work with the SDK and I borrowed the "SearchiTunesWinForSong" routine from him. I am going to come up with a more complete library of functions as time allows but wanted to get this out so folks know that they can start hacking away at iTunes. Enjoy, mdog Read more... (6 kB) READMORE tags added by Arunbear | [Offer your reply]

XP Nodelet
You have 20 votes left today.
Personal Nodelet
Nodes to consider Windows Threaded Proxy Server Push HTTP server UDP sniffer Simple UDP example anyone? Re: Sending Rich/HTML email Re: Help from the very start. CGI.pm vs Apache::Request CGI::Application + Net::HTTPServer Advice on HTTPServer problem please Pack/Unpack Tutorial (aka How the System Stores Data) Manage or Add to pub / priv pad or nodelet
Chatterbox
• Plus 44 archived <tye> Drat! DBD::AnyData doesn't suppport 'left join x on' <Moriarty> My machine is on a go slow. Time to reboot. biab. (@!$#! Windoze). How do I use this? | Other CB clients
Other Users
Others scrutinizing the monastery: (21) Zaxo Mr. Muskrat tlm atcroft castaway holli bradcathey Moriarty esskar hackdaddy TilRMan bpphillips cazz jbullock35 BaldPenguin GaijinPunch cyclist38 siracusa izut prlmnks.org snoopy As of 2005-07-31 23:52 GMT