Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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
Perl structure to Javacript-ready JSON
4 direct replies — Read more / Contribute
by Jeppe
on Nov 25, 2014 at 08:21
    Esteemed monks, I'm essentially trying to escape the contents of a JSON structure.
    my $hashref = { abc => "abc'abc" };
    This should become
    {"abc":"abc\'abc"}
    , and it must work for blessed references as well. I'm currently using the JSON module, but I'm not married to it. Ideally, this would have been resolved by a hook in the JSON module - but that does not seem to be available. What other options do I have?
about Term::ReadLine and it's imported function
3 direct replies — Read more / Contribute
by Discipulus
on Nov 24, 2014 at 08:10
    Hello monks,
    i used readline::rl_basic_commands as only way to have autocompletion working in win32. if i remember correctly i found the mention here.

    I had used this sub in some programs based on various Strawberry distros without any problem.

    Now i have a new program i would like to run on Win32 as in Linux and it uses only core modules.
    I developped on my strawberry Perl but when tried on the Linux box it complains about that missing sub:
    Undefined subroutine &readline::rl_basic_commands called at...
    So i changed the call in something like:
    if (exists &readline::rl_basic_commands){&readline::rl_basic_commands( +keys %cmds);}

    Now on Linux works (without autocompletion), but using that syntax on win32 made the first time intialization of available commands not working. The following working example shows my problem:
    #!perl use strict; use warnings; use Term::ReadLine; $ENV{TERM}=undef; my $term = Term::ReadLine->new('xxx'); my %cmds = (a=>1,b=>2); # THIS WORKS &readline::rl_basic_commands(keys %cmds); # THE FOLLOWING CHECK DOES NOT WORK if (exists &readline::rl_basic_commands){print "BEFORE WHILE EXISTS +\n"; &readline::rl_basic_commands(keys %cmds);} while ( defined ( $_ = $term->readline( '>') ) ) { print "$_\n"; } continue{ # THIS CHECK WORKS INDEED! if (exists &readline::rl_basic_commands){print "IN CONTINUE EXISTS\ +n"; &readline::rl_basic_commands(keys %cmds);} }

    Where and when the sub is defined?
    There is safe way to deal with it?
    There is a way have it working on both Linux and Win32, even in the sad case without autocompletion?

    Thanks for the attention.
    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Regular expression for a comma separated string
6 direct replies — Read more / Contribute
by naderra
on Nov 23, 2014 at 18:22

    Hi,

    I would like to define a regular expression that matches strings consisting of two sub-strings separated by a single comma. Each sub-string may not be empty, and consists exclusively and without repetition of the characters 'A', 'G', 'C' and 'T'. Thus, the pattern should match strings such as:

    A,G
    AG,CT
    TC,CA <- correct, 1st and 2nd sub-strings may have characters in common
    ( as long as these are not repeated within the sub-string ).
    GAT,CGA
    CGAT,TG <- correct, sub-strings may be of different length.
    etc ...

    and should not match:

    ,G <- missing 1st sub-string
    ACGT <- missing comma
    X,A <- incorrect character X
    AA,G <- repetition of character A in 1st substring
    AC,GGC <- repetition of character G in 2nd sub-string
    ATGA,TGG <- repetition in both sub-strings
    ATCXG,AAC <- incorrect character X and repetition in 2nd
    sub-string
    etc ...

    So far I have:

    /^(?=[ACGT]{1,4},[ACGT]{1,4}$)(?!.*(.).*\1.*,)(?!,.*(.).*\1).*$/

    /^(?=[ACGT]{1,4},[ACGT]{1,4}$)(?!.*(.).*\g{1}.*,)(?!,.*(.).*\g{1}).*$/

    also tried joining the capture groups with:

    /^(?=[ACGT]{1,4},[ACGT]{1,4}$)(?!.*(.).*\g{1}.*,.*(.).*\g{2}).*$/

    Now, (?=[ACGT]{1,4},[ACGT]{1,4}$) seems to match the "two sub-strings separated by a single comma" and "consists exclusively of the characters 'A', 'G', 'C' and 'T'" through out the string;

    (?!.*(.).*\1.*,) seems to match "without repetition" up to the comma.

    However, (?!,.*(.).*\1) appears not to be ensuring that it doesn't match a repeated character after the comma.

    I'd greatly appreciate replies with clues and/or patterns that help with the desired matching.

    Using perl v5.18.2

    Thanks in advance

    Robert

