Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Think about Loose Coupling
 
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  (10285 days remain)

New Questions
Analyzing opcodes of lvalue-subs...
on Nov 20, 2008 at 20:26
3 direct replies by LanX
    Hi

    I'm trying to understand lvalue subs on the opcode level and would appreciate some help. For the motivation please read Another approach to extend lvalues ?

    here the perlcode I want to analyze

    my $scalar;
    test();
    
    sub normal :lvalue {
        proxy();
    }
    sub proxy :lvalue  {
        $scalar;
    }
    
    sub nlv {
        my $x="not lvalue"
    }
    
    sub test {
        nlv();
        normal=42;
        print $scalar;
    }
    
    and now how the corresponding opcode looks like, I added the original code as comment. *

    Well I'm still struggling to understand the opcodes, but please look at the lines 6-a and k.

    my interpretation is that by assigning a value to a lvalue-sub the steps are:

    1. cache the value
    2. call the lvalue-sub, which returns a var by refrence
    3. assign the cached value to this var

    so even on the level of the opcodes the lvaluesub has no chance to know which value will be assigned to the variable he returns ... Correct?

    I'm a little bit confused about line j, what means "rv2cv" (it's the first time this happens right before a sub call)?

    I googled through perlguts op.c and concise.pod but can't get a sufficient answer...

    Thanx Rolf

    * I know with 5.10 I wouldn't need to do it manually but I'm sticking on 5.8.8. I have to figure out how to safely install two different perl-versions on the same system, so that I can maintain both with aptitude.


[Offer your reply]
Fast, Efficient Union and Intersection on arrays
on Nov 20, 2008 at 11:31
6 direct replies by barvin
    Hi all,

    I'm doing graph traversal and calculating the Jaccard distance on each pair of vertices. Calculating intersection and union are consuming 98% of my processing time.

    I've tried Set::IntSpan, Set::Array and List::Compare, but they are all even slower than my own code. Is there a VERY fast (maybe implemented in C) way to calculate intersection and union on two arrays? Just for interest, my implementation is ($in and $jn are array refs of positive integers):

    sub get_int_uni {
    
            my ($in, $jn) = @_;
            
            my (%int, %uni);
            for my $i (@$in) {
                    $uni{$i}++;
            }
            for my $j (@$jn) {
                    $int{$j}++ if $uni{$j};
                    $uni{$j}++;
            }
            return ((scalar keys %int), (scalar keys %uni));
    }
    

[Offer your reply]
Log4Perl to send mail via SMTP
on Nov 20, 2008 at 08:08
1 direct reply by weismat
    Maybe I am missing something obvious, but how do I send SMTP mail via Log4Perl?
    I use this configuration

    log4perl.category = FATAL, Mailer
    log4perl.appender.Mailer = Log::Dispatch::Email::MailSend
    log4perl.appender.Mailer.to = x@z.com
    log4perl.appender.Mailer.from = y@z.com
    log4perl.appender.Mailer.subject = Something's broken!
    log4perl.appender.Mailer.layout = SimpleLayout
    log4perl.appender.Mailer.Server = x.x.x.x
    log4perl.appender.Mailer.smtp = 1

    I have tested that Mail::Mailer works, but I do not get it to work with the standard appender. The constructor for Mailer needs to be in the following format: $mailer = Mail::Mailer->new('smtp', Server => $server);

