Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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
why does location of function matter?
6 direct replies — Read more / Contribute
by smartyollie
on Jun 01, 2015 at 17:54
    I have run into a situation several times at my current job where the location of a perl function affects whether it works or not. I've never had this happen before and feel like I'm missing something obvious. The current scenario happens on both Windows (Strawberry Perl) and UNIX (Solaris v5.8.4). I have a function called PostValidate with one argument. When I put the function definition AFTER the main body of code, it runs; when I put the function before, it fails with

    Too many arguments for main::PostValidate at line 349, near "$ofile)"

    The call within the main body looks like this:

    if ($validate) {

    I don't have any prototypes defined and I'm really at a loss as to why it should matter where the function is. Any ideas?
Serial communication between perl and arduino
4 direct replies — Read more / Contribute
by perl_sck_58
on Jun 01, 2015 at 11:32

    Hi Monks, I am trying to setup a serial communication between arduino uno and pc using Perl. I am using Win32::SerialPort module for the same

    The problem is I am not able to read or write to the arduino In file I am saving the settings to conf.cfg file and in I am trying to write to arduino

    use strict; use Win32::SerialPort; my $ob = Win32::SerialPort->new ('COM22') || die; $ob->baudrate(9600); $ob->parity("none"); $ob->parity_enable(1); # for any parity except "none" $ob->databits(8); $ob->stopbits(1); $ob->handshake('none'); $ob->buffers(4096, 4096); $ob->write_settings || die "cant write settings"; $ob->save("conf.cfg"); print "wrote configuration file conf.cfg\n";

    use strict; use Win32::SerialPort 0.11; my $ob = Win32::SerialPort->start ("conf.cfg") || die; $ob->write("abc"); $ob->write("xyz"); undef $ob;

    After running this code I get a message that "Second Write attempted before First is done at line 10. Use of uninitialized value $written in numeric ne (!=) at C:/Perl64/site/lib/Win 32/ line 1580."

    In the arduino program I am just waiting to receive for any serial data

    void setup() { pinMode(13,OUTPUT); Serial.begin(9600); } void loop() { if(Serial.available()) { pinMode(13,HIGH); } }

    I also tried adding a delay between the two serial writes in the perl program, but also got the same error message.

    Please let me know if I need to do any other configurations or if I am doing something wrong

    Thanks all

XML Simple
4 direct replies — Read more / Contribute
by Arenas
on May 31, 2015 at 08:03

    I start with perl now. I don't no how to do.

    (I comms from Powershell, sorry)




    #!C:/Dwimperl/perl/bin/perl.exe use strict; use warnings; use LWP::UserAgent; my $url = ''; my $ua = LWP::UserAgent->new(); my $response = $ua->get($url); if($response->is_success) { # print $response->decoded_content; my $city = XML::Simple->new; print $city->{city}->{population} print population ? print bases =>tra ? } else { die $response->status_line; }

    The module xml::simple is not load

    How format numbers (1246633 => 1 246 633)



WCF alternative in Perl
1 direct reply — Read more / Contribute
by Divan
on May 30, 2015 at 22:12

    I'm a primarilly a .NET/js/DB developer but I know a little bit of Perl (learned the basics as my first programming language).

    I've just recieved a small project in Perl 5.10+ and that sparkled my interest in Perl again. I've decided to master it alongside with Linux (debian based) which I use ocasianlly.

    I've assembled a reading list of, what should be, the best books on Perl.

    Now, I'm interested in service oriented programming, SOA, and since I have experience with WCF, I'm looking for something "similar" in Perl.

    I was looking at Kensho::Task and saw POE and IO::Async for async programming. I also saw Mojolicious, Dancer, Catalyst and now I'm wondering where to start, what is preffered in the community etc.(I know that these things have different purposes)

    Should I first learn the low level stuff (fork, socket programming) and then middlewares, before diving into high level stuff or just start at the top ?

    Thanks in advance.

get each column key/value
2 direct replies — Read more / Contribute
by Peamasii
on May 30, 2015 at 10:38
    Hello, I have been trying to figure out how to do this with DBI. I have a few tables each containing different fieldnames and just one row of values. I'd like to write a sub which stores the column name and corresponding value in row, in a hash, in the order they are in. The I want to loop over the hash and retrieve the key/value pairs. How can I do this? I looked at at fetchrow_hashref and fetchrow_arrayref but the columns were not returned in order. Any help would be appreciated. Update: I found the answer
    sub print_stats { my($table) = @_; my $sth = $dbh->prepare('SELECT * FROM '.$table) or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute # Execute the query or die "Couldn't execute statement: " . $sth->errstr; my $names = $sth->{NAME}; # or NAME_lc or NAME_uc while( my $report_row = $sth->fetchrow_arrayref() ) { for my $col_idx ( 0 .. $#{$names} ) { print "$names->[$col_idx]: $report_row->[$col_idx]\n"; } } }
decimal to binary conversion
3 direct replies — Read more / Contribute
by mp0065789
on May 30, 2015 at 07:10

    Can anyone help here:

    #!/usr/bin/perl -w use strict; use warnings; print "Enter decimal number less than 256:"; my $decimal; $decimal=<STDIN>; #chomp $decimal; print $decimal & 128 <=> 0; print $decimal & 64 <=> 0; print $decimal & 32 <=> 0; print $decimal & 16 <=> 0; print $decimal & 8 <=> 0; print $decimal & 4 <=> 0; print $decimal & 2 <=> 0; print $decimal & 1 <=> 0 ;
    output: F:\Perl\code> Enter decimal number less than 256:255 11111111 F:\Perl\code> Enter decimal number less than 256:12 00000000 F:\Perl\code> Enter decimal number less than 256:8 00000000

    output is coming wrong for all except 255. Frown

tie variable
2 direct replies — Read more / Contribute
by grasshopper!!!
on May 29, 2015 at 17:31

    Is it possible to tie a scalar to Tk entry and a subroutine as to return length of input on each letter input. Writting twitter client and wish to display number of characters input to entry in label on each keystroke at a loss. thank you

what are my options with perl, sybase dblib?
1 direct reply — Read more / Contribute
by taiq_m
on May 29, 2015 at 15:42
    i am on solaris 10 using sybase dblib on most of my perl scripts. we are now moving to RHEL 6. Pls note I have no root access, can i get sybase dblib to work on rhel? we have DBI on rhel but no dbd sybase. can i have dbd sybase installed on one of my custom libraries? below is what i have. perl -v This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
Threads From Hell #3: Missing Some Basic Prerequisites
7 direct replies — Read more / Contribute
by karlgoethebier
on May 29, 2015 at 14:43

    Hi all,

    I try to create a queue for multithreaded processing of a huge image file.

    Here is what i figured out:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dump; use Time::HiRes qw(time); # use feature qw(say); my $start = time; my @queue; my @line; # ratio 1.25 - i guess 160 pixel # my $width = 20; # my $height = 8; # my $qsize = 4; # same ratio - i guess 20 MP my $width = 1280 * 4; my $height = 1024 * 4; my $qsize = 32; for my $x ( 0 .. $width - 1 ) { for my $y ( 0 .. $height - 1 ) { my $coords = [ $x, $y ]; push @line, $coords; if ( scalar @line == $width ) { push @queue, [@line]; @line = (); # audacious actions start here if ( scalar @queue == $qsize ) { # dd \@queue; @queue = (); } } } } printf "Took %.3f seconds\n", time - $start; __END__ karls-mac-mini:monks karl$ ./ Took 13.531 seconds

    It looks like it works as designed.

    But i can't suppress the vague feeling that this solution is ungeschickt.

    How can i improve this code?

    Update: Thank you very much to all that contributed to this interesting thread. I have little time this week, but when i have studied the numerous examples given, i'll reply more detailed respectively ask more questions :-)

    Thank you very much for any hint and best regards,


    «The Crux of the Biscuit is the Apostrophe»

Serverspec, but using Perl?
2 direct replies — Read more / Contribute
by neilwatson
on May 29, 2015 at 09:33


    Do you know if there is a Perl equivilant to Serverspec? If you were to build such tool in Perl how you go about it?

    Neil Watson

system stdout redirected ok to a file but not to a variable.
5 direct replies — Read more / Contribute
by guybrush
on May 28, 2015 at 16:56

    Hi, I'm trying to run a command on a unix box using system because I need the exit code. but I also need the stdout and stderr. so I try some STDOUT redirection unfortunatelly it works if I redirect to a file:

    open(STDOUT, '>' ,"kk.txt" ) or die "Can't redirect stdout: $!";

    But not when I do it to a variable:

    open(STDOUT, '>' ,\$output ) or die "Can't redirect stdout: $!";

    This is my Testing code:

    #!/usr/bin/perl my $exitcode ; my $output=''; my $error=''; # take copies of the file descriptors open(OLDOUT, ">&STDOUT"); open(OLDERR, ">&STDERR"); #close current outs as per manual of open close(STDOUT) or die "Can't close STDOUT: $!"; close(STDERR) or die "Can't close STDERR: $!"; # redirect stdout and stderr open(STDOUT, '>' ,\$output ) or die "Can't redirect stdout: $!"; #open(STDOUT, '>' ,"kk.txt" ) or die "Can't redirect stdout: $!"; open(STDERR, '>' ,\$error ) or die "Can't redirect stderr: +$!"; printf "Before system\n"; # run the program system("echo I cant get this into a variable"); $exitcode=($? >>8); printf "After System\n"; # close the redirected filehandles close(STDOUT) or die "Can't close STDOUT: $!"; close(STDERR) or die "Can't close STDERR: $!"; # restore stdout and stderr open(STDOUT, ">&OLDOUT") or die "Can't restore stdout: $!"; open(STDERR, ">&OLDERR") or die "Can't restore stderr: $!"; # avoid leaks by closing the independent copies close(OLDOUT) or die "Can't close OLDOUT: $!"; close(OLDERR) or die "Can't close OLDERR: $!"; printf "Exitcode: %d\n" ,($exitcode); printf "still here\n"; print $output ; print $error ;

    Please any help on why it does not work the redirection when is a variable?

    Also if posible. how can I do this without system. remember I need exitcode stdout and stderr and I do not want to do any alter to the actual command to do unix redirections.

    Many many thanks.


WebService::Plotly dies with HTTP::Response=HASH(...)
1 direct reply — Read more / Contribute
by docdurdee
on May 28, 2015 at 14:12
    Dearest Monks, I'm trying to use the WebService::Plotly module. It works great on two computers (running p5.20.1 on OS X 10.10.3), but it dies without warnings on three other computers (running p5.20.1 on OS X 10.8.4) dumping: HTTP::Response=HASH(0x7f87ba65afe0) Do you have any suggestions as to how to solve this problem? Here is the code:
    use Modern::Perl; use WebService::Plotly; my $plotly = WebService::Plotly->new( un => $user, key => $key); my @ss = map {[split]}( '2.1120 0.0047', '2.1160 0.0045', '2.1200 0.0058', '2.1240 0.0045', '2.1280 0.0036', '2.1320 0.0029', '2.1360 0.0024', '2.1400 0.0032', '2.1440 0.0020', '2.1480 0.0014', ); my @ds = map{$_->[0]} @ss; my @ps = map{$_->[1]} @ss; my $response = $plotly->plot( \@ds, \@ps ); print "url is: $response->{url} \n"; print "filename on our server is: $response->{filename} \n";
    For the computers that it works, I can dump $response:
    $VAR1 = { 'filename' => 'plot from API (7)', 'warning' => '', 'url' => '', 'message' => '', 'error' => '' };
    Thanks! D
New Monk Discussion
Can I please have multiple downvotes per (certain monk's) posts.
13 direct replies — Read more / Contribute
by BrowserUk
on May 29, 2015 at 10:30

    I'd willing give up 5 XP per downvote to do it. 10 even.

    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
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2015-06-02 20:35 GMT
Find Nodes?
    Voting Booth?

    What kind of chocolate gives you the most pleasure?

    Results (109 votes), past polls