Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Seekers of Perl Wisdom

( #479=superdoc: print w/replies, xml ) Need Help??

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
How do I run MBTI personality test in Linux Mint?
1 direct reply — Read more / Contribute
by tayacha
on Oct 23, 2016 at 07:47

    Hi, I am an extreme newbie. I found a personality test I don't know how to run it in Linux Mint. I installed it but do not know what to do with the following information

    use Personality::Type::MBTI; my $mbti = Personality::Type::MBTI->new(); # sample results from a questionnaire my @test = qw/i e i e i n s n s n t f t f t p j p j p/; # calculate type my $type = $mbti->type( @test ); print "Your type is '$type'\n";

    How do I use that information? How do I run it? I was able to successfully create my first but that is the extent of my Perl knowledge.

    thank you for your help

File Handle questions
2 direct replies — Read more / Contribute
by demichi
on Oct 23, 2016 at 07:37

    I have 2 questions.

    1) Avoid warning message

    use strict; use warnings; my $logfile = "test.log"; my $log_fh = *LOG_FH; LOG_MSG_OPEN($log_fh,$logfile); ... sub LOG_MSG_OPEN { my $par_fh = $_[0]; open($par_fh,"> $par_filepath") or die ("Can't open $par_filepath: + $!\n"); $par_fh->autoflush(1); }

    gives me this error message: Name "main::LOG_FH" used only once: possible typo at...

    Is the only way to get rid of the mssage to add

    #no warnings 'once';


    2) Create FILEHANDLE name from a variable

    my $logfile = "test.log"; my $log_fh = "LOG_FH"; LOG_MSG_OPEN($log_fh,$logfile); ... sub LOG_MSG_OPEN { my $par_fh = $_[0]; my $par_filepath = $_[1]; my $par_fh_2 = *${par_fh}; open($par_fh_2,"> $par_filepath") or die ("Can't open $par_filepat +h: $!\n"); $par_fh_2->autoflush(1); }

    gives me this error message: Can't use string ("LOG_FH") as a symbol ref while "strict refs" in use..

    Is it possible to create FILEHANDLEs with a variable

    Thanks for help.

    Regards, de Michi

XML::LibXML drives me to drinking
2 direct replies — Read more / Contribute
by tunafish
on Oct 22, 2016 at 19:37

    I'm trying to figure out XML::LibXML. It's rough going. I need to be able to access the text content of a node by name. Here is my code:

    #!/usr/bin/perl use strict; use XML::LibXML; my $string = qq~<?xml version="1.0"?> <ItemLookupResponse xmlns=" +ervice/2013-08-01"> <Items> <Item> <ASIN>B01KI4JSQY</ASIN> </Item> </Items> </ItemLookupResponse> ~; my $parser = XML::LibXML->new->load_xml(string => $string, {no_blanks +=> 1}); my $xml = XML::LibXML::XPathContext->new($parser); $xml->registerNs('x', ' +ce/2013-08-01'); # Parse items foreach my $item ($xml->findnodes('/x:ItemLookupResponse/x:Items/x:Ite +m', $parser)){ print $item->firstChild->nodeName, "\n"; print $item->firstChild->toString, "\n"; print $item->findvalue('ASIN'), "\n"; print $item->findvalue('./ASIN'), "\n"; print $item->findvalue('./ASIN', $item), "\n"; }

    Expected result:


    Actual result:


    Probably I'm just misunderstanding something in the docs. But I don't know what it is. I tried $item->findvalue('x:ASIN'), but that threw an error. Please help. I have a family. If I become an alcoholic, they will suffer.

Shortening paths for display
1 direct reply — Read more / Contribute
by morelenmir
on Oct 22, 2016 at 17:08

    There used to be an undocumented win32 function called 'PathSetDlgItemPath'. This allowed you to set the text of a windows control to a file or folder path, automatically editing what was displayed to fit inside the available client area. It did this where necessary by shortening the path using '...' ellipsis characters.

    I wondered if there was anything similar for perl? I guess, rather than the size of a windows control it would need to be told the maximum number of characters you wanted to display.

    "Aure Entuluva!" - Hurin Thalion at the Nirnaeth Arnoediad.