[Offer your reply]
Tkx: "invalid command error" OS X
on Nov 19, 2008 at 15:33
2 direct replies by vlsimpson

    I am in the process of moving a Perl/Tk text editor to Perl/Tkx.

    The following code works fine on Windows XP ActivePerl 5.8/5.10 but my OS X testers are getting this error:

    invalid command name "tk::text" at tkx-test-1.pl line 24. (There's a FIXME: on the offending line.)

    I've searched on this but no results that seemed applicable.

    Thanks for any and all responses.

    use warnings;
    use strict;
    
    use Tkx;
    
    my $VERSION = "1.0.0";
    my ($mw, $tw);
    $mw = Tkx::widget->new(".");
    
    $tw = $mw->new_tk__text( # FIXME: Barfs on OS X
        -width => 40, 
        -height => 10 );
    
    $tw->g_grid;
    $tw->g_focus;
    
    $tw->insert("1.0", "If you can read this it worked.");
    
    Tkx::MainLoop();
    exit;
    


[Offer your reply]
windows vs unix: different behaviour with exec()
on Nov 19, 2008 at 14:56
3 direct replies by abecher
    I need to know if there is a solution to exec() acting differently on windows vs unix. Best way to see the difference is using 3 little perl scripts (I'm displaying them from unix with the "more" command, but they will run as-is on windows also)
    -> more testexec*
    ::::::::::::::
    testexec.pl
    ::::::::::::::
    #!/usr/bin/perl
    system("testexec2.pl");
    print "Done $0\n";
    ::::::::::::::
    testexec2.pl
    ::::::::::::::
    #!/usr/bin/perl
    exec($^X,"testexec3.pl") or print "exec failed - $!\n";
    print "Done $0\n";
    ::::::::::::::
    testexec3.pl
    ::::::::::::::
    #!/usr/bin/perl
    `sleep 3`;
    print "Done $0\n";
    
    On unix, things act as expected: testexec.pl waits for testexec2.pl to finish (with testexec2.pl being replaced with testexec3.pl). But on windows, testexec.pl does NOT wait for testexec2.pl/testexec3.pl to finish. Is there a way to get Windows to act the same as unix? The windows behaviour does not seem correct...

    FYI: I'm trying to avoid using system() instead of exec() because then I open a can of worms regarding the quoting of arguements to the programs being exec()'d (the arguements themselves may contain quotes and/or spaces, and going through the shell on the two different OS's is a whole lot of extra code I can avoid if exec() would just act right :-)

    Aaron


[Offer your reply]
Popup Window
on Nov 18, 2008 at 15:17
1 direct reply by perl2008
    Hello Monks -- I am in desparate need for your help. I am trying to get a PDF file from a secured site by using Perl:Mechanize. When I click on that file, a "File Download" popup appears with buttons "Open", "Save" "Cancel" and if "Save" is selected then another popup window "Save As" appears with "File name:" & "Save as type:" along with "Save" & "Cancel" buttons. How do I handle these using Perl:Mechanize? Thanks a lot.

[Offer your reply]
Import comma delimited file into an array
on Nov 18, 2008 at 13:04
4 direct replies by drodinthe559
    Monks - What is the best way to import a comma delimited file into an array or hash? I am tasked with taking one row out a series of comma delimited file to dump out into a single file. Thanks, Dave

[Offer your reply]
How to know if a perl script is put in the background
on Nov 18, 2008 at 11:08
5 direct replies by rapide
    I've tried to find an answer to this problem but It seems like there is something which I do not quite understand:

    I have a script in which dumps a lot of output to the shell. When I do use the ampersand (&) to run the script as a background process it still prints the same output to the shell. So my questions is: Is it possible for the script to know that it has been put in the background by ampersand so that it can silence itself (re-direct STDOUT/STDERR TO /dev/null)?

    Update: Thanks everyone for great feedback. I got what I was looking for and I ended up with a check that was in almuts link:

        171 if (!open(TTY, "/dev/tty")) {
        172     print "no tty\n";
        173 } else {
        174     my $tpgrp = tcgetpgrp(fileno(*TTY));
        175     my $pgrp = getpgrp();
        176     if ($tpgrp == $pgrp) {
        177         print "foreground\n";
        178     } else {
        179         print "background\n";
        180     }
        181 }
    
    

[Offer your reply]
switch, transliteration ( tr/// ) of $_ issue
on Nov 18, 2008 at 10:56
5 direct replies by whakka
    Why doesn't this work?
    switch ( $foo ) {
        case ( tr/I/I/ == 1 ) { $bar = 'low' }
        case ( tr/I/I/ == 2 ) { $bar = 'med' }
        case ( tr/I/I/ == 3 ) { $bar = 'high' }
        else { warn "Bad $_\n" }
    }
    
    Yields:
    Use of uninitialized value $_ in transliteration (tr///)
    

    (Update: Because switch doesn't set $_, it filters the source code - in this case "insanely".)

    Here's a test script:

    Update: Thanks kyle and jeffa, the lesson is learned - don't use Switch. (jeffa's solution indeed doesn't work - which surprised me)

    Update2: moritz has it right, use the (5.010) given-when construct:

    given ( $foo ) {
        when ( tr/I/I/ == 1 ) { $bar = 'low' }
        when ( tr/I/I/ == 2 ) { $bar = 'med' }
        when ( tr/I/I/ == 3 ) { $bar = 'high' }
        default { $bar = "drat" }
    }
    

[Offer your reply]
A tale about accessors, lvalues and ties
on Nov 18, 2008 at 08:32
4 direct replies by LanX
    Hi

    In OOP it really bothers me to write use getter/setters instead of lvalues for simple attributes like in other languages.

    As shown in PBP, the problem with lvalue is, that if ever you need a wrapper to control the setting of the attribute you can only rely on tieing the variable or you have to change the API and refactor...

    Which is not really an issue of ugly syntax, I found at least two cpan-modules showing that a posteriori FETCH and STORE can be done in a very clean, readable and maintainable syntax. e.g. in Class::Closure

    I did some benchmarks showing that accessing a tied variable via lvalue is 4-5 times slower than with a getter sub! : (

    Now I "superfound" Re^3: Class::Accessor and Damian's PBP and was quite exited to see Contextual::Return...

    So, very curious about how TheDamian fixed the problem, I took a look into the source:

      $impl = tie $_[0], 'Contextual::Return::Lvalue',     
                $subname => $handler, args=>$args;
    

    Do I get it right, it's just another sugar for Tieing? 8 (

    Well I don't know how big the trade-off of wrapping lvalues is in other languages ...

    But shouldn't this general "banning" of lvalues better be replaced by the rule

    It's acceptable to use them as long you are aware that later changing will result in a call-overhead of 400%!

    Time critical calls should be done with getter/setters!

    ???

    BTW: Is there any activity to speed up tie? I don't see why the calling mechanism for FETCH and STORE needs to undergo the same slow dynamic ways OOP needs. A kind of "tie :static" might really solve the problem!

    Hopefully looking for a serious discussion of this somehow "religious" matter..

    Cheers LanX

    - - - - - Which song???

    UPDATE: fixed some typos, some English, some mark up ...

[Offer your reply]
How to decide the size of block in file transferring?
on Nov 18, 2008 at 00:52
3 direct replies by SadEmperor
    Now I'm trying to finish a script for sharing huge files (maybe 3Gb) in local network using remote drive under MSWin32. I found some helpful code at http://www.perlmonks.org/index.pl?node_id=44963, and in this code, the following statements make me puzzle :
      my $blksize  = int ($filesize / 10);
      my $num_read = sysread(SRC, $buffer, $blksize);
    
    That's how to decide the block size ($blksize) to get the best performance, and is that sensitive to file system, if I use my script under Linux, shall I modify the value ?
    Thanks very much !

[Offer your reply]
New Monk Discussion
Vulnerability discovered in cbstream plogin
on Nov 17, 2008 at 10:11
1 direct reply by ambrus

    Yesterday, I found two vulnerabilities in cbstream that have been there from almost the start. As you may know, cbstream (then called cbupstream) is a bridge that allows you to talk on the perlmonks chatterbox using any IRC client. This requires a login: you have to tell to cbstream about your perlmonks account name and password. There are two modes of login in cbstream: normal login remembers your account details only until you leave the IRC channel, whereas persistent login remembers them until the bridge is shut down and recognizes you when you enter the channel again. This means cbstream has to know someone entering the channel is the same user as the one who logged in persistently, but the mechanism how it did this was buggy. This bug could have allowed someone to send public perlmonks chatter or private messages in the name of anyone who used the persistent login feature.

    I have temporarily disabled the persistent login feature yesterday, so now cbstream users have to login each time they enter the channel. This closes the vulnerability.

    Kudos to tomaw and jilles who educated me on the workings of the hyperion irc server and thus found these bugs.

    The good news is that there is a secure way to implement persistent login with very little user-visible change. I'll do this when I have time (don't hold your breath) and post an update as a reply here when I've done so.


[Offer your reply]
a place for other monks?
on Nov 15, 2008 at 12:46
7 direct replies by xiaoyafeng

    I think we need a place for non-programmer monks.

    This idea comes from a non-programmer monk's cry. Personally, I don't agree some gurus' reply. Hacking, writing modules for CPAN or even program in perl ought not to be a requirement for join perlmonks. Some monks have been away from perl for some time would like to stay here but don't mean they still like to write code in perl. If we write a poem in perl, we could post on Poetry , if we write a snippet, we post it on Snippets, but if we named our puppy in perl or simply want to share a joke to monks, where do we post?

    Meditations ? Perl News ? It seems a alternative, but the name keeps many people away.


    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

[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 exploiting the Monastery: (16)
moritz
wfsp
atcroft
syphilis
herveus
jaldhar
Eyck
clinton
explorer
jkva
dHarry
ggvaidya
mje
LanX
joec_
ela
As of 2008-11-21 12:03 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

Global warning is an act of:

Man
God
dog
Sheep
Zod
-W
other

Results (282 votes), past polls