Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl Sensitive Sunglasses
 
PerlMonks

The Monastery Gates

 | Log in | Create a new user | 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 | Recent Threads | Newest Nodes | Donate | What's New | 

( #131=superdoc: print w/ replies, xml ) Need Help??
Donations gladly accepted
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 2007  (10274 days remain)

New Questions
Redirects with query string
on Dec 02, 2008 at 02:57
3 direct replies by htmanning
    Hi Monks, This isn't exactly a Perl question but I wonder if someone knows how to handle .htaccess redirects with a query string. I'm trying to forward some phpbb forums. This doesn't work:
    RewriteEngine on
    RewriteRule /forums/viewtopic.php?f=12 http://www.newdomain.com/forums
    +/
    
    It seems the query string hangs it. Any ideas?

[Offer your reply]
Best practice for user-input in eval
on Dec 02, 2008 at 02:12
3 direct replies by humbledisciple
    I know, but it can't be avoided. I need to allow a user, novice or experienced (even with malicious intent), to input a perl expression or program and have it run over string. Here's the setup. This is done in a web context where the user can enter a simple or complex perl program. I will feed the user's program an input string and it should return an output string back to me. Obviously, I don't want the user to trash my hard drive, open files, etc. Should the eval operation be wrapped in an exception handler? Can I restrict the access to my file system? Is there a way to prevent the user from running and infinite loop expression and thus bring my server to a halt?

[Offer your reply]
A caller() by any other name
on Dec 01, 2008 at 13:24
4 direct replies by Bloodnok
    Hello again, all you venerable chaps & chappesses,

    After a relatively prolonged absence (from cluttering this forum with my trivial perl questions), I thought it was once again time I bolstered my candidature for the useless brain-dead question of the year award (it makes a change from pressing for the equivalent award for answers/responses:-)), so here goes...

    In my current incarnation, I've encountered numerous situations of late whereby a called method could do with establishing, not just the package/sub i.e. class method details, but the instance i.e. object, details of the caller - the currently vexatious situation revolves around the implementation of a dynamic bi-directional relationship between classes and indeed, objects.

    To my (obviously inadequate) mind, the self-evident answers have, if not problems, then at the very least, limitations...

    • Passing the caller class/instance as an argument looks not a little inelegant and also IMO, violates one, or more, OO rules & best practice(s).
    • The use of inheritance is, apart from being frowned upon by the powers that be (since composition is the new inheritance), not applicable in all but one of the instances (pun intended)
    IMHO there ought to be a method to identify such details in a more elegant, perl-like manner - in either...
    • A manner similar to that of the automatically supplied called class/instance details or...
    • An extension to caller() such that either the package or an instance (dependant on whether it was called by a package sub/class method or an object method) is returned as the 1st element of the list.
    Question is, is it just me ... or do any of you have a view &/or an alternative solution ?

    OTOH, maybe in addition to the above, the best solution would be to find/write a class to implement class/instance inter-relationships whereby each side of the relationship knows about both the...

    • The class on the 'other side' of the relationship & the ...
    • Arity of its own side of the relationship

    .oO(...wonders whether if, in the light of 20/20 hindsight, this node might not be re-housed as a meditation - if the nodereaper can leave it alone that long:-)

    A user level that continues to overstate my experience :-))

[Offer your reply]
"use"ing from dynamic namespace
on Dec 01, 2008 at 11:32
5 direct replies by esper
    I've been looking over my old code lately and finding a lot of projects where one has Foo::Module and another has an extremely similar Bar::Module, so I'm thinking the thing to do is factor out that commonality into Base::Module and derive both of the others from it.

    Simple enough.

    However, Foo::Module references Foo::Config, Bar::Module references Bar::Config, and each needs to talk to different databases, if nothing else, so the referenced modules need to remain distinct for each app's namespace, but I've had no luck in trying to track down a conventional way of accomplishing this.

    What I've come up with so far is (in Base::Module):

    BEGIN {
      my $ns = __PACKAGE__;
      $ns =~ s/::[^:]*$//;
    
      eval "require $ns\::Config";
      "$ns\::Config"->import(qw(x y z));
    }
    
    as a replacement for use xxx::Config qw(x y z) which should pull in Base::Config from Base::Module, Foo::Config from Foo::Module, and Bar::Config from Bar::Module, but it really feels horribly ugly and clumsy to me.

    Is this the standard way of accomplishing what I'm trying to do or is there a better solution out there?


