Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
perldoc of s///ee wrong or just misleading?
3 direct replies — Read more / Contribute
by LanX
on Dec 28, 2014 at 10:40

    according to perlop#s/PATTERN/REPLACEMENT/ a double eval isn't a double eval but the eval of an interpolation

    e Evaluate the right side as an expression. ee Evaluate the right side as a string then eval the result.

    At least that's how I understand the distinction between "evaluate as an expression" and "evaluate as a string"...

    Problem is that I can't see this distinction, ee produces just

    eval ( eval 'RHS' )

    eval do { RHS }

    for me and not

    eval "RHS".

    Or what is meant here, if "evaluate as a string" isn't supposed to mean "string resp. var interpolation", what else?

    some snippets as demonstration

    DB<135> $_='x' => "x" DB<136> $a='$b'; $b="B" => "B" DB<137> s/x/$a.$a/r # -> "$a.$a" (interpolation) => "\$b.\$b" DB<138> s/x/$a.$a/re # -> eval '$a.$a' => "\$b\$b" DB<139> s/x/$a.$a/ree # -> eval "\$b\$b" not eval "\$b.\$b" Scalar found where operator expected at (eval 106)[(eval 105)[multi_pe]:2] line 1, near "$b$b" ...


    DB<140> s/x/$a.'.'.$a/ree # -> eval "\$b.\$b" => "BB" DB<141> s/x/$a.'.'.$a/re # -> eval '$a.\'.\'.$a' => "\$b.\$b" DB<142> s/x/$a.'.'.$a/r # -> "$a.'.'.$a" => "\$b.'.'.\$b" DB<143> eval "\$b.'.'.\$b" # what perldoc predicts for line 140 => "B.B"

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    ) updated: the RHS is precompiled, but eval 'RHS' means runtime compilation

how to move the folder from one directory to another directory?
1 direct reply — Read more / Contribute
by vasuperl
on Dec 27, 2014 at 22:56

    I have a folder with the name of session in the path of desktop/tma-log/loggeddata. Now I want to move the session folder into some other directory. How to move the folder into some other directory using Perl? Can any of you give some inputs how to do this?

Perl Moose syntax
2 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 27, 2014 at 21:53

    Why Moose uses syntax like this:

    has 'friends' => ( is => 'rw', isa => 'Array', default => () );

    instead of using this syntax

    has 'friends' => [ is => 'rw', isa => 'Array', default => () ];

    I think the latter syntax does make more sense

In place search and replace with a hash
4 direct replies — Read more / Contribute
by hkates
on Dec 27, 2014 at 20:49

    I want to replace a set of strings across multiple files. Ideally, I want to change the files in place. I have a hash where the keys are the strings to find, and the values are the strings to replace them with.

    So something like s/$key/$hash{$key}/g. Below is my code to build the hash and to search files for the hash keys and replace them with the associated hash values. The errors are included at the end. I ensured that the hash is working, and I have tested that my regex is correct as well.

    open my $fh, '<', 'hash.txt' or die "Can't open file $!"; LINE: while (my $line = <$fh>) { my ($key, $value) = split /\s/, $line; next LINE if not $key; $hash{$key} = $value; chomp (%hash); } close $fh; @file_array =<*.txt> or die $!; foreach $file (@file_array) { open FH, "$file", or die $!; while (<FH>) { if (/(\S+):(\S+).*\n/) { s/$1/$hash{'$1'}_$2/g; } } close FH; }

    The s/foo/bar/g itself is not working (I tried s/$1/$2/g), and I have never done a find and replace in place before. I am getting two "use of uninitialized value" errors:

    Use of uninitialized value $hash{"$1"} in concatenation (.) or string at line 29, <FH> line 5.

    Use of uninitialized value $2 in substitution iterator at line 30, <FH> line 5.

    I get these errors once for every line in FH where the search pattern is found.

    Thanks for reading!

how to execute multiple Perl scripts
4 direct replies — Read more / Contribute
by vasuperl
on Dec 27, 2014 at 13:36

    I have a small doubt in Perl? can any one do your favour? I have 4 Perl scripts like,,, I want to execute those 4 scripts consecutively such that the master script should exit if there is any error in any of the script...for example if there is an error in, then it should not execute, it should exit from the script.. if any of you had any idea how to do this, please help me.. I am a beginner of Perl.. please do needful.

