Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

The Monastery Gates

( #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.

New Questions
AnyEvent timers by EV
2 direct replies — Read more / Contribute
by bash
on Apr 01, 2015 at 14:28
    Im stuck on understanding how EV calculcate timers. I have this code:
    #!/usr/bin/perl use common::sense; use AnyEvent::HTTP; use Data::Dumper; sub do { my $iter = shift; # It will resolve every timeout problems, but it is not question # AnyEvent->now_update; my $cv = AnyEvent->condvar; $cv->begin; http_get 'https://imasheep.hurrdurr.org/category/about.html', timeout => 2, sub { my ($body, $hdr) = @_; say "$iter $hdr->{Status}"; unless ($hdr->{Status} == 200) { warn Data::Dumper->Dump([$hdr],['hdr']); } $cv->end; } ; my $timer = AnyEvent->timer( after => 300, cb => sub { warn "Exit by timer"; $cv->send }, ); my ($n, $t) = (AnyEvent->now, AnyEvent->time); my $d = $t-$n; say "$iter diff = $d, time = $t, now = $n"; $cv->recv; } for (1..4) { &do($_); sleep(5); }
    output:
    1 diff = 0.0233771800994873, time = 1427912097.54858, now = 1427912097 +.5252 1 200 2 diff = 5.0014123916626, time = 1427912103.05933, now = 1427912098.05 +791 2 596 $hdr = { 'Reason' => 'Connection timed out', 'URL' => 'https://imasheep.hurrdurr.org/category/about.html', 'Status' => 596 }; 3 diff = 5.00508308410645, time = 1427912108.06631, now = 1427912103.0 +6123 3 200 4 diff = 5.00244045257568, time = 1427912113.56622, now = 1427912108.5 +6378 4 596 $hdr = { 'Status' => 596, 'Reason' => 'Connection timed out', 'URL' => 'https://imasheep.hurrdurr.org/category/about.html' };
    Iteration 2 - This is OK for me. Timer was set when internal time has 5 sec lag
    Iteration 3 - But I do not understand this case. Everything is the same. Why there is no timeout ???
    Iteration 4 - And once again. So every even call to do() generate timeout, and every odd call doesn't generate.
    Could you please clarify me about this behaviour?
Find directories lt 7 days old
6 direct replies — Read more / Contribute
by Jeri
on Apr 01, 2015 at 11:17

    Hi,

    Maybe I should be writing in a unix forum, but I was hoping this was possible using perl. How can I find directories less than 7 days old? Is CPAN's File::DirList the only way to go? Is there no built in function for this type of search?

    Thanks,
    Jay

How @INC can be managed from c code?
3 direct replies — Read more / Contribute
by DrMoisha
on Apr 01, 2015 at 09:15

    Dear monks, my question is as short as title:
    How can I add directory to @INC from C code, when I'm embeding perl?

    A little bit longer explanation. I have embeded perl in my c application, but I have problem, that even without xs_init() like here embedding perl functions with 'use()' I get error: Can't locate module-name.pm in @INC ... (@INC contains .) I understand, that it is because @INC is not iniialized automatically, so it is empty.

Make use of a java object in Perl
4 direct replies — Read more / Contribute
by ravin_perl
on Mar 31, 2015 at 15:18
    Scenario : there is a java class as given below :
    public class TestSelenium { public void googleTest() throws Exception { WebDriver driver = new InternetExplorerDriver(); driver.get("http://www.google.com/webhp?complete=1&hl=en"); } }
    There is a perl program which makes use of Inline::Java module to call the googleTest of java class written above. Perl program looks like.
    use warnings; use Selenium::Remote::Driver; use Inline Java => 'STUDY', CLASSPATH => 'C:\selenium\selenium-java-2.37.0\selenium-2.37.0\libs\se +lenium-java-2.37.0.jar;C:\selenium\SeleniumTestPoc\bin\MyJar.jar;C:\s +elenium\selenium-java-2.37.0\selenium-2.37.0\libs\selenium-server-sta +ndalone-2.37.0.jar', STUDY => ['TestSelenium']; $test= TestSelenium->new; $test->googleTest;
    Now the above Perl code will open IExplorer and go to google.com page. In my Perl program further to $test->googleTest; I want to make use of same browser that was opened by java(WebDriver driver = new InternetExplorerDriver();) and perform a search for text "Cheese".

    Question is, can the object of WebDriver class("driver" in this case) be further used in my Perl program so that I can use same browser and perform different UI operations on it in Perl?

Replace bits
1 direct reply — Read more / Contribute
by newbiecali
on Mar 30, 2015 at 17:35
    Hi

    i am newbie to Perl and need to learn from starting
    i have very big file of data (3Gb)like below


    00101100
    00101101
    11H00101
    HHHHHHHH
    01011001
    LLLLLLLL
    1011010L
    0110111L
    00000000
    00111111


    i need to replace H to 1 in column3 and L to 0 in column 8
    only so the file looks like below

    00101100
    00101101
    11100101
    HHHHHHHH
    01011001
    LLLLLLL0
    10110100
    01101110
    00000000
    00111111


    can it take text file "file1.txt"
    and spit out the output in "output.txt"
    something like that

    i will appreciate any help

    Thanks

Python dict to perl hash
10 direct replies — Read more / Contribute
by garg10may
on Mar 30, 2015 at 07:32
    Hi Monks, I want to convert a python dict to perl hash. Is there a better/easy way to do this than below.
    use Data::Dumper; sub rot13 { my $x = "'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g' +:'t', 'h':'u', 'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a', 'o':'b', +'p':'c', 'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', +'x':'k', 'y':'l', 'z':'m', 'A':'N', 'B':'O', 'C':'P', 'D':'Q', 'E':'R', +'F':'S', 'G':'T', 'H':'U', 'I':'V', 'J':'W', 'K':'X', 'L':'Y', 'M':'Z', +'N':'A', 'O':'B', 'P':'C', 'Q':'D', 'R':'E', 'S':'F', 'T':'G', 'U':'H', +'V':'I', 'W':'J', 'X':'K', 'Y':'L', 'Z':'M'"; $x =~ s/['\s+]//g; my %hash = split /[:,]/, $x; print Dumper\%hash; } rot13();
Module for Conditional Parsing
4 direct replies — Read more / Contribute
by mwb613
on Mar 30, 2015 at 03:16

    Hello, thanks for looking!

    I have been looking around CPAN for a module that might save me from starting from scratch. So far I haven't found what I'm looking for hopefully due to a failure to use the proper terminology or focusing on too narrow of a use case.

    I have some data in Redis that I'd like to be able to run SQL-style queries on. I'm not looking to invent a SQL wrapper for Redis but I do think it would be useful to have an ability to test values against ad hoc conditionals.

    I'd like to be able to pass a subroutine a conditional test and a value to test against it. For example the conditional could be a hash ref like this (say we're testing a zip code):

    my $conditional_test = { 'OR' => { 'condition1' => { 'value' => '10022' } 'condition2' => { 'value' => '96813' } 'condition3' => { 'value' => '55401' } } } if(my_condition_test_sub($conditional_test,$redis_handle->get('key'))) +{ #do something }

    Possible conditions could be (and, or, not...)

    Ideally we could nest these conditions as well which is why I'm looking for a parser that's already built. I did find a grammar parser that looked like it worked for natural language as well as some conditional parsers specific to specialized data (XML, lists) but nothing generalized.

    Has anyone come across this particular problem (not Redis queries but checking data against ad hoc value tests) before and found a good solution? It seems like a use case that might warrant a module.

    Thanks again for looking.

Can I ask Perl if an object will stringify?
5 direct replies — Read more / Contribute
by haukex
on Mar 29, 2015 at 13:16

    Hello everyone,

    I seek your wisdom on this question: Is there a way to ask Perl whether an object supports stringification, including via "magic autogeneration"? The only way I've found so far was by trying to eval the stringification, like in the code below. The specific case here is the "ICanStringify" class, where overload::Method($s,'""') is false, but the object still stringifies. Did I miss some function somewhere that can tell me whether that class will stringify?

    #!/usr/bin/perl use strict; use warnings; { package OnlyAString; use overload fallback=>0, '""'=>sub { ${shift()} } } { package ICanStringify; use overload fallback=>undef, '0+'=>sub { ${shift()} } } { package OnlyANumber; use overload fallback=>0, '0+'=>sub { ${shift()} } } bless my $s1=\do {my $x=111}, 'OnlyAString'; bless my $s2=\do {my $x=222}, 'ICanStringify'; bless my $s3=\do {my $x=333}, 'OnlyANumber'; can_str($s1); can_str($s2); can_str($s3); use overload (); sub can_str { my $s = shift; print "Object ", overload::StrVal($s), ":\n"; print " \"\" ", overload::Method($s,'""') ?"IS":"is NOT", " overloaded\n"; my $e = eval { "$s" }; print " stringification ", defined($e) ?"WORKED: $e\n":"DIDN'T work: $@\n"; }

    Output:

    Object OnlyAString=SCALAR(0x3684370): "" IS overloaded stringification WORKED: 111 Object ICanStringify=SCALAR(0x3664210): "" is NOT overloaded stringification WORKED: 222 Object OnlyANumber=SCALAR(0x3679682): "" is NOT overloaded stringification DIDN'T work: Operation """": no method found, argu +ment in overloaded package OnlyANumber at test.pl line 28.

    The background is that I have a function that accepts only strings. Because passing a reference was a mistake I made a few times, I started warning if any references were passed to it, including objects. But then I realized that some objects stringify and that's useful, and that some objects die when you try to stringify them. I'd like to loosen the restrictions, and still warn on references and objects that don't stringify, but not on objects that stringify.

    Any wisdom on this topic would be greatly appreciated!

Perl reference array
4 direct replies — Read more / Contribute
by teun-arno
on Mar 28, 2015 at 16:57
    I have a question about the following :
    use Data::Dumper; # it seems that ', ,' in the third record is not producing a index in +the array. $arr= [ [1,2,3,4,5,6,7,8,9 ], #works as expected [1,2,3,'',5,6,7,'',9 ], #works as expected [1,2,3, ,5,6,7, ,9 ], #does not work as expected "empty fields " + are ignored ]; my $dd=Data::Dumper->new([$arr],[ qw(arr) ] )->Indent(1)->Quotekeys(0) +->Dump; print $dd; $str_arr=q`$arr= [ [1,2,3,4,5,6,7,8,9 ], [1,2,3,'',5,6,7,'',9 ], [1,2,3, ,5,6,7, ,9 ], ];` ; print "\n\n"; print $str_arr ; $str_arr=~ s/,\s*,/,'',/g; # filling in the empty fields print $str_arr ; $arr = undef; $arr = eval $str_arr; my $dd=Data::Dumper->new([$arr],[ qw(arr) ] )->Indent(1)->Quotekeys(0) +->Dump; print $dd; # this seems to work, next question is : The original $arr is send to +a subroutine. # How to stringify that in the subroutine without hardcoding this. ( a +s I have done in the above ) ! #
    Thanks for your time dear perl monks.
What is (a => b => c) ?
4 direct replies — Read more / Contribute
by philgoetz
on Mar 26, 2015 at 15:29

    What does this do?

    @x = (a => b => c);

    There's a lot of that in the program I'm looking at. As far as I can tell, it's the same as

    @x = (a, b, c);
XML::CSV out of memory
3 direct replies — Read more / Contribute
by slugger415
on Mar 26, 2015 at 12:03

    Hello, I am using XML::CSV to parse a very large (500MB) CSV file and convert it to XML. I keep getting an "out of memory" error during the parse.

    Is there a way to "purge" data to free up memory during this process, like you can with XML::Twig? (Though I don't see anything about handlers in the doc....)

    FWIW:

    my $csv_obj = XML::CSV->new( error_out => 1 ); $csv_obj->{column_headings} = \@heads; my $status = $csv_obj->parse_doc($file);

    thanks!

Beginner here - basic help
3 direct replies — Read more / Contribute
by xr6turbo
on Mar 26, 2015 at 09:09

    Hey Monks, Just starting off in perl (and programming in general) and need to do an exercise but a bit confused on the process.

    Trying to create a script that will ask the user to enter a number between 1 and 5 and for it to print a colour represented e.g. if they enter 1, Blue comes up or 3, red gets printed. I've done a bit of research and I know I need to create an array (@) but not too clear on how to do so.

    After that I need to assign an emotion to the colour e.g. blue = calm, red = angry. I'd also like to know how to incorporate modulus into this so on even/odd number inputs I can print it out (think ill need an if/else statement for this?)

    At the end I'd like it to look like this:

    Please enter a number between 1 and 5:

    1

    Blue. Even. Calm

    So far I've got the code below and have no idea how to finish or even progress, any ideas?

    print "Please enter a number between 1 and 5 (inclusive) below:"; my $number = <STDIN>; chomp($number); my @colours = ("red", "green", "blue", "purple", "black"); my %table = ( 1 => "angry" 2 => "sick" 3 => "calm" 4 => "worried" 5 => "sad" );
New Meditations
A Big "Thank You" To Strawberry Perl Folks.
No replies — Read more | Post response
by Anonymous Monk
on Mar 31, 2015 at 03:14

    Big Thank You to all the Strawberry Perl creators/Maintainers/Developers. You have created an awesome distribution. What is even more awesome is Strawberry Portable Perl. It has made my life simpler. No Admin Rights needed to install it. I have it running on our production servers. There are some applications which are using Perl, and I treat that as "System Perl". So no fiddling there. I first downloaded the portable perl to my workstation, installed a few modules, and simply copied the folder to the production server, where I had some scripts running. Worked like a charm. Beautiful.

    It also ended up installing gmake/dmake etc which were extremely useful. I use gVim on windows and was recently playing around with some plugins which required vimproc. Compiling it was easy peasy. All thanks to the extra goodies you folks have provided.

    Thank you all once again.

MJDs Contract Warnings - courtesy of Perlweekly
4 direct replies — Read more / Contribute
by ww
on Mar 30, 2015 at 08:10
New Cool Uses for Perl
Binary DeBruijn sequences.
No replies — Read more | Post response
by BrowserUk
on Mar 30, 2015 at 13:55
    yamātārājabhānasalagam

    I needed a binary (alphabet 0|1) DeBruijn sequence, and found a simple rule for producing one (see the comments).

    I first coded it using strings of '0' and '1' characters and a hash to detect words already included.

    Very quick to code, but using one byte per bit, and a hash, the size rapidly chewed through gobs of memory, long before I reached my target of 31-bit words.

    #! perl -slw use strict; ### Prefer Ones: ### Write n zeros. ### Then, always write a one unless it would cause the repetition of a +n n-length string; ### In which case, write a zero. our $N //= 4; my $seq = '0' x $N; my %seen = ( $seq => 1 ); for( $N .. 2**$N ) { $seq .= ( not exists $seen{ substr( $seq, -( $N-1 ) ) . '1' } ) ? +'1' : '0'; ++$seen{ substr( $seq, -$N ) }; } $seq .= substr $seq, 0, $N-1; print length $seq; <STDIN>; my $re = '(.)(?=(' . '.'x($N-1) . '))'; print $1 . $2 while $seq =~ m[$re]g;

    So then I coded another version that used vec to produce the sequence directly into bits; and another bitvector to track the words seen.

    This was much tricker to code -- despite the apparent simplicity of the code -- and goes much higher, using a mere faction of the memory, but unfortunately stops before my target because vec (as of the version of Perl I'm using) still treats its second argument as a signed, 32-bit integer despite that a) negative offsets make no sense; b) I'm using a 64-bit version of Perl :(

    (If you try it with -N=7 or greater, I strongly recommend redirecting the output, or disabling it, because watching 100s or 1000s of 0s & 1s scroll up the screen is a very boring occupation :)

    #! perl -slw use strict; ### Prefer Ones: ### Write n zeros. ### Then, always write a one unless it would cause the repetition of a +n n-length string; ### In which case, write a zero. our $N //= 4; my $t1 = "b${ \(2**$N+$N-1) }"; my $seen = ''; my $mask1 = ( 1<<$N )-1; my $seq = pack 'Q*', (0) x 100; my $val = 0; for( $N .. 2**$N+$N-1 ) { ## if N=5; 5 .. 36; if N=6 +, 6 .. 64+6-1 = 69; $val = ( $val << 1 ) & $mask1; vec( $seen, $val | 1, 1 ) or do{ $val |= 1; vec( $seq, $_, 1 ) = 1 +; }; vec( $seen, $val , 1 ) = 1; } print unpack $t1, $seq;

    Note: both the above versions produce the 2N+N-1 bit complete sequence, rather than the 2N sequence that is shown in the Wikipedia page which only become complete once you 'wrap them around'.

    Ultimately, I ended up moving to C to achieve my target, which even more tricky (damn, I miss vec in C), but it eventually allowed me to produce the 1/4GB binary sequence I was after.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
New Monk Discussion
The boy who cried...
6 direct replies — Read more / Contribute
by BrowserUk
on Mar 31, 2015 at 13:22

    At what point does the Troll-caller become the troll? Discuss.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2015-04-02 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who makes your decisions?







    Results (58 votes), past polls