Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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
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.

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
8 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?
4 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" );
Module::Build's adding new filetypes in ExtUtils::MakeMaker
4 direct replies — Read more / Contribute
by vicdan
on Mar 25, 2015 at 13:22

    Hi there,

    as Module::Build will vanish out of the perl core in some versions, I'd like to know how I can do Module::Buildīs adding of new file types.

    Say I have .dat files that are located in /dat in my modules path.

    With Module::Build I can add a file type by adding my files within the definition ( dat_files => {'some/dir/Bar.dat' => 'lib/Foo/Bar.dat'},), then make that known to the builder:

    $build->add_build_element('dat');

    Is there anything similar in ExtUtils::MakeMaker?

    I searched for that feature but is was not really obvious to me ...

New Meditations
MJDs Contract Warnings - courtesy of Perlweekly
2 direct replies — Read more / Contribute
by ww
on Mar 30, 2015 at 08:10
New Cool Uses for Perl
"Spritz" crypto algorithm
No replies — Read more | Post response
by RonW
on Mar 24, 2015 at 16:41

    For testing purposes, I implemented Rivist's new crypto algorithm in Perl. It is a proposed replacement for his (once very popular) RC4 algorithm. Thought there might be some curiosity value to it.

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 having an uproarious good time at the Monastery: (12)
As of 2015-03-31 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:









    Results (661 votes), past polls