Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
File::Temp::tempfile : name versus handle
3 direct replies — Read more / Contribute
by mldvx4
on Mar 22, 2018 at 08:24

    I'm using File::Temp::tempfile to get a name to use for a temporary file to temporary output from a piped command. The book Programming Perl, 4th ed. mentions that it is prefereable to use the file handle that the tempfile() function creates instead of the file name. Is there any way to do that in the context of the code snippet below?

    my ($handle, $output) = tempfile("tidyXXXXXX", DIR => "/run/validator", SUFFIX => ".dat" ); my $tidy = q(/usr/bin/tidy); my @args = ('-quiet', '-xml', '-file', $output); # process web page with tidy open(TIDY, "-|", $tidy, @args, $tempfile) or die("Could not open '$tidy' : $!\n"); close(TIDY);

    What about other improvements to the above?

compute paths in Pascal's triangle (aka Tartaglia's one)
8 direct replies — Read more / Contribute
by Discipulus
on Mar 22, 2018 at 05:03
    Hello wise monks and nuns,

    I want to add a 17th fun experiment to my project: infact there is a properties I have still not shown: the number in a specific tile is also the number of different shortest path from the top tile (no backwards move nor lateral ones).

    I want to show (colorizing them) all distinct paths in sequence and to do it I need a serie of coordinates: given the following structure

    0-0 1-0 1-1 2-0 2-1 2-2 3-0 3-1 3-2 3-3 4-0 4-1 4-2 4-3 4-4 5-0 5-1 5-2 5-3 5-4 5-5

    if the user click the node 3-1 i need to have back:

    0-0 1-0 2-0 3-1 0-0 1-0 2-1 3-1 0-0 1-1 2-1 3-1

    I have asked in the chatterbox some days ago and oiskuu, Eily and Lanx were so kind to suggest various approach, but franckly i was not able to implement a simple way: infact not all combinations are valid: 0-0 1-0 1-1 2-1 3-1 contains the illigal lateral move 1-0 1-1

    Well I can produce all combinations and then throw away solutions with too much moves.. but for sure exists a simpler perlish way.

    Squeezing my brain I only ended with naive method to highlight the area of such valid tiles:

    sub enum_area{ my ($x,$y) = split '-', $_[0]; my $minus = $x - $y; print "$_ " for grep { my ($cx,$cy) = split '-',$_; $cy > $cx ? 0 : ( $cx-$cy < $minus + 1 ? 1 : 0) } glob '{'.(join ',',0..$x).'}-'. '{'.(join ',',0..$y).'}'; }

    thanks in advance


    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.
What are best practices for Fast CGI nowadays?
4 direct replies — Read more / Contribute
by mldvx4
on Mar 21, 2018 at 11:43

    Where can I look for the current best practices regarding implementing a perl script using Fast CGI? Which module(s) should I look at and which should I avoid? I'm starting to process data from HTML forms using Fast-CGI, but have done quite a lot with slow CGI in the distant past. The perl script will be communicating with the web server via UNIX sockets.

    Also, should the script fork ever? If so, when and what should be done with the UNIX socket to handle the forking?

how to get a 64bit random number with rand() ?
7 direct replies — Read more / Contribute
by iglake
on Mar 21, 2018 at 08:11

    Hello all monks,

    In the following code rand() doesn't even give me 52bit of data;
    both MSB and LSBs are fixed !

    what is the best way to get 64bit numbers ?

    0000000000000000010111100101110000011011100101010001000000001111 00005 +E5C1B95100F 0000000000001100111011111010100001011001101001100011011101111111 000CE +FA859A6377F 0000000000000011001010100001010110101011101000101010001000101111 00032 +A15ABA2A22F 0000000000000110110100001111000101010110001101100010111100011111 0006D +0F156362F1F 0000000000001101001011001010011110010100111010001101000101001111 000D2 +CA794E8D14F 0000000000000101110101011101011111101000111011101111111110111111 0005D +5D7E8EEFFBF 0000000000000001000001010001000100101100100010010110010101101111 00010 +5112C89656F 0000000000001111110001100010010000001010111000101101000101011111 000FC +6240AE2D15F 0000000000000100100100001010101010101111001011110110011010001111 00049 +0AAAF2F668F 0000000000001100100011011000010010011010000101000000101111111111 000C8 +D849A140BFF 0000000000001101000011001100011010101101010100010001110010101111 000D0 +CC6AD511CAF 0000000000000010100111010110010110111101100011110101011110011111 00029 +D65BD8F579F 0000000000000000001100001110001000000110010000100000111111001111 00003 +0E206420FCF 0000000000000101001111111000100011111000000101011001110000111111 00053 +F88F8159C3F 0000000000000100100110101110000010010110100101010000011111101111 00049 +AE0969507EF 0000000000000100111011101101111101110011110101100000000111011111 0004E +EDF73D601DF 0000000000000110110111111000000010010010010011100000110100001111 0006D +F80924E0D0F 0000000000000111011111011100100001010101101101111111000001111111 00077 +DC855B7F07F 0000000000001110110001011111000001110111011001000110011100101111 000EC +5F07764672F 0000000000001101011110001010000111001010101101010001000000011111 000D7 +8A1CAB5101F
    #!/usr/bin/perl $|++; srand(54321); for (1 .. 20) { my $n = int rand(0xF_FFFF_FFFF_FFFF); printf "\r%064b %016X\n",$n,$n; } exit $?;
