Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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
Not able to execute perl sub routine
2 direct replies — Read more / Contribute
by sachin raj aryan
on Dec 03, 2016 at 01:38

    I have two files one and another I am calling from Main file ... problem is that when i uncomment the commented part of Main file it is not executing whereas when i run it with commented part it is going to unzipping and unzipping all file below is my code #!/usr/bin/perl use strict; use warnings; use Net::FTP::Recursive ; require ""; my $host = "xx.xx.xx.xx"; my $user = "tt"; my $password = "tt"; my $basedir = "locations"; my$reportfldr = "reports"; my $filename = 'List.txt'; my $date = "20161128"; #my $brchid="b00330"; ############Fetching report from Server#################### open(my $fh, '<:encoding(UTF-8)', $filename)or die "Could not open fi +le '$filename' $!"; while ( my $brchid = <$fh>) { chomp $brchid; print "$brchid\n"; # system "mkdir $brchid"; # chdir($brchid); # system "mkdir $date"; # chdir ($date); # my $dir = join"/",$basedir,$brchid,$reportfldr,$date; # my $f = Net::FTP::Recursive->new($host,Debug => 0) or die "Can't + open $host\n"; # $f->login($user, $password) or die "Can't log $user in\n"; # $f->binary(); # $f->cwd($dir) or die "Can't cwd to $dir\n"; # $f->dir(); # $f->rget(); # $f->quit; # print " I am entering into subroutine \n"; &unzipping($brchid,$date); # sleep 5; # chdir "../.."; } #!/usr/bin/perl use strict ; use warnings ; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); sub unzipping{ my $brchdir = $_[0]; my $date = $_[1]; my $dir1 = "Transaction_Reports"; my $dir2 = "Reports_On_Request"; my $dir3 = "Other_Reports"; my $dir4 = "MIS_Reports"; my $dir5 = "Letters_Reminders"; my $dir6 = "Exception_Reports"; my $dir7 = "Concurrent_Audit_Reports"; my $dir8 = "Adhoc_Audit_Reports"; my $dir = $brchdir."/".$date; print "I m in unzipp dir $dir \n"; my $dirf=$dir."/".$dir1; my $dirs = $dir."/".$dir2; my $dirt = $dir."/".$dir3; my $dirft = $dir."/".$dir4; my $dirfv = $dir."/".$dir5; my $dirsx = $dir."/".$dir6; my $dirsn = $dir."/".$dir7; my $direi = $dir."/".$dir8; print "I m here \n" ; print " $dir"; for my $input ( glob "$dir/*.gz" ) { print " I m inside for $dir \n"; my $output = $input; $output =~ s/.gz// ; gunzip $input => $output or die "Error Uncompressing '$input': $GunzipError\n"; } } 1;
Perl Diameter protocol
1 direct reply — Read more / Contribute
by iThunder
on Dec 02, 2016 at 18:15
    Hi Monks,

    Do perl has module for diameter protocol? This question was previously asked in this forum (

Are we lacking behind in YAML spec versions?
2 direct replies — Read more / Contribute
by mikkoi
on Dec 02, 2016 at 11:55
    The new API description language RAML uses YAML 1.2 as file format. But it seems that neither YAML, YAML::Tiny, YAML::XS nor YAML::Syck support YAML 1.2 spec, only 1.1. And 1.1 came out already in 2009. Situation can't be this?
download an array of text as pdf
2 direct replies — Read more / Contribute
by ajaykannan
on Dec 02, 2016 at 04:19

    The code is to fetch data after executing commands from windows machine to network devices.Am getting the result but the problem is have to outfile it as pdf. I done but only first line are printed and other content are not saved in that.I need to put all the content and save it as pdf format. Any help plz.

    #!/usr/bin/perl use strict; use warnings; use Net::Ping; use Net::SSH2; use PDF::API2; #************set pdf*************** my $save_pdf_as="mypdf.pdf"; # Create a blank PDF file my $pdf = PDF::API2->new(); # Add a blank page my $page = $pdf->page(); # Set the page size $page->mediabox('Letter'); # Add a built-in font to the PDF my $font = $pdf->corefont('Helvetica-Bold'); #************ssh module*************** my $ssh = Net::SSH2->new(); my $username="username"; my $password="password"; my $ip_address = ("ipaddress"); my $ping= Net::Ping->new(); if($ping->ping($ip_address)) { if($ssh->connect($ip_address)) { if($ssh->auth_password($username,$password)) { my $chan = $ssh->channel(); $chan->blocking(1); $chan->shell(); $chan->write("terminal length 0\n"); $chan->write("show ip interface brief \n"); my $text = $page->text(); $text->font($font, 20); $text->translate(200, 700); while (<$chan>) { if (/\bterminal length\b/i .. /^\n*$/) { print; $text->text($_); } } $pdf->saveas($save_pdf_as); } else { print "auth failed"; } } } else { print "not pinging"; }
Web Scraping with Find / Replace
1 direct reply — Read more / Contribute
by sjfranzen
on Dec 01, 2016 at 17:00
    I am sure this is easy for you monks, but this new guy would love your help. I need to read a set of web pages, extract content between a
    and then find all referential links and replace them with fully qualified links(i.e. add domain to the link) then finally save it to a file. Thanks in advance
    use feature 'say'; # a better "print" use Mojo; ###################################################### my $insert_str = ""; #get the pages to fetch from the links.txt file open (LINK, "links.txt") || die "couldn't open the file!"; my $ua = Mojo::UserAgent->new; #loop through all of the urls while ($record = <LINK>) { say ("Getting web site info for: $record\n"); #determine the new file name by the subdirectory / path since all f +etched pages will be index.html $newFileName = (substr $record, (rindex($record, "/", (rindex($recor +d, "/") -1)) + 1), (rindex($record, "/") - rindex($record, "/", (rind +ex($record, "/") -1)) -1)) . '.html'; print("Should save the information to a new file as $newFileName\n") +; #get the page contents my $response = $ua->get($record)->res->dom; if ($response->is_success) { #Find the <div class="main-content"> my $content = $response->at('.main-content'); #TODO Replace all of the links with fully qualified url's #TODO Save the master_content to a file with the same file name } # else { # die $response->status_line; # #TODO Send an email to admin letting them know of the issue # } #end of while loop } close(LINK);
Perl debugging initialization: did I just reinvent the wheel?
4 direct replies — Read more / Contribute
by mpersico
on Dec 01, 2016 at 14:53

    I have worked out a system that allows me to maintain breakpoints in the Perl debugger from session to session and load them up next time I run the debugger:

    First, I create a ~/.perldb file that contains:

    ## -*- cperl -*- sub afterinit { use Cwd; my $dbg = getcwd(). "/.perldb"; print "$dbg...\n"; if ( -e $dbg ) { no strict; do $dbg; } }

    Then I create a local .perldb file in the current working directory where I run  perl -d:

    ## -*- cperl -*- push @DB::typeahead, 'f', 'b 75', 'f bin/mymainscript', 'b 180', 'b 192', 'L';

    So, my questions are:

    1) Is this technique widely enough known that I would look like an idiot for blogging about it?

    2) Is the usage of @DB::typeahead unsafe in that it's a direct tap into the DB module that may be changed and, therefore, should not be promoted?

    Thank you.
