Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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
NET::Foreign Commands
1 direct reply — Read more / Contribute
by moz
on Jan 28, 2015 at 10:55
    Hi I am new to Perl and using SFTP in general. I am being required to use the NET::Foreign package as a means of file transfer by a project at work. My problem is that I am unable to issue other ssh commands like cmd() to perform other ssh tasks without opening up a system call to open a ssh session. This greatly slows things down in my application. I would like to open one connection using NET::Foreign and keep it open to be able to issue ssh commands and sftp commands. Is this possible? And how would I do it?
one liner to capture value of field and insert in another field
2 direct replies — Read more / Contribute
by dirtdog
on Jan 28, 2015 at 10:19

    Hi Monks, I'm struggling with the task of capturing a date value that is populated in column 8 of a pipe delimited file and inserting it into column 11.

    row|1|CUSIP|12345678|RED|||20150125|||| row|1|CUSIP|1234A82H|RED|||20150316||||

    for the 2 examples above the final result should be:

    row|1|CUSIP|12345678|RED|||20150125|||20150125| row|1|CUSIP|1234A82H|RED|||20150316|||20150316|

    I know i need to use memory parens to capture the value in column 8, but can't seem to get it to work

    perl -pi -e 's/\(RED|||([0-9]{8})|||\)/\1/g'

    The above is what i have so far.

    Thanks for any suggestions

How can I access object data in a user defined sort cmp function?
4 direct replies — Read more / Contribute
by tkguifan
on Jan 28, 2015 at 09:19
    I want to sort the keys of a hash stored within an object. If I create a user defined cmp function for my sort, it only gets the values $a and $b. Within this cmp function how do I access the $self of the object?
    # object $self={ hash_ref=>{item1=>'value1',item2=>'value2'} } # cmp funtion sub byvalue { #instead of $a<=>$b; #something like $self->{hash_ref}->{$a}<=>$self->{hash_ref}->{$b}; #how do I access $self in this function? } # object's sort function sub sort_hash_ref_keys_by_value { my $self=shift; my @keys=sort byvalue keys(%{$self->{hash_ref}}); }
Get fullpath of file using grep
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 28, 2015 at 09:12
    Having the following line I get just the filename. How can I modify it to get all path including the file name?
    my $directory; @files = grep {-f "$directory/$_" && /\.dat$/i} readdir DIR;

    I would like to get: c:\temp\file1.dat

    Right now I just get: file1.dat