last/next/redo usages
5 direct replies — Read more / Contribute
by hurricup
on Mar 20, 2018 at 16:29

    Perldoc says pretty same for them, like

    next cannot be used to exit a block which returns a value such as eval {} , sub {} , or do {} , and should not be used to exit a grep or map operation.

    I read cannot as doesn't work. But. In some modules, like Par::Dist I can see code like:

    File::Find::find( sub { next unless $File::Find::name; (-r && !-d) and push ( @files, substr($File::Find::name, 5) ); } , 'blib' );

    What is it? Outdated documentation? Do I get cannot wrong way? Some tricky next, like sub?

    Also, TIL, missing in docs, that last/next/redo may be used in statement modifiers, like say $_ and last for 1,2,3

    I'm working on new code inspection, so question is not rythorical.

Idiomatic Perl?
7 direct replies — Read more / Contribute
by thenextfart
on Mar 20, 2018 at 10:06
    I am currently learning Perl by trial/try/error, and I have a Python background. The purpose of the program of the subject of the question, however, is to find out what is idiomatic Perl and what is not. I don't want to end up writing Python in Perl. This is my program:
    use strict; use warnings; print "RegEx Engine 1.0\n________________\n"; print "Gimme a string: "; my $str = <STDIN>; print "Gimme a RegEx: "; my $pattern = <STDIN>; my $answer = eval("\"$str\" =~ $pattern"); if ($answer) { print "Yes!"; } else { print "No."; } print "\nkthxbye\n";
    Is this good/idiomatic/bad/ugly/encouraged/discouraged/ Perl? (Note: I am using Perl 5)
Date to Epoch
6 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 20, 2018 at 09:51

    I am trying to convert a file date to epoch using a single line of code in Solaris 10, which uses an older version of perl.

    I have been able to convert epoch to a date with your assistance:

    echo $epoch | perl -MPOSIX -e 'print strftime("%m%d%H%M", gtime <stdin>)'

    However, I would like to now reverse this process.

    I was thinking about something like the following:

    echo "Mar 20 2018 09:00" | perl -MPOSIX -e 'print strptime(<stdin>, "%s")'

    But, I receive an error "Undefined subroutine &main::strptime called at -e line 1, <stdin> line 1. I tried substituting <stdin> with $ARGV[0] but still receive a subroutine error.