Can't update file
6 direct replies — Read more / Contribute
by TClayJ
on Dec 01, 2016 at 11:30

    I am trying to update a file that contains the md5 checksum for a file if the value in the file is different from the new md5checksum but it is not updating. Here is the code, any suggestions are greatly appreciated.

    if ( &download( $url, $save_file ) ) { # Successfully downloaded, now compair previous md5 value # and if different move to the transfer folder my ( $guidId, $ext ) = split ( '.zip', $guid ); my $md5File = "${hifld_md5_folder}/${guidId}.md5"; open ( MD5, '+>>', $md5File ) || &loggingError("Couldn't open md5 file ($md5File) for + reading : $!"); $md5row = <MD5>; close( MD5 ); my $md5chksum = &calcMD5( $save_file ); print "md5row = ${md5row}\n"; print "md5chksum = ${md5chksum}\n\n"; # check to see if the file has changed since the last down +load # if not then delete the downloaded file otherwise # update the md5 file and rename the downloaded file for m +oving if ( $md5row = $md5chksum ) { my $cmd = "rm $save_file"; # `$cmd`; } else { # Update the md5 file open ( MD5OUT, '>', $md5File ) || &loggingError("Couldn't open md5 file ($md5File) +for writing : $!"); print MD5OUT "${md5chksum}"; close( MD5OUT );
Extracting datapart (YYYYMMDD) with filename regular expression
3 direct replies — Read more / Contribute
by rahulme81
on Dec 01, 2016 at 10:36

    Hi. I have log file names in below format

    1) apachelog_LT100.7.7.7_21.SRV.apache.20161130-123456.258976.log

    2) APACHE_PP1.20161130-12:39:55-30822.Release.LT100.7.7.7.PP1.log

    3) APACHE_PS_PP1.20161130-15:29:37-5319.ebf_run.LT100.7.7.7.log

    Need to store the date 20161130 in one variable for all files separately, How do I create the regular expression for this. Thanks.

download as txt and pdf
1 direct reply — Read more / Contribute
by ajaykannan
on Dec 01, 2016 at 10:10

    this program is to collect data that is running from window to network devices using SSH2.Output came but do no how to download it as .txt or .pdf in perl .Here is my code and any help will help to improve my knowledge

    #!/usr/bin/perl use Net::Ping; use Net::SSH2; use strict; use warnings; my $ssh = Net::SSH2->new(); my $username="username"; my $password="password"; my $ip_address = ("ipadderss"); my $ping= Net::Ping->new(); if($ping->ping($ip_address)) { if($ssh->connect($ip_address)) { if($ssh->auth_passwo +rd($username,$password)) { my $chan = $ssh->channel(); $chan->blocking(1); $chan->shell(); $chan->write("terminal length 0\n"); $chan->write("show cdp neighbor \n"); while (<$chan>) { print if /\bterminal length\b/i .. /^\n*$/ ; } # $chan->close; } else { print "auth failed"; } } } else { print "not pinging"; }
Concatination Issue
1 direct reply — Read more / Contribute
by TClayJ
on Dec 01, 2016 at 09:10
    this code:
    if ( !-e $save_file ) { if ( $access eq "Open" ) { ( $http, $trash, $website, $directory, $guid ) = split ( '/', $d +lFile ); $url = $dlFile; } else { ( $http, $trash, $website, $gii, $sharing, $rest, $content, $ite +ms, $guid ) = split ( '/', $dlFile ); my $test = "123456781234567890abcdefghijklmnopqrstuvwxyz"; $url = $dlFile . $test; print "url=$dlFile$test\n"; } }

    produces this output:


    I want it to product this output:

    for some reason I can not figure out the $test variable is being appended to the start of the $dlFile variable and not the end.

    Any help is greatly appreciated.

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2016-12-03 13:39 GMT
    Find Nodes?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:

      Results (56 votes). Check out past polls.