[Offer your reply]
From Perl 5.10 to Perl 6
on Nov 30, 2008 at 22:05
4 direct replies by Anonymous Monk

    Hi all,

    I'm about to start on a new web project using Perl. Currently I've Perl 5.10 installed on my computer, so my code will be tried and tested on this version. I've a feeling most hosting companies are still using Perl versions higher than 5 but below 5.10, so I think my code that's written for 5.10 should still run on these lower versions.

    But will it run on Perl 6 when it becomes the default Perl installation? Will things like "use" or "autouse" still be used in Perl 6?

    Thanks in advance!


[Offer your reply]
Knight's Tour Problem in Perl
on Nov 30, 2008 at 20:44
4 direct replies by ptoulis
    There was an article yesterday in Shashdot about writing a Python script that solved the Knight's Tour Problem in 60 lines and less than 1 second for a 100x100 board. PerlMonks have already been encountered with this problem in this node but the question remained unanswered. So, I spent some time and made a Perl script, much shorter (35 lines) and faster (<<1sec) for the same benchmark. If you feel like spending some leisure time over the problem and try to squeeze more the Perl solution, I would be happy to hear some thoughts. Here is my code.
    if(@ARGV<4) {print "\nUse: SIZE START_X START_Y [0,1]" and exit;}
    our ($N,$X0,$Y0,$VERBOSE) = @ARGV ;
    our ($MOVES,@moves,@board)= (0,([-2,-1], [-1,-2], [-2 ,1],[-1 ,2], [2 
    +,-1], [1, -2],[2, 1],[1, 2]),());
    my $last_move = [$X0,$Y0] and inform_board();
    $last_move  = inform_board($last_move) while($MOVES< $N*$N);
    ###############  SUBROUTINES ########################
    sub init { return ($_[0]>$N/2)? init($N-1-$_[0],$_[1]) : ($_[1]>$N/2? 
    +init($_[0],$N-1-$_[1]):($_[0]>=2 && $_[1]>=2? 8:(($_[0] >=2 && $_[1]=
    +=1 || $_[1]>=2 && $_[0]==1)?6:($_[0]+$_[1]>1?4:($_[0]+$_[1]==1?3:2)))
    +) )}
    sub inRange{
    my ($pos, $D) = @_;
    my ($x,$y) = ($pos->[0]+$D->[0],  $pos->[1]+$D->[1]);
    return  ($x>=0 && $x<$N && $y >= 0 && $y<$N && $board[$x][$y]>=0)?    
    + [$x,$y] : 0;
    }
    sub inform_board {
    my ($moved_to) = @_;
    if($moved_to)
    {
    $MOVES++;
    print "\n$MOVES.",$moved_to->[0],",",$moved_to->[1] if $VERBOSE;
    my ($MIN,$next_to_move) = (10,[]);
    foreach(@moves)
     {
     if(my $finalPos = inRange($moved_to, $_))
      { 
      my $value_ref = \$board[$finalPos->[0]][$finalPos->[1]];
      ($MIN,$next_to_move, $board[$moved_to-> [0]][$moved_to->[1]]) =     
    +($$value_ref,$finalPos,0) if(--$$value_ref>=0 && $$value_ref < $MIN);
    + } }
     return $next_to_move;
    }
    else
     {
     push @board, [(0)x $N] for(1..$N);
     for my $i (0..$N-1)  { $board[$i][$_] = init($i,$_) for (0..$N-1);}
      } }
    


    Some hints: The Warnsdorff's algorithm is used for the solution.
    init() initializes the board
    inRange() Says is a knight move is inside the board limits
    inform_board() Marks the last-visited square of the board and returns the next square to move according to the algorithm

