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
Using Perl instead of awk!
6 direct replies — Read more / Contribute
by Ragged Robin
on Sep 30, 2014 at 14:50

    Hey guys, I just need an example of how to express something in Perl the way I know how to do it with awk.

    Ideally I would like to do something like this:

    my $pSWP = `awk '/something/ { print $3 }' /dir/file`;

    But the value of $pSWP appears to be null when I can confirm that the awk command should return a value when ran on the system. Apparently doing something like this is kind of a no-no and I can do the same thing in Perl but I'm not sure what all is available to do this in only Perl :(
email through Exchange server using StrawberryPerl
1 direct reply — Read more / Contribute
by sgmansell
on Sep 30, 2014 at 14:44

    Hello Perl Monks

    I have multiple files I want to send to multiple emails. One file goes to one email recipient. I have StrawberryPerl 5.20.1.1 (64bit) on Windows 7 (64 bit). I have Mail::Sender installed. I copied the following code from, actually, I've looked at so many sites I forget where I copied it from. SMTP, To and From are real and will be used for my testing purposes.

    #!/usr/local/bin/perl -w use Mail::Sender; $sender = {}; if ($sender->MailMsg({ smtp => 'ECMAIL7.internal.lacoe.edu', from => 'mansell_steven@lacoe.edu', to =>'sgmansell@gmail.com', subject => 'this is a test', msg => "Hi Steve.\nHow are you?" }) < 0) { print "if must be less than 0\n"; die "$Mail::Sender::Error\n"; } print "Mail sent OK."

    I get this error

    Can't call method "MailMsg" on unblessed reference at email_code.pl line 5.

    I am not familiar with methods so I'm not sure what to do at this point.

    1. Is there a way to send email without using methods? or do I need to learn methods?

    2. Is there a way to test if I'm able to send an email through the Exchange server?

    Thanks,

    Steve

How to re-format a name field
3 direct replies — Read more / Contribute
by pbassnote
on Sep 30, 2014 at 14:17

    I am working with a csv file of several fields of data, but I only need help with re-formatting a name field. The name field is a variable-length, slash-separated (/) data that can be in any of these various formats:

    George Washington/GOV/VA/US John Adams/GOV/MA/US John Q Adams/GOV/MA/US John Q Adams Jr/GOV/MA/US John Q Adams JR/NonEmployee/GOV/MA/US Thomas Jefferson/GOV/VA/US

    As you can see, most of these lines have a name followed by three slash-separated bits of data, but some will have more. All I need from this is the name part of the data. Everything from the first "/", including the slash character isn't needed.

    With the name, I need to re-format into two fields for last name, and first name, to put into a csv file, so that the above data needs to come out as:

    Washington, George Adams, John Adams, John Q Adams Jr, John Q etc.

    I've considered using some kind of split() statement to separate the name field by the slashes, but I don't know how to do that? Also, how to reverse the names to display last name before first name?

    Thanks!

Finding node with attribute XML::LibXML
5 direct replies — Read more / Contribute
by jjap
on Sep 30, 2014 at 13:42
    Dear Monks,

    I am navigating a nested XML data structure and at some point, I want to retrieve the value of a tag having a specific attribute. I cannot figure out why I am not getting anything from the minimal reproducible below (return 0).

    use strict; use XML::LibXML; my $parser = XML::LibXML->new; my $doc = $parser->parse_file("minimal.xml"); my @vol = $doc->findnodes(q {Volume[@VolumeCategory="L"]}); # also tried various quoting scheme... my $tmp = scalar(@vol); print "Number of entries: $tmp \n"; # 0
    minimal.xml
    <root> <ProductKey>99</ProductKey> <Volume VolumeCategory="L" MeasurementCategory="Real">0.063</Volume> <Volume VolumeCategory="cuft" MeasurementCategory="Real">2.2</Volume> </root>
    Any hints would be greatly appreciated!

    Update: Thanks to both Ikegami and Toolic for great pointers.
    Also did some spelling edit.

    Update 2: It turns out my problem was related with namespace (in my large document, not the toy example I made for this post). As alluded to in numerous other postings, XML::LibXML::XPath offered the registerNs method which then made XPath expressions find everything I needed. Thanks to all for your inputs, guidance and alternate solutions.
pipe list open trouble?
1 direct reply — Read more / Contribute
by dk
on Sep 30, 2014 at 09:01
    Hello all,

    Can anybody explain why I'm seeing the difference in those pieces of code? I thought these should be identical:

    $ perl -e 'open F, @ARGV or die $!' '|-' echo 42 No such file or directory at -e line 1. $ perl -e 'open F, $ARGV[0],$ARGV[1],$ARGV[2] or die $!' '|-' echo 42 42
    Thank you!