Error running Gitlab CI pipeline on module
1 direct reply — Read more / Contribute
by loris
on Mar 20, 2018 at 05:54


    I'm jumping on the CD/CI bandwagon and have set up a Gitlab instance, created a new module with module-starter, and pushed it to the remote. In my local repository, running prove -l t works fine. I then added and pushed a .gitlab-ci.yml file with the following contents:

    stages: - test job 1: stage: test script: prove -l t tags: - perl

    The pipeline runs but fails with the following error:

      TAP::Object::_construct(TAP::Harness=HASH(0x5608cb0c3e98), "TAP::Parser", HASH(0x5608cb64f990)) called at /usr/share/perl/5.24/TAP/ line 852
            TAP::Harness::make_parser(TAP::Harness=HASH(0x5608cb0c3e98), TAP::Parser::Scheduler::Job=HASH(0x5608cb80a480)) called at /usr/share/perl/5.24/TAP/ line 651
            TAP::Harness::_aggregate_single(TAP::Harness=HASH(0x5608cb0c3e98), TAP::Parser::Aggregator=HASH(0x5608cb6171e0), TAP::Parser::Scheduler=HASH(0x5608cb80a420)) called at /usr/share/perl/5.24/TAP/ line 743
            TAP::Harness::aggregate_tests(TAP::Harness=HASH(0x5608cb0c3e98), TAP::Parser::Aggregator=HASH(0x5608cb6171e0), "t") called at /usr/share/perl/5.24/TAP/ line 558
            TAP::Harness::__ANON__() called at /usr/share/perl/5.24/TAP/ line 571
            TAP::Harness::runtests(TAP::Harness=HASH(0x5608cb0c3e98), "t") called at /usr/share/perl/5.24/App/ line 546
            App::Prove::_runtests(App::Prove=HASH(0x5608cb0b7d48), HASH(0x5608cb526548), "t") called at /usr/share/perl/5.24/App/ line 504
            App::Prove::run(App::Prove=HASH(0x5608cb0b7d48)) called at /usr/bin/prove line 13
    ERROR: Job failed: exit status 1

    From the above I can't even see what error has occurred. Can anyone illuminate me?



Text::Unaccent::PurePerl undef values
1 direct reply — Read more / Contribute
by IB2017
on Mar 18, 2018 at 15:50

    Dear Monks

    I'm sorting an array deaccenting its elements with Text::Unaccent::PurePerl.

    Everything is fine except that my array happens to have now and then some undef elements which apparently is not accepted by Text::Unaccent::PurePerl (Error: unac_string: Input character string is undefined). Any way to turn - on the run - these undef elements let's say, to an empty string ""? My array comes from reading a SQLite database. What would be the approach you suggest?

    @$ResultsFinal =( sort { unac_string($a->[($OptOrderToDisplayTable)]) +cmp unac_string($b->[($OptOrderToDisplayTable)])} @$ResultsFinal );
Hash key composition with a comma?
6 direct replies — Read more / Contribute
by mpersico
on Mar 16, 2018 at 16:09
    Just when I thought I had seen everything:
    my %hash; my $foo = 'hello'; my $bar = 'world'; my $baz = 'folk'; $hash{ $foo, $bar, $baz } = '##eek what magic is this?'; print $hash{"$foo\x{1c}$bar\x{1c}$baz"};
    ##eek what magic is this?

    By what rule are multiple variables, separated by a comma, turned into a string with a File Separator between the pieces? I mean if I squint hard enough, that expression for the key is a list, and then the list is "stringified" to be a key, in which case I would have expected a space (\x{20}) separated expression. Perl 5.16 and Perl 5.26.1

Using Net::SMTP to send pdf attachment
4 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 16, 2018 at 06:25

    Hi monks,

    I'm trying to send pdf files as attachment in emails sent with Net::SMTP. The PDF files reside on my website (like home/mysite/files/PDF_FILES_HERE).

    The code I have below is for sending normal emails:

    use strict; use Net::SMTP; use MIME::Base64; my $smtphost = 'some_smtp_host'; my $username = 'some_username'; my $password = 'some_password'; my $emailto = ''; my $emailfrom = ''; my $subject = 'Hello world'; my $message = 'Test message'; sub date_r { my ($monthday, $mon, $yr, $ time, $hour, $str); my (@lt) = (); @lt = localtime(); $monthday = $lt[3]; $mon = $lt[4]+1; $yr = $lt[5] + 1900; $hour = $lt[2]; $time = sprintf("%02d:%02d:%02d", $hour, $lt[1], $lt[0]); $str = $mon . '/' .$monthday . '/' . $yr . ' ' . $time; return $str; } my $smtp = Net::SMTP->new($smtphost, Debug => 1, Timeout => 5); $smtp->datasend("AUTH LOGIN\n"); $smtp->datasend(encode_base64($username)); $smtp->datasend(encode_base64($password)); $smtp->mail($emailfrom); $smtp->to($emailto); $smtp->data(); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("From: $emailfrom\n"); $smtp->datasend("To: $emailto\n"); $smtp->datasend("Date: " . date_r() . "\n"); $smtp->datasend("Subject: $subject"); $smtp->datasend("\n\n"); $smtp->datasend($message); $smtp->datasend("\n\n"); $smtp->dataend(); $smtp->quit;

    What do I need to change to make so that I can send attachments (pdf files)?

    Please enlighten me. Thank you in advance.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-03-23 10:14 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (290 votes). Check out past polls.