[Offer your reply]
Practical Example of Converting Packed Value
on Nov 30, 2008 at 19:04
1 direct reply by Jim
    I've never groked converting bit vectors, bitmaps, packed fields or any Perl bitwise operations and it's high time I do. I want to learn something using a practical example of my own that I kind of understand already rather than trying to puzzle through unfamiliar examples in a book, manpage or tutorial.

    Let's say I have these two bytes in a variable: 0xA8 0x38. ("Are these two bytes a string or a 16-bit integer?" you ask. I don't know; you tell me. This is part and parcel of my profound ignorance of this stuff. Pretend I just read the two bytes from a file into a variable.) These 16 bits are a packed representation of an MS-DOS FAT date.

    • 5 bits 0–4 = Day (1–31)
    • 4 bits 5–8 = Month (1 = Jan, 2 = Feb, etc.)
    • 7 bits 9-15 = Year offset from 1980

    I want to use Perl bitwise operations to convert this 16-bit integer (or string) to the date that it represents: 2008-05-08. Here's the conversion as I (naïvely) understand it:

    A8       38         Hexadecimal (Little Endian)
    10101000 00111000   Binary (Little Endian)
    
    38       A8         Hexadecimal (Big Endian)
    00111000 10101000   Binary (Big Endian)
    11111100 00000000
    54321098 76543210   Bit Ruler (Indexed From 0)
    
    0011100 0101 01000  Binary (Regrouped)
    1111110 0000 00000
    5432109 8765 43210  Bit Ruler (Regrouped)
    
    Year    Month Day   Packed Format
    
    0011100 0101 01000  Binary
    1C      05   08     Hexadecimal
    28      5    8      Decimal
    
    Year  = 1980 + 28 = 2008
    Month = 5
    Day   = 8 
    
    MS-DOS FAT Date = 2008-05-08
    
    How do I accomplish this conversion in Perl? All hints are welcome. Remember: I'm seeking genuine learning more than a solution to a problem -- and especially not the answer to a homework problem, I promise. IANAS!

    Jim


[Offer your reply]
sys/ioctl.ph broken in perl 5.10?
on Nov 29, 2008 at 22:20
1 direct reply by bcrowell2

    O monks,

    If I do  perl -e "require 'sys/ioctl.ph'" on most Linux systems, including Ubuntu Hardy, it works, but on Ubuntu Intrepid I get

    Prototype mismatch: sub main::__LONG_MAX__ () vs none at /usr/lib/perl
    +/5.10/_h2ph_pre.ph line 309.
    Constant subroutine __LONG_MAX__ redefined at /usr/lib/perl/5.10/_h2ph
    +_pre.ph line 309.
    

    Is this just a bug in perl 5.10? Can anyone reproduce this in perl 5.10 on a non-ubuntu system? If it is a bug, what's the best way to report it?

    TIA! -- Ben


[Offer your reply]
What is the use de-referencing and creating a new anonymous hash in new() and rnew() ?
on Nov 28, 2008 at 09:40
4 direct replies by sathiya.sw
    I have been going through the module Devel::Symdump.
    There i have seen a line of code which i would want a better clarification.,
    my $self = bless {%${"$class\::Defaults"}}, $class;
    
    It is a reference and dereferencing operation ? Am i right?? Or else some thing is also achieved here?
    Why this is needed ? What is the intension in doing this ??
    Sathiyamoorthy

[Offer your reply]
Closing rt.cpan.org tickets
on Nov 28, 2008 at 01:18
2 direct replies by syphilis
    Hi,
    There are some bug reports on rt.cpan.org that I wish to close.

    I've found the "Resolve" link for these bug reports which, I presume, is the link I needed to locate. It takes me to a form ... but the form contains some blank fields with which I'm unsure how to deal.

    Firstly, there's a blank "Owner" field. Does that need to be filled in ?

    Secondly, there's a "Worked" field where a number of minutes/hours can be entered. What's the point of that ?

    And thirdly, there's a "Message" field. Does anything need to be written in it ? (From memory, any bug closure reports I've received for tickets that I've created seem to have a standard layout - so I assume that the message field is provided only in case there are additional comments that the closer wishes to make.)

    Cheers,
    Rob