timelocal error wired behaviour
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 30, 2014 at 02:44
    Hi Monks,

    I am getting a date input from a csv file, which i want to check each date with current date, and if older than 1 month, i can do some operation on it for that entry.

    I am not allowed to install extra libs so, i am not using Date::Calc and have to somehow stick to Time::Local.

    Here is the snippet of my code, and suggest where I am going wrong.

    Code :

    if (my ($inpmm, $inpdd, $inpyyyy) = @date[1] =~ '(\d+)/(\d+)/(\d+)') { { print "@date[1] : Has been split to $certyyyy, $certmm, $certdd"; my @certdate=(0, 1, 1, $certdd, $certmm-1, $certyyyy); print "---@certdate"; # Just to see how it gets formed my $inptime=timelocal_nocheck(@certdate); print "<td>$inptime</td>"; my $timediff=$inptime - $lmtime; print "$timediff"; if ($timediff <= 2628000) { <<Go with Job>> } else { <<Skip Job>> } } else { print "Not in Format" }

    Output

    08/12/2016 : Has been split to 2016, 08, 12---0 1 1 12 7 2016

    After this, it ubruptly ends, even when i have some 100 odd entries to go.

    Thanks a lot in advance

String::MkPasswd still supported?
5 direct replies — Read more / Contribute
by Skeeve
on Sep 30, 2014 at 01:25

    Recently I needed a function to generate passwords so instead of writing my own I did a CPAN search and found String::MkPasswd.

    Unfortunately the function simply increases the number of lower case letters if you don't give enough minimum characters of each class. Example: If you want at least 2 lower case letters and 2 upper case letters and 8 characters length, you always end up with 2 upper case and 6 lower case letters.

    I think this isn't too good so I created a pull request which will spread the missing 4 characters over all character classes.

    Now I'm wondering whether or not the module is still supported as there seems to be nothing happening.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Style in CGI Header
1 direct reply — Read more / Contribute
by Anonymous Monk
on Sep 29, 2014 at 23:42
    I have code that prints html header this way with CGI.pm:

    -style => {-code=> "$page_style", 'src'=>[$_css_path,$_css_path2]},#.. +.. I just copied that line out...

    this is just part of it that goes in this header:
    print start_html();

    My question is, if I have a page that I want to pull in a different CSS is there a way to put them all in a array instead of the way I did it, where the files are all printed into there so they are all pulled in:
    @whatever = ("$_css_path","$_css_path2"); -style => {-code=> "$page_style", 'src'=>@whatever}, #....
    Then if I have another path, say in this tag: $_css_path3, how would I add it? push?

    Please let me know how to make it work.
    Thank you. :)
    Ukn
Hash of subroutines as member of a class
3 direct replies — Read more / Contribute
by Hameed
on Sep 29, 2014 at 21:18
    Hi PerlMonks,

    It maybe a simple thing, but I seem to have hit a brick wall.

    I have a very simple class and in the constructor, I am creating a hash that is holding references to subs which are members of the same class as well.

    see below:
    package EOYStats; use strict; use warnings; use Data::Dumper; sub new { my $class = shift; my $this = bless {}, $class; $this->{ACT} = shift; %{ $this->{ACTIVITIES} } = ( 'Sale' => { 'func' => \&total_sold, 'datefield' => "date_sold", }, 'Purchae' => { 'func' => \&total_bought, 'datefield' => "date_bought", }, ); return $this; } sub get_stats { my $this = shift; my $arg = shift; $this->{ACTIVITIES}->{$this->{ACT}}->{'func'}->($arg); } sub total_sold { my $this = shift; my $arg = shift; $this->dosomething(); } sub total_bought { my $this = shift; my $arg = shift; # Do stuff.... } sub dosomething { my $this = shift; print "This is test...\n"; } return 1;
    Now, when I call total_sold from another method in this class (get_stats in this case), '$this' is undefined in total_sold and '$this->dosomething' returns an error 'Can't call method "dosomething" on an undefined value at EOYStats.pm line 37.'

    There is no real need to have these methods in a hash. I can simply write conditions in the get_stats and call the methods accordingly, but I wanted to try and see if this worked.

    so is it really ridiculously bad idea to do this or is it not possible or simply silly thing to do?

    Sorry if this is too confusing.

Running a perl script as a system command in another thread hangs while main thread reading from STDIN
1 direct reply — Read more / Contribute
by rmahin
on Sep 29, 2014 at 20:14

    Hi perlmonks, have run into a weird issue. In this program, the main thread is reading from STDIN, and other threads are issuing system commands in the background. The weird part is that it only seems to cause a problem when the system command is a perl command. Have tested this on Windows 7 and Windows 2012 R2.UPDATE 2: This affects all Windows 8, and Windows 2012 platforms that I have used.

    This is not the original code I found, but a simplified script that I hope adequately demonstrates the issue. If the script is run with input, it simply executes echo hello world, if it is given input it will run perl -e "print \"hello world\""

    My hunch would be that reading from STDIN is preventing perl from performing the fork, which would make sense if it had this problem with all system commands but it seems to be limited to perl, or something else that I am not seeing. My first thought was to close STDIN in the thread, but this didn't seem to make any difference. Hopefully one you has some idea of whats going on here :/

    This yields the following output

    Thanks for taking a look!

    UPDATE: Including perl -v output. This is the same for both 2012 systems that are affected by this issue. Other operating systems seem to be unaffected.

    C:\>perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x +64-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2013, Larry Wall Binary build 1801 [297964] provided by ActiveState http://www.ActiveSt +ate.com Built Feb 24 2014 11:06:47 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

    UPDATE 3 - Workaround: On a whim, I decided to try piping nul to the perl process. Surprisingly, it seems to work..

    C:\>test.pl 1 ### Running a perl script ### 0s : Thread 0 - > Creating threads 0s : Thread 1 - Created 0s : Thread 2 - Created 0s : Thread 3 - Created 1s : Thread 0 - All Threads created 1s : Thread 0 - > Waiting for input now. All system commands should fi +nish without pressing enter 3s : Thread 1 - Starting 'perl -e "print \"hello world\"" < nul' 3s : Thread 3 - Starting 'perl -e "print \"hello world\"" < nul' 3s : Thread 2 - Starting 'perl -e "print \"hello world\"" < nul' 3s : Thread 3 - Finished the command: hello world 3s : Thread 1 - Finished the command: hello world 3s : Thread 2 - Finished the command: hello world 7s : Thread 0 - > Input received, all done 7s : Thread 0 - All Threads joined

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 taking refuge in the Monastery: (4)
    As of 2014-10-01 23:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (40 votes), past polls