Replacing builtin macro languages with perl -- how difficult?
3 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 23, 2014 at 00:22
    I keep getting involved in projects where a big software package has a hand-rolled macro language that is pathologically deficient. I'd love to replace such languages with Perl and expose the underlying data without needing to copy it or pipe into a separate perl process. Can anyone comment on how difficult this might be? Are there any examples of this being done?
Verify syntax of large JSON files
1 direct reply — Read more / Contribute
by ron800
on Nov 22, 2014 at 22:00
    Hello, I've searched through this group, but no one seems to have the same issue. I want to parse large JSON files to see if the syntax is correct. I found examples but they all seem to want to parse from a string. I want to do something like this:
    use JSON::Parse 'assert_valid_json'; unless (valid_json ($json)) { # do something } eval { assert_valid_json ('["xyz":"b"]'); }; if ($@) { print "Your JSON was invalid: $@\n"; } }
    My files are multiple megabytes and reading them into a string for parsing is not realistic. Any suggestions would be appreciated.
Getopt::Long defaults for options with multiple values
3 direct replies — Read more / Contribute
by PetaMem
on Nov 21, 2014 at 07:23

    Reviewing some middle-aged code, I stumbled across this topic...

    Unfortunately, the G:L documentation is silent about default values for options with multiple values: Getopt::Long#Options-with-multiple-values. Even more unfortunate seems an inconsistency compared to defaults for options with single values and maybe a semantic inconsistency at all. If you have a single value option, you may define a default like:

    my $tag = 'foo'; # option variable with default value GetOptions ('tag=s' => \$tag);

    This works as expected. Good. You can - of course - do a similar thing for options that take multiple values:

    my $listref = ['a','b','c']; # option variable with default valu +es GetOptions ('list=s{,}' => $listref);

    If you omit the -list option, the program will have the default value, which is good. If you, however, will give a list option, G:L seems to push that option to the list already given in default, which may have its applications, but is not that great as default behavior. If you want to actually replace the default given, you would have to define defaults the ugly, backward and programmatically DIY way:

    my $listref = []; # no default GetOptions ('list=s{,}' => $listref); my $listref = @{$listref} ? $listref : ['a', 'b', 'c']; # DIY default

    Which is actually code I see right now. Yuck! That can't be right - can it?

    Bye
     PetaMem
        All Perl:   MT, NLP, NLU

Runaway CGI script
3 direct replies — Read more / Contribute
by Pascal666
on Nov 19, 2014 at 11:15
    tl;dr: Somehow a CGI script that doesn't write to disk kept running for about 16 hours after the client disconnected, filled up the disk about 10 hours in, and then freed the space when Apache was killed. Contents of script unknown.

    Fully patched CentOS 7. Woke up this morning to "Disk quota exceeded" errors and:

    # df -h Filesystem Size Used Avail Use% Mounted on /dev/simfs 25G 25G 0 100% / # du -sh 3.9G .
    Top indicated that I had plenty of ram left and a CGI script I wrote yesterday was the likely culprit:
    KiB Mem: 1048576 total, 380264 used, 668312 free, 0 buffe +rs KiB Swap: 262144 total, 81204 used, 180940 free. 33856 cache +d Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COM +MAND 5140 apache 20 0 239888 2348 1756 R 17.3 0.2 144:42.67 htt +pd 14980 apache 20 0 30840 1884 1228 S 15.6 0.2 153:43.94 bou +nced.cgi
    I killed Apache and now my disk and cpu utilization are normal. I didn't have lsof installed so I couldn't see what file was causing the problem.

    Access_log shows only me accessing the script, and error_log shows nothing since I wrote it.

    I wrote this quickly yesterday with no error handling, but worst I expected to happen if there was an error was the script to die. I can't understand how the following could possibly fill up my disk. It appears to work as intended.

    #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; print $q->header; print $q->start_html('Bounce summary'); my @files = </home/user/Maildir/cur/*>; for (@files){ open(IN, $_); while (<IN>) { last if /The mail system/; } while (<IN>) { if (m#/domain.com$#) { print '<p>'; last; } s/</&lt/g; print; } close IN; } print $q->end_html;
    Edited to add:
    Pulling the CGI components out gives nearly identical output to what the web browser tab displays, with no errors showing. The directories I was/will run this against never have subdirectories.

    Having thought about it today, I believe one of my initial assumptions when opening this thread was probably incorrect. As a CGI script it only runs when I access it. I only ran it a couple times in its final state (above). It is probable the stuck version was different, and I simply didn't notice it. It could have run for many hours before crippling the server. I do not make a habit of confirming scripts end when they stop loading or I hit X in my web browser, I just assume Apache will kill them.

    I just really don't understand how a cgi script could stay running without a client attached. I just created one with an intentional infinite loop, and as soon as I hit X Apache killed it.

    From /var/log/messages after I ran "service httpd stop" this morning:

    Nov 19 10:38:44 systemd: httpd.service stopping timed out. Killing. Nov 19 10:38:44 systemd: httpd.service: main process exited, code=kill +ed, status=9/KILL Nov 19 10:38:44 systemd: Unit httpd.service entered failed state.
    "kill -9 14980" probably would have fixed the problem without killing Apache, but I didn't think of it at the time.

    Update 2:
    It is actually trivial to create a cgi script that won't die when the client disconnects. My test above contained a "print" inside the loop. Looks like Apache disconnects STDOUT when the client disconnects which causes print to kill the script. For example, a cgi containing just:

    #!/usr/bin/perl sleep 1 while 1;
    will keep running after the client disconnects, and a "service httpd stop" will yield the same errors as above, however, Apache will kill it after the cgi timeout. So apparently one of my interim scripts entered an infinite loop without a print, but with something that caused Apache's timeout not to kill it. Still no idea how that could use up all free disk space, and then free it immediately when killed.

    I just tried writing to STDERR in the loop, both with "print STDERR" and by trying to read a closed filehandle. In both cases error_log recorded the errors immediately and continued to grow in size. When I experienced the disk full error yesterday one of the first things I checked was the log files. error_log was only 7728 bytes.

Basic Apache2::REST implementation
1 direct reply — Read more / Contribute
by Anonymous Monk
on Nov 19, 2014 at 10:33
    Hello,

    I'm new to Perl and i'm trying to implement REST API using Perl. I installed Apache2::REST , and i couldn't find enough detailed example to implement it (i'm poor in Perl).

    Ok, i see the below details in the document attached with the module (http://search.cpan.org/~jeteve/Apache2-REST-0.07/lib/Apache2/REST.pm) ,

    <Location />
    SetHandler perl-script
    PerlSetVar Apache2RESTHandlerRootClass "MyApp::REST::API"
    PerlResponseHandler Apache2::REST
    </Location>


    i use Apache2 with Mod Perl, my web root path is /usr/local/apache2/htdocs

    and i see the Sample program/package named MyApp::REST::API in the document

    My question is, where should i place this Package/program ? under ..../htdocs/MyApp/REST/API.pm ? or ..../htdocs/API.pm? or how i tried both but no luck,..

    Could any one please guide me to implement this API?

    Regards,
    Rik.
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 wandering the Monastery: (7)
As of 2014-11-26 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (167 votes), past polls