[Offer your reply]
How to build a static multilanguage site with TT?
on Nov 28, 2008 at 00:20
1 direct reply by danmcb

    I am making up a website that contains only static pages, using Template Toolkit. Original pages are in Dutch, but an English version is also required. So I will have template pages that look like this:

    <p>Ik woon in Antwerpen.</p>
    

    I need an easy way to rip these strings out of the xhtml-like templates (no worries if the odd span or b tag gets left in, but paragraphs should be intact), and writes them into a file(s) to be translated. The template also needs to be re-written as (something like) ...

    <p>[% _tx(Ik woon in Antwerpen.) %]</p>
    

    ... so that TT can make the relevant substitution at the final pass

    Now, I am far from the first to travel this road ... so I don't want to be making any new wheels. Does anyone know where I can borrow some old ones that work well enough?


[Offer your reply]
New Meditations
RFC: CGI::Uploader V 2.90_01
on Nov 30, 2008 at 23:32
2 direct replies by Anonymous Monk
    Hi Folks I've re-written from scratch CGI::Uploader V 2.15, with the knowledge of that version's author Mark Stosberg, and uploaded the dev version to CPAN. I'd appreciate a few people taking it for a spin. TIA.

[Offer your reply]
Structured Learning of Perl, Important or Not?
on Nov 29, 2008 at 09:50
8 direct replies by koolgirl

    Hi Monks :). OK, not sure if this is the appropriate section to ask, it seemed the only possibility to me given the options, and I do know as a newbie I've probably made some poor decisions in previous posts, but since my attempt is both sincere and dedicated, I accept my mistakes as par for the course, so please forgive if this is the case here.

    I have a concern about the approach I'm taking in learning Perl, and was hoping to perhaps get some advice on whether or not the concern is warranted, one, and what to do if it is thought to be so, two. I am a mother, I work full time and have have quite a hectic life. I will begin school in summer 2009, however at the moment, I am self teaching, until I begin moving towards my bachelor's in comp science. As a result, I find myself, at times, going a week, sometimes even two!, without coding or studying at all. Granted there are plenty of times when I get stuck on a project, glued to the computer, my eyes glaze over, and someone has to literally come and pry me away, but as I said, I do find myself going a few days to a week or so without even looking at any of it.

    Now please make no mistake, the dedication is there. I love coding and am fully prepared for all the good and bad it will come along with (my spouse is a developer, and he "goes into the basement" quite often). It's just that, in order to spend a substantial amount of time studying every day, I would have to seriously restructure my, and my children's, entire life, and I am willing to do this if needed, however I'm not sure where this stands in importance to my actual career, development as a hacker, ability, skill and any possible problems that may result from this approach down the line.

    I know I can't be the first coder with a family, these worries, and this situation, so I thought I'd ask your opinion. Is a complete restructure of my approach a serious benefit in the pursuit of a career in this field, or is it kind of like riding a bike, once you advance to the next skill level, you're pretty much there? Of course we're all individuals and no one can answer this for me, or make the decision for me, I'm just curious because I do respect your opinions very much, as all of you seem to have achieved greatly in an extensive knowledge of the language.

    Please note, I'm asking in concern about my eventual outcome, the type of coder my education is likely to produce, not so much about the time issue. I of course realize I'd have to make serious changes to get it done faster, but, again, this is not my focus at the moment. Also, please do not doubt my dedication because of this, I certainly have it abundantly. If I had two of me, the best one would be stuck to the computer non-stop, trust me! ;)


