Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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 to find all the elements in a circular linked list
No replies — Read more | Post response
by punitpawar
on Feb 09, 2016 at 18:55
    Hello , I wanted to know if there is a way in perl to iterate a circular linked list and capture all the elements in it .
    so if I have a list like 2->4->5->4->6->2->7->8->2 , where after 8 it points back to the head 2 . Is thee a way in which I can iterate through this list and push the elements in an array ?
    I tried something like this below , but this does not seem to work. Any advise on how this can be solved ?
    Consider that the each node in the linked list is having the following data structure
    $head ={ data=>2 next=>undef; ## or the next node to which it is linked to }
    my @elements; my $curr=$head; ## I am trying to make my curr pointer start from t +he head while (1){ last unless ($curr->{next} == $head); # here I am trying to +check that the next element is not head. And if is then break out of +the loop print "data : $curr->{data} \n"; push(@elements,$curr->{data}); $curr = $curr->{next}; }
Accumulate values for each data field
2 direct replies — Read more / Contribute
by JobC
on Feb 09, 2016 at 17:03

    I have some CSV data like this:

    I want to accumulate the values of textbox95 for each value of textbox85.
    This would give a value of 6 to textbox95 when textbox85 eq "456.321.07" for record 1
    It should also eliminate all other records that contain "456.321.07" in textbox85

    Currently, I am reading this in line by line.
    Modifying the data fields as needed.
    I write to a new file the format that can be used at the next step.
    This strips the last two data fields, which help make the lines unique.
    However there is a sequence number in the first field that will be "skipping" numbers after records are removed.

How to encode apostrophe and quote using XML::Writer?
1 direct reply — Read more / Contribute
by bcurrens
on Feb 09, 2016 at 16:32

    (<, >, &) are encoded by XML::Writer, however (', ") are not. I need to encode both (', ") and I suspect my current solution is not very good. I have essentially made a placeholder for each character, and, after XML::Writer has rendered the XML I'm running a global search replace so I end of with the desired output.

    I should add that if I encode these characters before outputting with XML::Writer I get:

    <root> <str1>one&lt;two</str1> <str2>one&amp;two</str2> <str3>two&gt;one</str3> <str4>Caisse D&amp;quot;Eparge</str4> <str5>Caisse D&amp;apos;Eparge</str5> </root>

    The desired output is:

    <root> <str1>one&lt;two</str1> <str2>one&amp;two</str2> <str3>two&gt;one</str3> <str4>Caisse D&quot;Eparge</str4> <str5>Caisse D&apos;Eparge</str5> </root>

    Program

    use strict; use warnings; use Cwd; use XML::Writer; use XML::Writer::String; use HTML::Entities; use utf8; use Modern::Perl; my $str1 = qq(one<two); # &lt; (<) my $str2 = qq(one&two); # &amp; (&) my $str3 = qq(two>one); # &gt; (>) my $str4 = qq(Caisse D"Eparge); # &quot; ("); my $str5 = qq(Caisse D'Eparge); # &apos; ('); $str4 =~ s/\x{22}/\#\#\#doublequote\#\#\#/g; $str5 =~ s/\x{27}/\#\#\#apostrophe\#\#\#/g; say "str4: $str4"; say "str5: $str5"; my $file = cwd() . '/test.xml'; my $BOD = XML::Writer::String->new(); my $Writer = XML::Writer->new( OUTPUT => $BOD, DATA_MODE => 1, DATA_INDENT => 2 ); $Writer->xmlDecl("UTF-8"); $Writer->startTag('root'); $Writer->dataElement('str1', $str1); $Writer->dataElement('str2', $str2); $Writer->dataElement('str3', $str3); $Writer->dataElement('str4', $str4); $Writer->dataElement('str5', $str5); $Writer->endTag('root'); $Writer->end(); my $xml = $BOD->value(); $xml =~ s/\#\#\#doublequote\#\#\#/\&quot\;/g; $xml =~ s/\#\#\#apostrophe\#\#\#/\&apos\;/g; my $fh = new IO::File "> $file"; if (defined $fh) { print $fh "$xml"; $fh->close; } exit;
[OT] Software Architecture - Pipe and Filter
3 direct replies — Read more / Contribute
by trippledubs
on Feb 09, 2016 at 15:40

    Hi Monks,

    Is "Pipe and Filter" a real thing? We are learning about it in school, but I have my doubts. It is mostly hand wavy and any questions I ask are usually answered by 'depends on implementation'. The concept seems sufficiently vague that it could mean just chaining a bunch of Unix operations together using proc1(stdout) -> stdin(proc2) OR a legitimate software pattern in use today that achieves some higher degree of concurrency OR anything else OR nothing.

    The assignment leads me down the road of Java Spliterators and/or streams, but I am wondering if there is a Perl equivalent.

    I think it is originally from this 3.5 star 2 cent book Software Architecture: Perspectives on an Emerging Discipline. ISBN 9780131829572

Populating a html template problem
1 direct reply — Read more / Contribute
by mavericknik
on Feb 09, 2016 at 14:09
    Hello monks, I'm very new to HTML template and am trying to output a table from a hash. Simplified code:
    my %trial_hash = ( ABC => "john", DEF => "mike", );
    My template is like so:
    my $tmpl = HTML::Template->new(scalarref => \ <<EO_TMPL <!DOCTYPE HTML> <html><head><title>Table</title></head> <body> <table><thead> <tr><TMPL_LOOP TH><th><TMPL_VAR CELL></th></TMPL_LOOP></tr> </thead> <tbody><TMPL_LOOP TD><td><TMPL_VAR CELL></td></TMPL_LOOP> </tbody></table></body></html> EO_TMPL );
    And the code I'm trying to use to populate is:
    $tmpl->param( TH => [ map { CELL => $_ }, qw( Type Value ) ], TD => [ map { CELL => $_ }, (each %trial_hash), ], );
    The output is
    <!DOCTYPE HTML> <html><head><title>Table</title></head> <body> <table><thead> <tr><th>Type</th><th>Value</th></tr> </thead> <tbody><td>ABC</td><td>john</td> </tbody></table></body></html>
    So it's only using one value from the trial hash. I assume that the problem is I need another loop to go through each key value pair of the hashes. So if I put in another loop for the rows:
    <tbody><TMPL_LOOP TR><tr> <TMPL_LOOP TD><td><TMPL_VAR CELL></td></TMPL_LOOP> </tr></TMPL_LOOP> </tbody></table></body></html>
    then im sure this way to populate:
    TR => [ { TD => [ map { CELL => $_ }, (each %trial_hash) ] }, { TD => [ map { CELL => $_ }, (each %trial_hash) ] }, ],
    Is completely wrong! Also, I dont know how many key/val pairs are going to be in the hash. How would I go about populating this? The data structures required to populate templates are really confusing me. I've been reading guides but cant seem to get it into my head. Any pointers are greatly appreciated!
Can't install PDF::API2 on Windows 10
2 direct replies — Read more / Contribute
by apadamson
on Feb 09, 2016 at 13:43

    Hello, I have a new computer with Windows 10. I have installed Strawberry Perl 5.22.1.2 with no problem and want to install PDF::API2 which I use in a program I wrote. However, I always get the same error. I use the command cpan PDF::API2 in a DOS window running as administrator.

    C:\Windows\system32>cpan PDF::API2 Loading internal null logger. Install Log::Log4perl for logging messag +es CPAN: CPAN::SQLite loaded ok (v0.211) Database was generated on Tue, 19 Jan 2016 18:03:37 GMT Running install for module 'PDF::API2' CPAN: Digest::SHA loaded ok (v5.95) CPAN: Compress::Zlib loaded ok (v2.069) Checksum for C:\STRAWB~1\cpan\sources\authors\id\S\SS\SSIMMS\PDF-API2- +2.025.tar.gz ok CPAN: Archive::Tar loaded ok (v2.04) CPAN: File::Temp loaded ok (v0.2304) Couldn't move C:\STRAWB~1\cpan\build\tmp-9636\PDF-API2-2.025\lib to C: +\STRAWB~1\cpan\build\PDF-API2-2.025-JGJwQD\lib: No such file or direc +tory at C:\Strawberry\perl\lib/CPAN/Distribution.pm line 571. CPAN::Distribution::run_preps_on_packagedir(CPAN::Distribution +=HASH(0x3843644)) called at C:\Strawberry\perl\lib/CPAN/Distribution. +pm line 388 CPAN::Distribution::get(CPAN::Distribution=HASH(0x3843644)) ca +lled at C:\Strawberry\perl\lib/CPAN/Distribution.pm line 1847 CPAN::Distribution::prepare(CPAN::Distribution=HASH(0x3843644) +) called at C:\Strawberry\perl\lib/CPAN/Distribution.pm line 2127 CPAN::Distribution::make(CPAN::Distribution=HASH(0x3843644)) c +alled at C:\Strawberry\perl\lib/CPAN/Distribution.pm line 3485 CPAN::Distribution::test(CPAN::Distribution=HASH(0x3843644)) c +alled at C:\Strawberry\perl\lib/CPAN/Distribution.pm line 3891 CPAN::Distribution::install(CPAN::Distribution=HASH(0x3843644) +) called at C:\Strawberry\perl\lib/CPAN/Module.pm line 479 eval {...} called at C:\Strawberry\perl\lib/CPAN/Module.pm lin +e 478 CPAN::Module::rematein(CPAN::Module=HASH(0x38433ec), "install" +) called at C:\Strawberry\perl\lib/CPAN/Module.pm line 587 CPAN::Module::install(CPAN::Module=HASH(0x38433ec)) called at +C:\Strawberry\perl\lib/CPAN/Shell.pm line 1896 CPAN::Shell::rematein("CPAN::Shell", "install", "PDF::API2") c +alled at C:\Strawberry\perl\lib/CPAN/Shell.pm line 2063 CPAN::Shell::__ANON__("CPAN::Shell", "PDF::API2") called at C: +/Strawberry/perl/lib/App/Cpan.pm line 592 App::Cpan::__ANON__("PDF::API2") called at C:/Strawberry/perl/ +lib/App/Cpan.pm line 601 App::Cpan::_default(ARRAY(0xa7784c), HASH(0x32e5cec)) called a +t C:/Strawberry/perl/lib/App/Cpan.pm line 516 App::Cpan::run("App::Cpan", "PDF::API2") called at C:\Strawber +ry\perl\bin/cpan.bat line 25

    The source directory exists but the destination directory doesn't. It exists upto \lib. I have installed this module on several versions of Windows without problem. Should I downgrade the version of Perl? Has anyone else had this problem?

Building a URL Query String from a Data Structure
3 direct replies — Read more / Contribute
by dorko
on Feb 09, 2016 at 12:44
    Hello,

    I'm looking to turn a Perl data structure into a CGI query string.

    There are several modules that turn query strings into data structures (eg CGI::Struct and CGI::State), but I need to do the opposite.

    I've got something like this:

    %hash = ( 'name' => 'test name', 'file_ids' => [ 1, 2 ], 'sub' => { 'name' => 'foo', 'message' => 'bar' }, );

    And I want to turn it into something like this:

    name=test+name&file_ids[]=1&file_ids[]=2&sub[name]=foo&sub[message]=bar

    Even "data structure ==> JSON ==> query string" would be ok too.

    I'm not finding any modules that do what I'm looking for. Does something like this exist?

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Cant find modules after upgrade
3 direct replies — Read more / Contribute
by cbtshare
on Feb 09, 2016 at 11:16

    I recently installed installed perl (v5.23.3) , now my old scripts wont work because it complains about modules, but those modules were installed and if I try reinstalling it says

    cpan -i Net::OpenSSH CPAN: Storable loaded ok (v2.51) Reading '/root/.cpan/Metadata' Database was generated on Tue, 09 Feb 2016 14:53:50 GMT CPAN: Module::CoreList loaded ok (v5.20160120) Net::OpenSSH is up to date (0.70). #>perl install_web.pl Can't locate Net/OpenSSH.pm in @INC (you may need to install the Net:: +OpenSSH module) (@INC contains: /usr/local/perl-5.23.3/lib/site_perl/ +5.23.3/x86_64-linux /usr/local/perl-5.23.3/lib/site_perl/5.23.3 /usr/ +local/perl-5.23.3/lib/5.23.3/x86_64-linux /usr/local/perl-5.23.3/lib/ +5.23.3 .) at install_web.pl line 5. BEGIN failed--compilation aborted at install_web.pl line 5.

    How do I install the modules in the new location or have the metadata read in the new location? thank you

Unable to establish SMB2 connection using Filesys::SmbClient
No replies — Read more | Post response
by Netras
on Feb 09, 2016 at 08:05

    Hello Perl Monks,

    I need to access a SMB host which requires SMB version 2. Filesys::SmbClient (3.2) has served me well working with SMB1 but does not successfully create a SMB2 connection. This is the error I get:

    samba_tevent: EPOLL_CTL_DEL EBADF for fde[0x27c9930] mpx_fde[(nil)] fd[8] - disabling

    Directly using smbclient with SMB version 2 works fine:

    smbclient -U domain\\user //HOSTNAME/ShareName -c "dir" -m SMB2

    But when omitting the max-protocol tag (-m) while using smbclient, a very similar error appears:

    smbclient -U domain\\user //HOSTNAME/ShareName -c "dir" samba_tevent: EPOLL_CTL_DEL EBADF for fde[0x7f454d1eff50] mpx_fde[(nil +)] fd[7] - disabling

    I assume that Filesys::SmbClient is trying to use SMB version 1 to connect to the target host and I have no idea how I can change this behaviour. I have checked the official module documentation with no success and tried forcing the client protocol version in /etc/samba/smb.conf but this has no effect on Filesys::SmbClient as well.

    Is there someone that has had this issue or has any ideas? I would appreciate any feedback and/or help in the matter.

    Thank you!

socket checker in multiple hosts for multiple destination
1 direct reply — Read more / Contribute
by Bams
on Feb 09, 2016 at 06:45

    Hi, I am trying to write a program in perl. it is to check socket from my local machine to multiple remote machines, to multiple destinations. The limitation i have is, on my local machine i only have standard modules installed. I am using expect to skip password as well 1. source hosts list abc.com def.com ghi.com 2. destination list contains host and port as a cvs file 123.com,1586 456.com,1234 789.com,5678 these files should be passed as argument , my program is still incomplete without expect. any help would be greatly appreciated

    #!/usr/bin/perl -w use Data::Dumper; use Expect; use strict; use warnings; my $exp = new Expect; $exp->log_file("SSHLOGFILE.txt"); $exp->log_stdout(0); my $user = 'xxxxxx'; my $pw = 'yyyyyyyy'; if( ! defined $ARGV[0] ) { print "Usage: new.pl source destination\n"; exit; } my $file1 = $ARGV[0] or die "Need to get CSV file on the command line\ +n"; my $file2 = $ARGV[1] or die "Need to get CSV file on the command line\ +n"; open my $info, $file1 or die "Could not open $file1: $!"; while( my $line = <$info>) { print $line; open(SSH,"/usr/bin/ssh xxxxxx\@host ps aux |") or die "$!\n"; open my $data, $file2 or die "Could not open $file2: $!"; for (my $nice = <$data>) { chomp ($nice); my ($servername, $portnumber) = split (',', $nice); print "$servername \n"; print "$portnumber \n"; while (<SSH>) { printf "%s %s\n", $servername, $portnumber; use IO::Socket; my $sock = new IO::Socket::INET (PeerAddr => $servername,P +eerPort => $portnumber,Proto => 'tcp'); print "ERROR: Could not create socket: $!\n" unless $sock +; printf "%s\n", "SUCCESS: Port Responded" if $sock; close($sock) if $sock; } close SSH; exit; redo; } close $data; } close $info; exit

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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2016-02-10 00:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?





      Results (329 votes), past polls