If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
Embedded function or subroutine - which is better?
3 direct replies — Read more / Contribute
by Linicks
on Aug 27, 2016 at 12:51

    Hi All

    I don't know how perl pre-parses code on the fly - is it quicker (i.e. will perl map out a call to a sub-routine on pre-parse) such as


    sudo code if ($debug == "true") { &debugit; # call the routine } or if ($debug == "true") { ** do all the debug code here and/or carry on ** }

    Thanks,

    Nick

Using unpack on Windows with external data
2 direct replies — Read more / Contribute
by locinus
on Aug 27, 2016 at 07:33

    Hi monks.

    I'm new using unpack function to get data from an external binary source. I'm having trouble getting the right values.

    My first value is coded as an unsigned 16-bit integer, little-endian. When I open it with my hex editor, I can see that the two first bytes are coded "DC 20", which represents 8412, which is the value I expect.

    My perl code is the following:

    my $fh = FileHandle->new; open ($fh, '<', $filename) or die 'missing file'; binmode $fh, ':raw'; my @values = unpack 'S<', $fh; print @values;

    and provides the value 26950. I really can't figure out where this data comes from. Same if I provide '<:raw' to the open function.

    I'm running on Windows 7 64-bit, Perl 5.12.3.

    Any idea/suggestion? I'm clueless right now.

How do I replace the camel icon on Windows exe built with pp?
2 direct replies — Read more / Contribute
by perltux
on Aug 27, 2016 at 05:52
    I use 'pp' to build self-contained exe files of my graphical (Tk) Perl applications for MS Windows. The icons in the task bar and in the top left corner of the application window show up correctly when setting them with $mw->iconimage($icon), but on the desktop and in the windows file browser I still get the default camel icon.

    In the past 'pp' supported the '--icon' switch which allowed to specify a custom icon, but recent versions of 'pp' no longer support that switch.

    So how do I replace this camel icon with my own custom icon with recent versions of 'pp'?
Rename Windows files with Unicode chars
1 direct reply — Read more / Contribute
by mnooning
on Aug 26, 2016 at 21:32

    I get files in Windows 7 that have wide chars. I need to rename them after stripping the non-ascii characters from them. Opendir and then readdir (in a while clause) does not work because readdir does bytes!

    Below is code that I also tried.


    # File name = a1.pl # Script to get rid of wide chars and non-ascii chars # in a Windows 7 file name. # This does not work. # In the Windows 7 file explorer, the file name shows as # "z &#8206;ay&#8206; &#8206;Pow&#8206;.mp4" # Note: There is at least one embedded wide char in the above # pasted file name. # The Windows 7 command promt shows the file name # as # "z ?ay? ?Pow?.mp4". use 5.14.2; # From: how to read unicode filename # http://www.perlmonks.org/?node_id=536223 open fList, '-|:encoding(UTF-16LE)', 'cmd /U /C dir /W'; # Note: I tried to opendir and readdir. I got the shortened # 8.3 character file name whenever a wide character # was in the file name. I could not rename. foreach (<fList>) { utf8::encode($_); my $orig_name = $_; my $new_name = $_; if ($new_name =~ m/.mp4/i) { print " 1 orig_name is \"$orig_name\"\n"; $new_name =~ s![^[:ascii:]]!!ig; print " 2 new name is \"$new_name\"\n"; rename "$orig_name", "$new_name"; # Does not work } } __END__ In the results below, note that the end double quotes are not at the end of the file name line! That should not be! >a1.pl 1 orig_name is "z &#915;ay&#915; &#915;Pow&#915;.mp4 " 2 new name is "z ay Pow.mp4 "
Perl Expect.pm
No replies — Read more | Post response
by MojaveJoe
on Aug 26, 2016 at 15:38

    So I am having a problem getting my perl expect script to work. It works fine on most commands but there is some weirdness on others. I essentially execute this command and it appears that there are several responses before it hits the match. Each time it waits, it clears the buffer before getting the next response. Here is the debug output (edited for brevity - notice the snips):

    Starting EXPECT pattern matching... at /usr/local/share/perl5/Expect.pm line 597 Expect::expect('Expect=GLOB(0x22cd510)', 60, '-re', ']MyPro +mpt>|]MyPrompt#|]MyPrompt...') called at ./get_ippools_exp.pl line 19 +2 main::cmd('Expect=GLOB(0x22cd510)', 'show config context da +ta | grep "ip pool"') called at ./get_ippools_exp.pl line 75 main::getPools() called at ./get_ippools_exp.pl line 25 spawn id(4): beginning expect. Timeout: 60 seconds. Current time: Fri Aug 26 13:50:58 2016 Waiting for new data (60 seconds)... spawn id(4): new data. spawn id(4): read 92 byte(s). show config context data | grep "ip pool" Friday August 26 18:50:58 UTC 2016 Waiting for new data (60 seconds)... spawn id(4): new data. spawn id(4): read 1815 byte(s). ip pool data1 ... ip pool data2 ... <snip> ip pool data16 ... Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 2048 byte(s). ip pool data17 ... ip pool data18 ... <snip> ip pool data38 ... ip Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 351 byte(s). pool data39 ... ip pool data40 ... ip pool data41 ... Waiting for new data (56 seconds)... spawn id(4): new data. spawn id(4): read 33 byte(s). <----here it receives the prompt!!!! [local]MyPrompt> Returning from expect successfully. spawn id(4): accumulator: ` '<----You can see that the buffer is dum +ped

    The code to call the command is simple:

    my ($exp, $cmd) = @_; $exp->print($cmd, "\n"); $exp->expect($timeout, -re => $prompt) or return; my $output = $exp->before; $output =~ s/^$cmd\n//; # remove the sent cmd return $output;
    It seems like each time the spawn id(4) process goes into "Waiting for new data (.. seconds)..." mode it dumps the prior values. Can anyone help me with this? Thank you!
Win32::SerialPort install error on Strawberry Perl PDL 5.24.0.1-32bit
1 direct reply — Read more / Contribute
by derneue
on Aug 26, 2016 at 02:35
    Hi Monks, does anybody knows if Win32::SerialPort works with Win7x64 and the Perl Version noted in the title? I receive the following error after fetching from CPAN:
    Creating new t/DefaultPort.pm Creating new nomake_test Creating new nomake_install Checking if your kit is complete... Looks good Generating a dmake-style Makefile Writing Makefile for Win32::SerialPort Writing MYMETA.yml and MYMETA.json BBIRTH/Win32-SerialPort-0.22.tar.gz C:\strawberry-perl-5.24.0.1-32bit-PDL\perl\bin\perl.exe Makefile.PL +-- OK Running make for B/BB/BBIRTH/Win32-SerialPort-0.22.tar.gz cp lib/Win32/SerialPort.pm blib\lib\Win32\SerialPort.pm cp lib/Win32API/CommPort.pm blib\lib\Win32API\CommPort.pm BBIRTH/Win32-SerialPort-0.22.tar.gz C:\strawberry-perl-5.24.0.1-32bit-PDL\c\bin\dmake.exe -- OK Running make test "C:\strawberry-perl-5.24.0.1-32bit-PDL\perl\bin\perl.exe" "-MExtUtils: +:Command::MM" "-MTest::Harness " "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib\lib', 'b +lib\arch')" t/*.t t/test1.t .. 1/309 # Failed test at t/test1.t line 534. t/test1.t .. 305/309 # Looks like you failed 1 test of 309. t/test1.t .. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/309 subtests t/test2.t .. ok t/test3.t .. ok t/test4.t .. ok t/test5.t .. ok t/test6.t .. 1/? SetCommState failed at t/test6.t line 306. t/test6.t .. ok t/test7.t .. ok Test Summary Report ------------------- t/test1.t (Wstat: 256 Tests: 309 Failed: 1) Failed test: 195 Non-zero exit status: 1 Files=7, Tests=1808, 62 wallclock secs ( 0.11 usr + 0.22 sys = 0.33 +CPU) Result: FAIL Failed 1/7 test programs. 1/1808 subtests failed. dmake.exe: Error code 255, while making 'test_dynamic' BBIRTH/Win32-SerialPort-0.22.tar.gz C:\strawberry-perl-5.24.0.1-32bit-PDL\c\bin\dmake.exe test -- NOT OK //hint// to see the cpan-testers results for installing this module, t +ry: reports BBIRTH/Win32-SerialPort-0.22.tar.gz Stopping: 'install' failed for 'Win32::SerialPort'. Failed during this command: BBIRTH/Win32-SerialPort-0.22.tar.gz : make_test NO cpan>
Code rendering issue with specific vars in MetaCPAN POD
2 direct replies — Read more / Contribute
by stevieb
on Aug 25, 2016 at 13:46

    I've found an interesting issue with POD rendering on MetaCPAN.

    I have the following snippet in the POD for one of my modules:

    =head2 send_cmd($command) Sends any arbitrary command to the LCD. (I've never tested this!). Parameters: $command Mandatory: A command to submit to the LCD. =head2 position($x, $y) Moves the cursor to the specified position on the LCD display. Parameters: $x Mandatory: Column position. C<0> is the left-most edge. $y Mandatory: Row position. C<0> is the top row.

    The $x and $y variables don't show up. The code block for them does, but the variables don't. Things work as expected for the $command variable in the send_cmd(), as it shows up fine. You can see that here.

    It works with Pod::Readme:

    position($x, $y) Moves the cursor to the specified position on the LCD display. Parameters: $x Mandatory: Column position. 0 is the left-most edge. $y Mandatory: Row position. 0 is the top row.

    ...and even in view source on meta:

    <h2 id="position-x-y">position($x, $y)</h2> <p>Moves the cursor to the specified position on the LCD display.</p> <p>Parameters:</p> <pre><code> $x</code></pre> <p>Mandatory: Column position. <code>0</code> is the left-most edge.</ +p> <pre><code> $y</code></pre> <p>Mandatory: Row position. <code>0</code> is the top row.</p>

    ...but doesn't render properly in any browser I've tested with.

    I'm wondering if I should just work around this, or whether this is a known issue (I couldn't find anything by searching), and if it's worth taking up the chain or not (perhaps other specific vars don't render correctly either).

    Can anyone please let me know if they are seeing what I am?

perl dbi, prepare function using place holder
1 direct reply — Read more / Contribute
by leostereo
on Aug 25, 2016 at 11:56

    Hi guys , im trying to declare an 'insert into' statement for a prepare function using a place holder for the table name but ... can not get it to work, the idea is as follows:

    $sth = $dbh->prepare("INSERT INTO ? (ip,bsid,firmware) VALUES (?, ?, ? +)"); $sth->execute($table_name,$ip,$bsid,$firmware);

    but im getting:

    DBD::mysql::st execute failed: called with 4 bind variables when 3 are + needed
Printing an element of a list not an array
4 direct replies — Read more / Contribute
by raghuprasad241
on Aug 25, 2016 at 11:13
    GMAN all,

    GMAN (Good Morning, Afternoon, Night) depending on where you are :-)

    Is it possible to print individual elements of a list with out putting them into an array ? I think putting the list into an array is a wastage of memory if you are just trying to print it. For e.g. I am trying to do below with no success

    perl -e 'use v5.10; say (localtime)[1]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

    Am I missing something simple ?

    Thanks!
    A Monk!
finding a file exist amongst many files
2 direct replies — Read more / Contribute
by t-rex
on Aug 25, 2016 at 10:00

    Hello Monks, I want to test if a file with name tpsm exists, and my folder might contain many file such as

    tpsm_01 tpsm_12 tpsm_23 . . . and so on

    the above code didnt work as i didnt have exact file name , on the shell we can directly use file* to check for multiple files with the same name ( some part of the name) but how fdo i check here?

    basically i want to test if a file with name tpsm _x exists or not, i don't know what will be the value of x , but i just want to know if files having such names are there or not. i tried with

    $pwd = $ENV{'PWD'}; $file = "$pwd/tpsm*"; if (-e $file) { #do something }

    please let me know how can i achieve this


Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.