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
File::Find traversing a link into a mounted flash drive
1 direct reply — Read more / Contribute
by swampyankee
on Feb 05, 2016 at 12:40

    I did a quick search, but didn't quite find anything that was sufficiently close to be an answer

    I'm trying to use File::Find to find image files, so I can randomly change my wallpaper. I know; it's a silly task ⌣. My problem is that I have a bunch of image files on a flash drive, and File::Find won't follow a symbolic link to the contents of the flash drive. I suspect it's because File::Find, even when $File::Find::follow is set won't recurse into a different file system.

    I'm using Fedora 21, Perl v5.18.4, File::Find version 1.23.

    sub image_search { my $name = $File::Find::name; my $dir = $File::Find::dir; my @globbed; my @temp; my $images = '(png$)|(jpg$)|(gif$)|(jpeg$)'; if (-l $name) { print "processing link named $name\n"; @globbed = glob("$name/*"); } else { print "processing directory named $dir\n"; @globbed = glob("$dir/*"); } if (@globbed) { @globbed = grep {m/$images/i} @globbed; $image_list{$dir} = [@globbed]; } } ## end sub image_search
    Sorry for the a) less-than-optimal code design and b) absence of comments.

    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

How can I acces the "member" stepxxx of the structure given below?
5 direct replies — Read more / Contribute
by adrya407
on Feb 05, 2016 at 07:35
    $VAR1 = { 'opt' => { 'step820' => '0', 'step190' => '0', 'step124' => '0', 'step127' => '0', 'step410' => '0', 'step015' => '2', 'step130' => '1', 'step013' => '0', 'step715' => '0', 'step540' => '0', 'step800' => '5', 'step135' => '0', 'step003' => '0', 'step455' => '0', + } };
    How can I acces the "stepxxx"? I need to acces it's name and verify if it equals a variable $x like this:
    if $stepxxx eq $x{ #do something; }
Appending Text of One XML Node to that of the Other
4 direct replies — Read more / Contribute
by thomasd
on Feb 04, 2016 at 17:47

    I am having difficulties trying to figure out how to append the text from one Child Node to that of another before moving to the next parent.

    I have tried using a few different XML libraries and am currently testing XPath. That is before I decide to just write my own parser which I would rather not do right now.

    The following is a sample of the XML page

    <xdoc> <MsgSigs> <MsgSig> <Description>TSC1 - Torque/Speed Cntrl 1</Description> <Key>ln1</Key> <XtdFrame>True</XtdFrame> <NetworkKey>net0</NetworkKey> <MsgSignals> </MsgSignals> </MsgSig> <MsgSig> <Description>TSC1 - Torque/Speed Cntrl 1</Description> <Key>ln2</Key> <XtdFrame>True</XtdFrame> <NetworkKey>net0</NetworkKey> <MsgSignals> <Signal> <Description>0_SPN695 Eng. Override </Description> <Key>sig695</Key> <ValueType>1</ValueType> </Signal> </MsgSignals> </MsgSig> </xdoc>

    What I am trying to do is loop through the MsgSig nodes and take the key (eg, ln1, ln2) and append this key to the Description. Note: I am not wanting to go into the Signal Node (why I ruled out DOM). The result would look something like this

    <xdoc> <MsgSigs> <MsgSig> <Description>TSC1 - Torque/Speed Cntrl 1 (ln1)</Description> <Key>ln1</Key> <XtdFrame>True</XtdFrame> <NetworkKey>net0</NetworkKey> <MsgSignals> </MsgSignals> </MsgSig> <MsgSig> <Description>TSC1 - Torque/Speed Cntrl 1 (ln2)</Description> <Key>ln2</Key> <XtdFrame>True</XtdFrame> <NetworkKey>net0</NetworkKey> <MsgSignals> <Signal> <Description>0_SPN695 Eng. Override </Description> <Key>sig695</Key> <ValueType>1</ValueType> </Signal> </MsgSignals> </MsgSig> </xdoc>

    As I mentioned I am currently using XPath and as a test have made it to here and here is where I sit.

    #!/usr/bin/perl -w use XML::XPath; use Data::Dumper; $file = "test.xml"; $xp = XML::XPath->new(filename => $file); @nodes = $xp->findnodes("/xdoc/MsgSigs/MsgSig"); foreach (@nodes) { $key = $_->findvalue('Key'); $descr = $_->findvalue('Description'); $text = $descr . "(" . $key . ")"; # A setValue would be awesome right about now or an # appendtext $_-> print $_->findvalue('Description'), "\n"; } #print out to xml file

    Any help would be very much appreciated.

    D. Thomas

Grouping Data
3 direct replies — Read more / Contribute
by okieheart
on Feb 04, 2016 at 16:37

    I recently got assigned some projects that use PERL, and I have no experience with it so I'm flying solo here. So I apologize in advance for using the wrong terminology or not clarifying my question.

    I have a section of code that groups records by diagnosis codes. Right now it groups anything that has a specific code number, but now I need to create a group of "everything else". Here's the current code. I need to change the Unspecified group to include everything not included in group A or group B, instead of having to list each one as I can have up to 100+ different diagnosis codes each time. The diagnosis codes are a text field, if that makes a difference, and some have numbers and letters.

    @DiagGroupA = qw(2770 27700 27701 27702 27703 27707 27709); @DiagGroupB = qw(277); @UNSPECIFIED = qw(0704 07049); foreach $diagnosis (@DiagGroupA) { $DiagGroupA_list{$diagnosis} = 1; } foreach $diagnosis (@DiagGroupB) { $DiagGroupB_list{$diagnosis} = 1; } foreach $diagnosis (@UNSPECIFIED) { #$DiagUNSPECIFIED_list{$diagnosis} = 1;
Dependant Arrays intersection
3 direct replies — Read more / Contribute
by simong
on Feb 04, 2016 at 16:17

    Hi all, i'm stucked on solve this situation: 2 arrays @a and @b containing both path elements. @a is quite heavy array of full path string;@b insted is a shorter array containing just a segment, a frame, of some of the elements in @a. Using (or not) a fast grep function, need to select the element in @a which satisfy (match)the content of @b. I try this , but not working:

    %tmp = map{ $_ => 1 } @b; @inte = grep(!/$tmp{$_}$/, @a) ;

    Any advise ? thanks Simon

    Sorry I posted the wrong code, but in any case cant how to match $tmp variable

    %tmp = map{ $_ => $_ } @b; @inte = grep(/$tmp{$_}$/, @a) ;

    ..in my intention would find, for every $_ of @a if any $_ of $tmp{$_} is part or $_ in @a

    An example

    @a = ('d:\1\1\1.txt', 'd:\1\1\2.txt', 'd:\1\1\3.txt', 'd:\1\1\4.txt')
    @b = ('\1\2.txt', '\1\3.txt'
PERL_ANYEVENT_NET_TESTS environment variable not set
2 direct replies — Read more / Contribute
by cbtshare
on Feb 04, 2016 at 14:48

    Can I please get some assitance in resolving my issue.I have a a centos 6, 6b -bit, with perl, v5.10.1. I am trying to install aws api module VM::EC2. I need to first install AnyEvent, when I go to install it "cpan AnyEvent", I get the following error:

    t/80_ssltest.t ............. skipped: no usable Net::SSLeay t/81_hosts.t ............... ok t/handle/01_readline.t ..... ok t/handle/02_write.t ........ ok t/handle/03_http_req.t ..... skipped: PERL_ANYEVENT_NET_TESTS environm +ent variable not set t/handle/04_listen.t ....... AnyEvent::Handle uncaught error: Connecti +on refused at /root/.cpan/build/AnyEvent-7.12-JRk62y/blib/lib/AnyEven +t/Socket.pm line 1000. t/handle/04_listen.t ....... Dubious, test returned 111 (wstat 28416, +0x6f00) Failed 2/2 subtests Test Summary Report ------------------- t/handle/04_listen.t (Wstat: 28416 Tests: 0 Failed: 0) Non-zero exit status: 111 Parse errors: Bad plan. You planned 2 tests but ran 0. Files=82, Tests=248, 72 wallclock secs ( 0.77 usr 1.17 sys + 3.37 cu +sr 3.78 csys = 9.09 CPU) Result: FAIL Failed 1/82 test programs. 0/248 subtests failed. make: *** [test_dynamic] Error 111 MLEHMANN/AnyEvent-7.12.tar.gz /usr/bin/make test -- NOT OK //hint// to see the cpan-testers results for installing this module, t +ry: reports MLEHMANN/AnyEvent-7.12.tar.gz

    I also tried to install NET::SSLeay, but it hangs

    rm -f blib/arch/auto/Net/SSLeay/SSLeay.so LD_RUN_PATH="/usr/lib64" gcc -shared -O2 -g -pipe -Wall -Wp,-D_FORTIF +Y_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 - +m64 -mtune= eay.so +\ -L/usr -L/usr/lib64 -L/usr/lib -lssl -lcrypto -lz \ chmod 755 blib/arch/auto/Net/SSLeay/SSLeay.so "/usr/bin/perl" "-Iinc" -MExtUtils::Command::MM -e 'cp_nonempty' -- SS +Leay.bs blib/arch/auto/Net/SSLeay/SSLeay.bs 644 Manifying 2 pod documents MIKEM/Net-SSLeay-1.72.tar.gz /usr/bin/make -- OK Running make test Running Mkbootstrap for Net::SSLeay () chmod 644 "SSLeay.bs" PERL_DL_NONLAZY=1 "/usr/bin/perl" "-MExtUtils::Command::MM" "-MTest::H +arness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'inc', +'blib/lib', t/handle/local/05_use.t ................ ok t/local/01_pod.t ....................... skipped: Test::Pod 1.00 requi +red for testing POD t/local/02_pod_coverage.t .............. skipped: these tests are for +only for release candidate testing. Enable with RELEASE_TESTING=1 t/local/03_use.t ....................... ok t/local/04_basic.t ..................... ok t/local/05_passwd_cb.t ................. ok t/local/06_tcpecho.t ................... Use of uninitialized value $g +ot in string eq at t/local/06_tcpecho.t line 66.
execute Oracle anonymous procedure using perl
2 direct replies — Read more / Contribute
by hs
on Feb 04, 2016 at 14:25
    Hello, I am trying to run a ORacle anonymous block using perl DBI, but I am receiving error as mentioned below. Please help.. sub runThis($procT) { my $dbh1=DBI->connect("dbi:ODBC:$db","$usr","$pass", {RaiseError => 1 , PrintError => 0} ); $sqlstmt1 = "BEGIN $procT END;"; $sth1 = $dbh1->do($sqlstmt1) or die $dbh1->errstr; $dbh1 -> disconnect; } runThis("BEGIN NULL; END;"); --------------------------------------- Error message: DBD::ODBC::db do failed: OracleODBCOraORA-06550: line 1, column 7: PLS-00103: Encountered the symbol "END" when expecting one of the following: ---------------------------- Thanks
Introspecting the current Perl file via DATA, even w/o DATA?
3 direct replies — Read more / Contribute
by LanX
on Feb 04, 2016 at 13:18
    I'm using this code to read the current Perl file into @line to be able to enrich my error messages with the originating code line.
    CHECK { my $datapos = tell main::DATA; seek main::DATA,0,0; @lines =<main::DATA>; seek main::DATA, $datapos,0; } __DATA__ whatever

    This works only if there is a __DATA__ line, otherwise the filehandle DATA doesn't exist.

    I'm pretty sure that DATA is only an alias of anther filehandle pointing to the current code...

    Any idea how to get this filehandle?

    (don't want to explicitly open $0 again for reasons of complexity and performance)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

IO::All fails on 203 response
1 direct reply — Read more / Contribute
by geoffleach
on Feb 04, 2016 at 10:46
    IO::All $foo = io($url);
    In the case that the content at $url is split up by the server. For example fetching the same URL with wget, I see the response:
    206 Partial Content
    My question: is there any way to persuade IO::All to process the partial content?


if defined not working
3 direct replies — Read more / Contribute
by liorlew
on Feb 04, 2016 at 10:42

    hi, I am running the following code:

    use strict; use warnings; my $newvar=0; if (defined $undefvar) { $newvar = $undefvar; }

    and getting the following error:

    Global symbol "$undefvar" requires explicit package name at C:\Users\l +iorlew\Desktop\perl\fixes\scripts\test.pl line 5. Global symbol "$undefvar" requires explicit package name at C:\Users\l +iorlew\Desktop\perl\fixes\scripts\test.pl line 7.

    this is not the first time I used this functionality, but I do not seem to find what is wrong with the code.

    Thank you,


Add your question
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.