Question regarding web scraping
4 direct replies — Read more / Contribute
by Lisa1993
on Oct 22, 2016 at 09:52

    Hello everybody,

    I am very very new to using Perl (as in have only been using it intermittently for less than a couple of weeks), and I'm having a problem with a very basic web scraping programme.

    To cut a long story short, I am taking a university module where the instructor has told us to scrape comments from the website Reddit, so that we can analyse the comments using computational linguistics methods.

    He has given us the code below to use. The goal is to get the programme to search for every comment on the page and then to save the comments only in an html file.
    use LWP::Simple; $URL = ' +l_blake_is_released_today/'; $CONTENT = get($URL); while ($CONTENT =~ <div class=\"usertext-body may-blank-within md-cont +ainer \"><div class=\"md\">(.+?)<\/div> <\/div><\/form><ul class=\"flat-list buttons\"> //gs ) { $x = "$1"; $y = "$y $x"; } open(OUT, "">C:/Users/user/perl_tests/reddittest.txt"); print OUT "$y"; close(OUT);
    However, when I try to run the code (from my windows command centre) I get the following error message:
    Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\user>perl perl_tests/ Scalar found where operator expected at perl_tests/ line 12, ne +ar "$x = " $1" (Might be a runaway multi-line "" string starting on line 7) (Missing operator before $1?) String found where operator expected at perl_tests/ line 12, ne +ar "$y = " " (Missing semicolon on previous line?) Scalar found where operator expected at perl_tests/ line 12, ne +ar "$y $x" (Missing operator before $x?) String found where operator expected at perl_tests/ line 12, ne +ar "open(O UT, "" (Missing semicolon on previous line?) String found where operator expected at perl_tests/ line 15, ne +ar "open(O UT, "">C:/Users/user/perl_tests/reddittest.txt"" Can't modify numeric lt (<) in scalar assignment at perl_tests/ + line 12, near "$x = "$1" syntax error at perl_tests/ line 12, near "$x = "$1" Execution of perl_tests/ aborted due to compilation errors.
    I know that there is probably a very "basic" error in the code, but I'm too inexperienced as of yet to know how to fix it. Any help would be greatly appreciated. Thank you!
Adding a database table using DBIx::Class
1 direct reply — Read more / Contribute
by davies
on Oct 22, 2016 at 08:01

    tl;dr: Are there any docs that actually show a simple example of creating a table with DBIC, whether from SQL (which I know I know how to write) or a DBIC class module (which I think I know how to write)? If not, would any kind monk please give me such an example?

    TIA & Regards,

    John Davies

pattern matching from an array to an array of arrays
1 direct reply — Read more / Contribute
by Anonymous Monk
on Oct 22, 2016 at 07:03

    Hi everybody. Being relatively new to perl is am still having problems applying what I know to my needs. How do I impose a condition in an if cicle for matching the first word of a string part of an @AoA data structure with the first word of a string in another array? My goal is then to add the second word of the first string to the second string. Thank you all very much in advance

    my($other_array) = ''; foreach $other_array (@other_array){ foreach my$arr (@AoA){ foreach my$sca (@$arr){ $sca if (($sca =~ m/\w+/g)[0] eq ($uniq_kmers =~ m/\w+/g)[0]){ $uniq_kmers =~ "\t".($sca =~ m/\w+/g)[1]; }else{ $uniq_kmers =~ "\t0"; } } } }
-n and -p suck up ARGV no matter what
3 direct replies — Read more / Contribute
by mpersico
on Oct 21, 2016 at 16:31
    I have this line of code in a loop in shell:
    pid=$(ps -l -p $pid | grep -v PPID | perl -ane 'print $F[4]')
    The idea is to get the parent of the process. Other code is checking for other conditions. But that's not the problem. The problem is that $F[4] only works on some 'ps'. For the 'ps' in Cygwin, I need to use $F[1]. Sooooo, I naively tried this:
    ppidi=4 [ $(uname) = 'Cygwin ] && ppidi=1 ... pid=$(ps -l -p $pid | grep -v PPID | \ perl -ane 'print $F[$ARGV[0]]' $ppidi);
    which resulted in
    Can't open 1: No such file or directory.
    In short, once you use -n or -p, there is no way to use ARGV as arguments, and not have them sucked up as filenames to feed to the virtual while(<>) {} loop, is there?
Recommendations for perl modules to work on data sets ?
4 direct replies — Read more / Contribute
by raghuprasad241
on Oct 21, 2016 at 11:06
    Hello monks,

    I am trying to find modules for some advanced data mining projects in perl. In python they have iterator module with functions like pairwise etc. that does the task for me. However I need to do this perl. So trying to avoid reinventing the wheel. I am going to describe a sample problem so that it will give you an idea for any recommendations you may provide. I have a data set like below...

    10/01/2016 99.71 10/02/2016 99.53 10/03/2016 100.10 10/04/2016 100.96 10/05/2016 100.99 10/06/2016 101.38 10/07/2016 100.74 10/08/2016 100.70 10/09/2016 99.88 10/10/2016 97.62 10/11/2016 97.55 10/12/2016 99.12
    Now I need to find 5 consecutive records either increasing or decreasing by value next to date field. For e.g. I need the following 2 result sets in my output.
    10/02/2016 99.53 10/03/2016 100.10 10/04/2016 100.96 10/05/2016 100.99 10/06/2016 101.38 10/07/2016 100.74 10/08/2016 100.70 10/09/2016 99.88 10/10/2016 97.62 10/11/2016 97.55
    Please note that I am looking for exactly 5 consecutive records with either increasing or decreasing patterns, any other number of consecutive records with those patterns should not qualify.

    I know this is a little advanced for me to write from scratch given my perl experience. Appreciate any inputs on the modules that I may use to accomplish this task or atleast any ideas to write the code efficiently given there are high volumes of records in each file.

    Again I am not expecting one of you to write code for me although I would not complain about it :-)

    Thanks! Monk
How to concatenate utf8 safely?
1 direct reply — Read more / Contribute
by gregor42
on Oct 21, 2016 at 10:22

    I am humbled and seeking help.

    This concerns data containing names so getting it Right is important.

    It is likely that I am fundamentally missing something when it comes to safely concatenating strings.

    A hand-rolled point solution sometimes works as intended and others times results in the dreaded:

    Wide character in syswrite

    I assume that the problem is my code and not the data coming in since one can usually depend on people to get their own names right.. But then i18n characters are tricksy, like Hobbits...

    sub jibe { my($s,$t) = @_; my $r = join('', (is_utf8($s)?$s:decode('utf8',$s)), (is_utf8($t)?$t +:decode('utf8',$t))); return $r; }

    To give it context, let's say that we are creating common name from given name plus surname: (Anglo-centric, I know...)

    my $cn = jibe(jibe($givenname," "),$sn);

    Thank you in advance for any nudges in the right direction that anyone might provide.

    Wait! This isn't a Parachute, this is a Backpack!

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.
  • 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 scrutinizing the Monastery: (3)
    As of 2016-10-24 03:51 GMT
    Find Nodes?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?

      Results (302 votes). Check out past polls.