[Offer your reply]
Test harness for executables
on Nov 26, 2008 at 14:20
0 direct replies by sfink
    While I'm between jobs, I've been trawling through various old code that I've written to see if there's anything I could clean up and release. One thing that I have found particularly useful is a handrolled test harness I wrote to make it easier to create unit tests for a program I was working on.

    I don't think there's anything stunningly novel about it, but I found it to strike a good balance between capability and simplicity. It was pretty easy for other people to pick up and start writing tests within, and for diagnosing the inevitable test failures.

    I wrote it before the recent TAPification of Perl test tools, so it could probably make use of some of the newer modules. (I wrote this tool partly in response to the difficult of extending the older Test::Harness to do what I needed.)

    Enough introduction. Why should anyone care? I'll try to enumerate what seems to be unique or vaguely unusual about this tool:

    • It is intended for processing the output of an application run on a given input file.
    • It uses a simple but extensible syntax for describing the set of tests to run.
    • The user can easily rerun specific failing tests with a very flexible way to pick the appropriate one. (This is slightly harder for this tool, since multiple tests can use the output of a single run.)
    • The output checks are simple regular expression matches. You may have multiple checks per test. Tests can be either positive or negative (X must match and Y must not match.) Before release, I'd be tempted to make this a bit more flexible.
    • Simple, TAP-compatible (I think) output
    • Convenient for smoke tests. Our continuous integration tool ran a large set of these tests after every build, and only created a release package if all tests passed. If a test failed, it would say exactly what it wanted and didn't get, together with the actual output.
    And now for the full description. I fortunately documented it in POD, so I'll just insert the pod2html here, rewritten to remove as much of the application-specificity as I can (I'll need to remove it for real before releasing): So my question is: is this of interest to anyone? I'm not inclined to do the work of polishing it up and severing it from the one specific application it was written for if nobody is going to use it. And I know there are similar tools out there now, which I haven't looked at enough to know whether my tool is of any use now.

[Offer your reply]
Macro system on B-Level?
on Nov 26, 2008 at 06:23
3 direct replies by LanX
    Hi

    I'm meditating about a macrosystem which is working after compilation and before execution

    The idea is to flag subs as :macro and and to replace every call with the string the sub returns.

    use B::MyMacro;
    sub foo :macro {
      return "bar()"
    }
    
    so that
     
    print;
    foo();
    print;
    
    will be replaced with
    print;
    do { bar() };
    print;
    

    Of course eval "string" needs also to be overridden, such that the macroreplacement will also be executed after eval's compilation.

    This mechanism can already be realised using B::Deparse, RegEx and evals, but with rather fragile dependencies. I'm looking for a stable and simple opcode-based solution, replacing every macro-call with new opcodes.

    IMHO, this approach would be much more stable and useful than codefilters, because "only perl can parse perl" ¹.

    I'm wondering if nobody ever tried to implement this, googling and supersearching didn't bring any examples. Do you know any other similar approach?

    Cheers Rolf

    UPDATES:
    (¹) ... but "Even B:: can parse optree" : )


[Offer your reply]
New Monk Discussion
Voting history
on Nov 26, 2008 at 15:48
4 direct replies by SilasTheMonk
    Is there (or could there be) a page in perlmonks that shows what I have voted on in the past?

[Offer your reply]
Login:
Password
remember me
password reminder
Create A New User

Community Ads
Chatterbox
and all is quiet...

How do I use this? | Other CB clients
Other Users
Others perusing the Monastery: (21)
BrowserUk
ikegami
moritz
kyle
tirwhan
atcroft
FunkyMonk
johngg
NetWallah
herveus
Crackers2
Eyck
clinton
Intrepid
Perlbotics
rovf
trwww
Prof Vince
SilasTheMonk
kennethk
StupidLarry
As of 2008-12-02 22: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
Snippets Section
Code Catacombs
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
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Perl Buzz
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Planet Perl
Use Perl
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

I work...

at home
from the nearest Starbucks
in an office
in a cubicle farm
shoulder to shoulder with my team-mates
wherever there's a free Wifi connection
all of the above
Work? What is this work?
Other

Results (193 votes), past polls