READ one line at a time
3 direct replies — Read more / Contribute
by wrkrbeee
on Dec 27, 2014 at 10:27

    Hello PerlMonks, Inherited a program that creates the "Out of Memory" error. Modified the code to read one line at a time rather than store the entire file in a variable (or so I thought). Program runs/works, but still encounters the "Out of Memory" error (obviously I failed). I suspect that the problem lies with a foreach statement at line 59. I'm thinking that the notion of reading all files into an array is unnecessary. Hence, lines 51-59 can be dropped. However, my dilemma now is how do I assign the filename to $file in lines 63 and 86? Apologize for such a trivial question to experienced users. Program is below. I am grateful for any insight you may have. Thanks!!

    #!/usr/bin/perl -w #use strict; # This program extracts data from an SEC filing, including chunks of t +ext use File::stat; #This program is going to obtain and extract the entire audit opinion +but you can #extract whatever text you are interested in by changing the regular e +xpressions for the start #and end strings below. #This program was written by Andy Leone, May 15 2007 and updated July + 25, 2008. #You are free to use this program for your own use. My only request i +s #that you make an acknowledgement in any research manuscripts that #benefit from the program. my $startstring='((^\s*?)((We\s*(have|were)\s*(audited\s*the\s*(Statem +ent\s*of\s*Financial\s*Condition|consolidated|accompanying|combined|b +alance\s*sheets)|(completed\s*|engaged\s*to\s*perform)\s*an\s*integra +ted\s*audit))|In\s*our\s*opinion,\s*the\s*(consolidated|accompanying) +))'; my $startstringhtm='(((We\s*(have|were)\s*(audited\s*the\s*(Statement\ +s*of\s*Financial\s*Condition|consolidated|accompanying|combined|balan +ce\s*sheets)|(completed\s*|engaged\s*to\s*perform)\s*an\s*integrated\ +s*audit))|In\s*our\s*opinion,\s*the\s*(consolidated|accompanying)))'; + #Specify the end of the text you are looking for. my $endstring='((^\s*)/s/|^\s*(Date:\s*)?(\d{1,2}\s*)?((January|Februa +ry|March|April|May|June|July|August|September|October|November|Decemb +er))\s*(\d{1,2},)?\s*\d{4}(\s*$|,\s{0,3}except|\s*/s|\s*s/|\d{1,2}))' +; my $endstringhtm='((>|^\s*)(/s/)?\s*(Date:\s*)?(\d{1,2}\s*)?(January|F +ebruary|March|April|May|June|July|August|September|October|November|D +ecember))\s*(&\w+?;\s*)?(\d{1,2},)?\s*\d{4}(\s*$|\s*[,\(]\s{0,3}excep +t|\s*with\s*respect\s*to\s*our\s*opinion|<\/P>|<BR>|\s{0,1}\<\/FONT\> +|\d{1,2})'; #Specify the directory containing the files that you want to read my $direct="E:\\Research\\SEC filings 10K and 10Q\\Data\\Filing Docs\\ +2008test"; my $outfile="E:\\Research\\SEC filings 10K and 10Q\\Data\\Header Data\ +\Data2008test.txt"; #If Windows "\\", if Mac "/"; my $slash='\\'; $outfiler=">$outfile"; open(OUTPUT, "$outfiler") || die "file for 2006 1: $!"; #The following two steps open the directory containing the files you p +lan to read #and then stores the name of each file in an array called @New1. opendir(DIR1,"$direct")||die "Can't open directory"; my @New1=readdir(DIR1); #We will now loop through each file. THe file names #have been stored in the array called @New1; foreach $file(@New1) { #This prevents me from reading the first two entries in a directory . +and ..; if ($file=~/^\./){next;} #Initialize the variable names. my $cik=-99; my $form_type=""; my $report_date=-99; my $file_date=-99; my $name=""; my $sic=-99; my $HTML=0; my $Audit_Opinion="Not Found"; my $Going_Concern=0; my $ao="Not Found"; my $tree="Empty"; my $data=""; #Open the file and put the file in variable called $data #$data will contain the entire filing { # this step removes the default end of line character (\n) # so the the entire file can be read in at once. local $/; #read the contents into data open (INPUT, "$direct$slash"."$file"); while ($data=<INPUT> ) { #The following steps obtain basic data from the filings if ($data=~m/<HTML>/i){$HTML=1;} if($data=~m/^\s*CENTRAL\s*INDEX\s*KEY:\s*(\d*)/m){$cik=$1;} if($data=~m/^\s*FORM\s*TYPE:\s*(.*$)/m){$form_type=$1;} if($data=~m/^\s*CONFORMED\s*PERIOD\s*OF\s*REPORT:\s*(\d*)/m){$report +_date=$1;} if($data=~m/^\s*FILED\s*AS\s*OF\s*DATE:\s*(\d*)/m){$file_date=$1;} if($data=~m/^\s*COMPANY\s*CONFORMED\s*NAME:\s*(.*$)/m){$name=$1;} if($data=~m/^\s*STANDARD\s*INDUSTRIAL\s*CLASSIFICATION:.*?\[(\d{4})/ +m){$sic=$1;} my $filesize = -s $direct . '/' . $file; my $sb = stat($direct . '/' . $file)->size; print OUTPUT "$cik,$form_type,$report_date,$file_date,$name,$sic,$file +size,$sb\n"; } close INPUT or die "cannot close $file: $!"; } }
