Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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
Parsing text string in Perl
3 direct replies — Read more / Contribute
by gbwien
on Sep 01, 2015 at 17:48
    Hi,

    I am trying to get back into Perl and I have a question regarding how best to parse the following

    I have a file which contains many of the following entries

    Time: 1/9/2015-09:01:43.010 Protocol: SMPP ESME: ehttp_rknoe Direction: Outgoing From: 10.247.231.212/2775 To: 10.247.231.212/35173 PDU Type: Full PDU PDU Length: 16 PDU Data: 00000010800000040000005800029a92 Decode Error 0x00000000 Decoded PDU: [ smpp hdr 16 octets ] 00000000: 00 00 00 10 command_length: 16 00000004: 80 00 00 04 command_id: 0x80000004 submit_sm_re +sp 00000008: 00 00 00 58 command_status: 0x00000058 ESME_RTH +ROTTLED 0000000C: 00 02 9A 92 sequence_number: 170642

    I want to parse the complete file in perl and print out time and only those entries that have ESME_RTHROTTLED.

    The file may contain many other entries but I only want to print out this entry

    Thanks tom
Problems with PDF::API2 and TextBlock
1 direct reply — Read more / Contribute
by dougconran
on Sep 01, 2015 at 17:41
    I have an application that uses PDF::API2 (amogst other things to display jpg images) and PDF::TextBlock which has been running without problems on perl5.10.1. However I've just (foolishly, probably) upgraded my server and perl to v5.22.0. I've installed the relevant modules but I'm getting some really weird behaviour. Sometimes the pdf document will print with data and image but as often as not it just prints the initial blank pdf page (which I load from a base pdf doc). After exhaustive testing it would seem that $pdf->image_jpeg is one culprit the other being the TextBlock ->apply method. As far as I'm aware the problem areas work fine in another similar program and sometimes in this one. What's going on and does anyone have any idea how to fix it?
Processing spreadsheet with some cells in ASCII, other cells in UTF-8
3 direct replies — Read more / Contribute
by Amphiaraus
on Sep 01, 2015 at 16:43

    I am writing a Perl program which takes input from a column in an Excel spreadsheet listing names of European cars. This column is named "MenuList". The names of European cars often contain letters with symbols above them that are not seen in English, and which must be encoded in UTF-8 (Examples: , , ). Other cells in the same column lack these foreign symbols and are encoded in ASCII. I am writing out the contents of this column to an *.ini file.

    I am finding that with the code shown below, the car names in ASCII are being written out to the *.ini file without problems, but the car names with foreign symbols - encoded in UTF-8 - are garbled in the *.ini file. These garbled strings are accompanied by the error message "Wide character in print"

    Is there a way to read input from an Excel spreadsheet with mixed encoding (some cells in UTF-8, other cells in ASCII), and write contents of these cells to an *.ini file, with no garbled output in the *.ini from the cells that contained UTF-8 encoding?"

    chomp $menuList[$i]; $decodedMenuList = decode("utf8",$menuList[$i]); $cfg_baselicense->newval($partNumList[$i],"MenuSelection",$decod +edMenuList); $cfg_baselicense->RewriteConfig();
Extracting network information
4 direct replies — Read more / Contribute
by Kasemodder
on Sep 01, 2015 at 12:57
    My apologies, I'm not a perl programmer (more of a bash scripter by trade), but I've inherited a set of perl scripts that need to be updated for a new OS release and I'm in a bit of trouble trying to fix them. What I'm trying to do is extract a number of relevant network parameters to use in other parts of the script.
    #!/usr/bin/perl chomp($syshost = `hostname`); chomp($sysip = `ifconfig | grep -i inet | egrep -v "inet6|127. +0.0.1" | head -n 1 | awk '{print $2}'`); chomp($sysnm = `ifconfig | grep -i inet | egrep -v "inet6|127. +0.0.1" | head -n 1 | awk '{print $4}'`); print "hostname : $syshost\n"; print "IP : $sysip\n"; print "Netmask : $sysnm\n";
    What I end up getting however, does not match the expected result.
    EXPECTED RESULT hostname : laptop.example.com IP : 10.10.50.21 Netmask : 255.255.255.0 RECEIVED RESULT hostname : laptop.example.com IP : inet 10.10.50.21 netmask 255.255.255.0 destination 1 +0.10.50.1 Netmask : inet 10.10.50.21 netmask 255.255.255.0 destinat +ion 10.10.50.1
    If I run the commands in the ` ` sections within my bash shell, I do get the expected result, but with the script, I do not. I've seen some stuff about awk and perl not getting along but it's what I know, and I'm in a bind with inherited code (with 0 comments), a language I am unfamiliar with, and tight deadlines, so any pointers into the right direction would be appreciated.