Sanity Check
1 direct reply — Read more / Contribute
by kcinmd
on Jan 28, 2015 at 07:24

    I am about to implement a preprocessing one liner to remove Invalid ascii character representations which fall out of the range of our decoding ability. Our data feed is a drop directory of XML that ultimately is loaded into data warehouse. Prior to load I will have the ETL issue the following pre-command:

    perl -pi -e 's/^&#x.+;//g' ./*.xml

    Testing has proven desired result. I figured I would throw this out there for opinion just in case I am missing something.

How to handle the error when we try to read an empty text file in perl
4 direct replies — Read more / Contribute
by ppp
on Jan 28, 2015 at 02:39

    I am a perl beginner and under a situation where i have written a script and i am trying to read a text file to do check if $address exist in the file(see in my code) through the perl script and my code to so this is written below :

    open my $fh, '<', 'C:\ppp.txt', or die "Could not open file $!"; while (my $line = <$fh>) { foreach my $str ($line) { if (index($line, $address) != -1) { $counter++; $flag='1'; } } }

    Where is the problem ? The problem is when i try to read a file which is empty then it gives error(because in my code i see at each line that does this $address exist in that line. If it exist then i return the flag=1 and if not then i return flag=0. The code gives error that could not open file at line(open my $fh, '<', 'C:\ppp.txt', or die "Could not open file $!";)it ONLY HAPPENS when file is empty (i mean when there is no string to compare $address in text file) and IT WORKS FINELY when there IS ATLEAST ONE string written to do comparison with $address. I have verified it and it means there is no read write problems. If it was a c++ code then i would have done something like this :

    open my $fh, '<', 'C:\ppp.txt', or die "Could not open file $!"; if ($fh contains an empty file) { // then add this the $address in the ppp.txt's first line }

    I mean if file is empty then it should add the $address in ppp.txt's first line otherwise go to the while loop and compare if the existing address in text file does match with $address or not and return flag=1 if it does?(which my while loop already do) How to do it in perl ?

use sqlldr with perl
4 direct replies — Read more / Contribute
by gary16
on Jan 28, 2015 at 01:41

    hi,everybody I m from China.And I now have a strange problem.This day I have imported the oracle data with sqlldr. AND I used the system() to call the sqlldr.The data text is encoded by GB2312;The title of the data text also is .When I run the perl script,The perl create a new data text encoded by utf8 and replace the old one.Here is my code.Have anybody figured out what is happened??

    #!/usr/bin/perl -w use strict; use Net::FTP; use Date::Calc qw(:all); use Encode qw(:all); $filename="&#27979;&#35797;&#25991;&#26412;"; if($filename){ system(" sqlldr test1/123456 data=$filename control=importwz.ctl"); + }
reg expression question
3 direct replies — Read more / Contribute
by healingtao
on Jan 28, 2015 at 01:21
    Hi, I need to substitute from one string to another in perl: Here are 4 examples that handle all my use cases: 1) From: 'GNRABS 2014-186' To: 'GNRABS14-186' 2) From: 'A10 2013-1' To: 'A1013-1' 3) From: 'CGBAM 2014-HD' To: 'CGBAM14-HD' 4) From: 'FHMS K032' To: 'FHMS-K032' Description: 1) The first part (before space) is just any number of characters and I need to use it as is 2) Second part starts after space and is the year I need to strip from 4 chars to 2 (as per first 3 examples) . Or skip if it doesn’t exist like example 4 3) The third part is what is remaining (including dash like all first 3 examples), but you always need a dash even if it doesn’t exist in the original string like examples 4 Can you recommend what code I need to use for this type of conversion from one string to another
Trouble Installing WWW::Mechanize::Firefox module
1 direct reply — Read more / Contribute
by Varsha307
on Jan 27, 2015 at 21:42

    Hi, I am quite new to Perl. I am trying to automate a search in a webpage that contains Javascript. After some research I decided to install the WWW::Mechanize::Firefox module. I am using a Windows 8 machine and installed ActivePerl on it. I downloaded the module and tried to install it. I have installed all the dependencies and even tested out the MozRepl module by writing a small script. Unfortunately the installation still fails. I think the installation is looking for Javascript connection, but Javascript is active in my Firefox, since that is the default and I have not changed it.

    I get the following error constantly during the installation -> "Subroutine-MozRepl-Load Plugins redefined at line 104." and the final tests fail. I am not sure how to handle this or to correct the Javascript option in firefox. Any help is much appreciated. Thanks !

[SOAP::Lite] Obtain request body before request is sent?
1 direct reply — Read more / Contribute
by three18ti
on Jan 27, 2015 at 21:32

    Hello Monks

    I am working with a 3rd party API and trying to use SOAP::Lite for the first time.

    I've figured out how to actually make the request but to use the secure version I essentially need to send an sha1 hash of the message body in a header.

    What I've got so far:

    #!/usr/bin/env perl use strict; use warnings; use 5.010; use LWP::UserAgent; use SOAP::Lite; my $client = SOAP::Lite->proxy($proxy) ->ns($namespace, 'foo') # I'm not sure I understand this. ->uri($uri) ->on_action(sub { sprintf '%s', $_[0] }) ->on_fault(sub { my($soap, $result) = @_; die ref $result ? "Fault Code: " . $result->faultcode . "\n" . "Fault String: " . $result->faultstring . "\n" : $soap->transport->status, "\n"; }); my $params = { foo => 'bar', biz => 'baz'}; my $result = $client->my_api_method($params);

    Essentially, after the xml is generated and before the request is made I need to insert a sha1 hash of the request body. I'm looking at the correct version of the docs now (was looking at 0.55 for some reason...) so hopefully there's something obvious that jumps out.

    EDIT: I think I can use SOAP::Data->name($params) somehow...

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!
  • 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?

    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 meditating upon the Monastery: (14)
    As of 2015-01-28 17:53 GMT
    Find Nodes?
      Voting Booth?

      My top resolution in 2015 is:

      Results (221 votes), past polls