Question on Regular Expression
6 direct replies — Read more / Contribute
by sjain
on Dec 26, 2014 at 19:07

    Hi everyone, I have a question related to regular expression and below are 2 code snippets. Can you please advise why they give diffferent output? What is wrong with code snippet 2 ?

    Code Snippet 1

    #!/usr/bin/perl use re qw(eval); my $pattern1; my $str1 = "RS"; $pattern1 = qr{(?<P_ROOTCODE>.*)(?{ push(@rc1, ${^MATCH}) })\K(?{ $p_r +ootcode = "@rc1" })(?<DAY1>[0-9])(?{ push(@rc2, ${^MATCH}) })(?<P_MON +_CODE>[A-Z])(?{ push(@rc3, ${^MATCH}) })$}; $str1 =~ m/$pattern1/; print "Value of p_rootcode in Pattern 1 is : $p_rootcode\n";

    Code Snippet 2

    #!/usr/bin/perl use re qw(eval); my $pattern2; my $str2 = "RS"; $pattern2 = qr{(?<P_ROOTCODE>.*)(?{ push(@rc1, ${^MATCH}) })\K(?{ $p_r +ootcode = "@rc1" })(?<DAY1>[0-9])(?{ push(@rc2, ${^MATCH}) })(?<P_MON +_CODE>[A-Z])(?{ push(@rc3, ${^MATCH}) })(?<P_NEW_MON_CODE>[A-Z])(?{ p +ush(@rc4, ${^MATCH}) })$}; $str2 =~ m/$pattern2/; print "Value of p_rootcode in Pattern 2 is : $p_rootcode\n";
displaying an email attachment from a hyperlink
1 direct reply — Read more / Contribute
by Anonymous Monk
on Dec 26, 2014 at 15:37
    Hi Monks,

    WARNING ====> Depending on the solution, this might be more of an html question than a Mime::Lite question, but either way, i'm confident that you monks will know what I'm doing wrong here. I have never been disappointed by this site.

    I am using Mime::Lite to email an html report which contains a hyperlink of text data. When a user clicks on the hyperlink, I want the text to be displayed in an appropriate OS application (in my case, it's Notepad). This seems simple enough, so I tried the following:

    ## this will not run and generate an html report my $html = <html content> my $msg = MIME::Lite->new( Subject => 'my html report', To => '', Type => 'text/html', Data => $html); $msg->attach( Type => 'text/plain', Path => '/var/tmp/data.txt' Disposition => 'attachment'
    I thought that referencing the attachment in an href like the following would do the trick
    <a href="./data.txt">
    but lo and behold, I have been fooled again! Strangely enough, I did this for image files with and it worked splendidly, so I'm not sure why it wouldn't work the same for plain text files. Anyone know what I'm doing wrong here?

    AS usual, thanks for taking the time to read my lengthy message.

    Best Regards, Michael

How to split big files with Perl ?
4 direct replies — Read more / Contribute
by zalezny
on Dec 26, 2014 at 12:20
    Hi Perl Gurus, does anybody knows how to split big files (for example: 10GB) to multiple small ones ? For example, I would like to take each big file in my backup folder and split for small peaces. For example 10GB file backup.dat needs to be splitted to :



    Is there any library in Perl for splitting files base on the size ? Or maybe some compress parameter to split automaticly files if they are bigger than size XX ? Thanks in advance for Your support ! Zalezny
SMS services
2 direct replies — Read more / Contribute
by jatuljain
on Dec 26, 2014 at 06:00

    I am new to SMPP module. I am looking for some script which can send a SMS from ESME --> SMSC. Though this we already have as "".

    My intention is to check SMSC server.

    What I need next is receive the same SMS sent to other ESME. In this case let say Basically setup would be like ESME (Subscriber 1) --> SMSC --> ESME(Subscriber 2) Can you please help me in this. I am able to send Submit_sm and get the response. But I am not getting how to forward the same to ESME receiver side ?

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 avoiding work at the Monastery: (7)
    As of 2014-12-28 17:38 GMT
    Find Nodes?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?

      Results (182 votes), past polls