calling sub with object as param
1 direct reply — Read more / Contribute
by perlnewb4life
on Sep 01, 2015 at 09:38

    Hello i need some help understanding OOP Perl using Moose; i have created a class whose attributes are all subtypes , for validation purposes. Example code :

    package InputData; use Moose; use InputDataSubtypes qw(subtype); has 'attribute' => ( is => 'rw', isa => subtype, required => 1, ); 1;

    I want to pass InputData to a subroutine , then use InputData->attribute , as if i would be using a getter in Java. The InputData->attribute is used to do a query on a mysql database using prepared statement:

    package Operations; use DBI; use DBConnection; use InputData; use Moose; sub querryDatabase() { my($inputData) = @_; my $query = 'select stuff from myTable where attribute = ?'; my $dataSource = DBConnection->getConnection(); my $preparedStatement = $dataSource->prepare($query); $preparedStatement->execute($inputData->attribute) or die "SQL Erro +r: $DBI::errstr\n"; while (my @row = $preparedStatement->fetchrow_array){ print "@row\n +"; } $preparedStatement->finish(); $dataSource->disconnect(); } 1;

    i call the subroutine like this :

    package MainScript; use strict; use warnings; use v5.22; use InputData; use Operations; use Moose; my $myInputData = InputData->new( { attribute => 'caller', } ); #here i do the sub call; Operations->querryDatabase($myInputData);

    But i get the error Can't locate object method "attribute" via package "Operations" at ....Operations.pm line 28. Any help would be greatly appreciated, also sorry if i didn't format the question appropiatly

Delete entire row without using Win32::OLE
3 direct replies — Read more / Contribute
by ravi45722
on Sep 01, 2015 at 07:26

    I have a excel sheet in the format of XLSX. For reading this I am using Spreadsheet::ParseXLSX. I read the nodes Delete rows in excel & a lot. But all are suggested using Win32::OLE. I am working in RHEL 5. Is there any other module to delete entire row in excel sheets??

    Update: Module name changed

PDF::API2 module prints junk characters
2 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 01, 2015 at 05:41

    Hello, When I run the below code, I am getting junk characters as output. Any solution to avoid this? I need last modified date from PDF document properties. I tried stat function but it is giving different date. I need modified date from document properties. After opening PDF, press Ctrl+D, it throws the properties and I want that modified date.

    my $pdf = PDF::API2->open("$file"); print Dumper { $pdf->info }
For loop not executing all the values
3 direct replies — Read more / Contribute
by parthodas
on Sep 01, 2015 at 02:40
    I'm reading a file and then running a for loop for each value. My script is stopping after executing the first value. The file_detail.txt has multiple lines. But it is not proceeding to the next value. My script looks something like this --
    open (my $fh, "<", "$mycrdir/file_detail.txt" ); foreach(my $value = <$fh>) { chomp($value); if($value eq "abc") { qx/"mkdir $mydir"/; func1($mycrnum,$mydir,$value,%envparams); func2($mycrnum,$mydir,$value,%envparams); } elsif($value eq "123") { qx/"mkdir $mydir"/; func1($mycrnum,$mydir,$value,%envparams); func2($mycrnum,$mydir,$value,%envparams); } }
how to move to the first row of array after passing it through a while loop
4 direct replies — Read more / Contribute
by wangruonan
on Aug 31, 2015 at 23:35
    #!/usr/local/bin/perl use strict; use warnings; use Text::CSV; use FileHandle; my $i=1; my @fields; my @array; my @list = (11, 15, 18); #my $colref=\@fields; my @headers; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); #my $IN = new FileHandle; my ($logfile, $logpath); $logpath = 'C:\\Users\\amwang\\Desktop'; $logfile = "$logpath\\log.txt"; open (LOG,">>","$logfile") || die ("Error : can't open log file"); my $file = "$logpath\\TCT_AGM.csv"; # foreach loop execution foreach $a (@list) { print "value of a: $a\n"; open(my $data, "<$file") or die "Could not open '$file' $!\n"; while (my $line = <$data>) { chomp $line; if ($csv->parse($line)) { my $field_count = $csv->fields(); while (my $fields = $csv->getline( $data )) { push(@headers,$fields->[$a]); } } else { warn "Line could not be parsed: $line\n"; } print LOG $headers [2], "\t"; } close ($data); }

    I want to print different col value of my file, but only able to do the very first value in the foreach loop. i think the problem is i need to move to the first row of the array again at the end of while loop, not sure how can i do it..

[Solved]: How to perform different actions before and after submit button is clicked.
3 direct replies — Read more / Contribute
by Perl300
on Aug 31, 2015 at 18:50
    Hello Monks,

    I am trying to perform specific tasks in my CGI script depending on if the "Submit" button is clicked or not.

    Basically, I have a dynamic webpage and I am trying to ensure that when the page is first called from browser, some code should be skipped. To do this, I am trying to put a check for Submit button.

    I am trying to use $query->param('submit') with a sample code (Test.pl) like:

    #!/usr/bin/perl use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use CGI; my $query = new CGI; print $query->header; print <<END_HTML; <html><head><title>Thank You</title></head> <body> <form method="post"> <input type="submit" value="Submit" id="button1" /> <br />Thank you - your form was submitted correctly! </body></html> END_HTML my $a = $query->param('submit'); print "Value: $a\n";

    With this code when I access Test.pl from browser, it shows the page with submit button, but even after I click on submit nothing is stored $a.

    Can you please help me find what I am missing here? My final goal is to run a chunk of code when the webpage is just fetched but Submit button is not clicked. After submit button is clicked, I am showing the resulting data in the same page.


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
  • 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 perusing the Monastery: (7)
    As of 2015-09-02 00:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred temperature scale is:










      Results (56 votes), past polls