Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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
Want END block to run except when usage()/--help invocation
1 direct reply — Read more / Contribute
by CoVAX
on Feb 26, 2015 at 23:50

    This is the first time I've used an END block. I need its functionality because I call 'die' at several points throughout this program and I have to always perform the code in the END block.

    I was making the final edits to a program's usage() function and noticed the following warnings following a --help invocation:

    perl -w -h
    Use of uninitialized value $time in concatenation (.) or string at ks. +pl line 253. Use of uninitialized value in concatenation (.) or string at lin +e 258. Use of uninitialized value in concatenation (.) or string at lin +e 258. Use of uninitialized value in concatenation (.) or string at lin +e 258. Use of uninitialized value in concatenation (.) or string at lin +e 258. Character in 'c' format wrapped in pack at line 262. Use of uninitialized value $time in concatenation (.) or string at ks. +pl line 283.

    Ah so! (German for "I see!") -- these warnings were the result of the END block being (unconditionally) executed after the usage() function.

    While I understand why this is happening, my question is: is there a way to have an unconditional END block execute except when the program's usage() function is being invoked? If not, then I propose to create yet another function to be called prior to die()'ing that will execute what's presently in the END block.

    Here's the relevant code:

    #!/usr/bin/perl -w use strict; use Getopt::Std; ... # Don't want END block to execute when these are invoked: sub HELP_MESSAGE(); # getopts() --help automatically invokes VERSIO +N_MESSAGE() first sub VERSION_MESSAGE(); # getopts() supports arguments --version and -- +help sub usage(); ... INIT { $| = 1; # Make Getopt::Std exit after printing # VERSION_MESSAGE() and/or HELP_MESSAGE() $Getopt::Std::STANDARD_HELP_VERSION = 1; } ...By design, main-line code comes to this END block: # An END block is always executed (barring some exceptions) as # late as possible, even after die() is called END { export_to_file_csv(); export_to_storable(); warn "Pages : $pages of Total Pages: $total_pages"; warn "Projs : $projs of Total Projs: $total_projs"; warn sprintf ("Images: %d amounting to bytes: %s", $imgs, $bytes)); } # END OF PROGRAM (normal or otherwise).

    I thank you for your time and assistance.

    Searched for donut and crumpit. Found donate and stumbit instead.
Getting started with BioPerl on Windows 8.1 64-bit
No replies — Read more | Post response
by bls61793
on Feb 26, 2015 at 23:37
    So, I need some help. I've been working on a project for a bit using several BioPerl Modules, and I'm having a hard time getting them set up and installed. I started with an Activestate install of Perl, and installed CPAN. However, the problem I was having is that I have a few required Perl modules that I was unable to install with CPAN, including Bio::Seq. CPAN told me two things: #1 I was missing two dependencies, including Data::Stag. and #2 dmake kept failing to execute (likely it didn't exist on my system). I tried to install the dependencies that I was missing, but I kept getting the same installation errors as #2. Because of this I came to the Monastery where someone suggested trying Citrus Perl instead of ActiveState, which I am now trying. I have downloaded and setup citrus perl such that I can us citrussterm.bat, but I'm honestly lost at this point. I would like to try and remove my previous (ActiveState) installation of perl and start over with CitrusPerl and get the modules I need installed. I'd really appreciate any help with this that I can get. Thanks a ton, -A Noob with a keyboard. Update: Now when I run CPAN.bat to launch cpan from the CitrusPerl directory and try to install the modules I need I keep getting the following error: "Could not make: unknown error"
Writing NULL values to a MySQL record via DBI
3 direct replies — Read more / Contribute
by ureco
on Feb 26, 2015 at 16:56
    Hope you guys will be able to help me out on this one, still new to Perl and working it out as a go !?! I'm reading a CSV File and storing the fields into variables. The CSV data can contain empty fields. These are being treated by Perl as undefined variables - which is what I believe is Perl's interpretation of NULL. I want to be able to write these undefined variables to my table (using DBI) which will then be stored as NULL values on my table. The code below is the segment updating the Database... whole script is 600 lines long so best not to post it all!
    # INSERT RECORD INTO MET_OFFICE_RAW_DATA TABLE no warnings 'uninitialized'; my $mo_raw_sql = "REPLACE INTO met_office_raw_data SET observation_datetime_utc = '$file_o +bs_datetime_utc', observation_datetime_local = '$file_obs_d +atetime_local', observation_datetime = '$file_obs_ +datetime', station_id = '$file_stati +on_id', station_name = '$file_station_ +name', temp_c = '$file_temp_c', dewpoint_c = '$file_dewpoint_c +', humidity_rh = '$file_humidity_ +rh', wind_dir_compass = '$file_wind_dir +_compass', wind_speed_mph = '$file_wind_s +peed_mph', wind_gust_mph = '$file_wind_gu +st_mph', visibility_metres = '$file_visibil +ity_metres', pressure_hpa = '$file_pressure +_hpa', pressure_tendency = '$file_pressur +e_tendency', weather_type = '$file_weather_ +type'"; my $mo_raw = $dbh->do($mo_raw_sql, undef) or die "Could not Insert record into met_office_ra +w_data \n";
    The fields that are undefined are being written as 0.00, 0 or " " not NULL. I found an unclear reference where I thought the ..., undef) parameter on the $dbh->do would map the undef variables to NULL in MySQL but this appears to make no difference. I've read bits on data binding for this but not sure how I can apply it to my code. Hopefully I'm missing something simple - very grateful for any assistance - Thanks
perldoc search
2 direct replies — Read more / Contribute
by derekstucki
on Feb 26, 2015 at 16:53 search box isn't functioning properly in Google Chrome in debian based linux distributions. No results are displayed, and it does not automatically go to the page that it should, like it does in Firefox. I've tested on two different computers running different debian based OSs. Can anyone reproduce this, or verify that it's only in Chrome on Debian based OS?

    On the search result page, I get the following javascript errors in Chrome:

    Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check
    Uncaught InvalidStateError: Failed to execute 'overrideMimeType' on 'X +MLHttpRequest': MimeType cannot be overridden when the state is LOADI +NG or DONE. ScriptLoader.load perldocSearch.doQuickSearch (anonymous function)
cpanm not retrieving remote files
2 direct replies — Read more / Contribute
by cLive ;-)
on Feb 26, 2015 at 16:43

    Confused about this. From the cpanm build log:

    Searching Net::SSH on cpanmetadb ... --> Working on Net::SSH Fetching -> FAIL Download +.tar.gz failed. Retrying ... -> FAIL Download +.tar.gz failed. Retrying ... -> FAIL Download +.tar.gz failed. Retrying ... -> FAIL Failed to download +t-SSH-0.09.tar.gz -> FAIL Failed to fetch distribution Net-SSH-0.09

    But, if I run this in the shell:


    the module downloads as expected, and I can install manually.

    That's great and all, but then I have to install each dependency manually.

    I've deleted the .cpanm and .cpan directories in /root, but I still keep hitting this issue with every module I install. I've tried specifting wget or curl as CL args on cpanm, but they don't seem to be making a difference.

    It worked fine when I first installed it, but then just stopped a few weeks ago.

    I'm not sure how to debug this one. Any ideas?

matching whole words only
3 direct replies — Read more / Contribute
by dirtdog
on Feb 26, 2015 at 16:39

    Hi Monks, i'm hoping there is a better way to check for my value from an Array of hashes. The flaw in the sample code is that i'm grepping through the values for the word SWIM and in the values is the word SWIMMING so it thinks there was a match. Is there a way to tweak it so it only finds an exact match in this scenario?

    #!/usr/bin/env perl use warnings; use strict; use Data::Dumper; my $state= 'OHIO'; my %region_of_state; my @events; while (<DATA>) { chomp; next if /^\s*$/ || /^\#/; my $aref = [split /,/, $_]; foreach ($aref->[2]) { print "event = $_\n"; print "region = $aref->[1]\n"; push( @{$region_of_state{$aref->[1]}}, $_) if ($aref->[0] eq $ +state); } } my $event = 'SWIM'; my $region_of_state = 'NE'; if (@events = grep /$event/, @{ $region_of_state{$region_of_state}}) { print "Found a match\n"; } else { print "No Match\n"; } #print Dumper(\@events); #print "@events\n"; __DATA__ OHIO,NE,SWIMMING SWIMMER CLOCK OHIO,SW,BASEBALL PLAYER BATTER

    Any help is greatly appreciated. DD

Making workbook visible with Excel 2013
1 direct reply — Read more / Contribute
by chinman
on Feb 26, 2015 at 14:19
    Greetings! I have a script that mucks around in Excel, which I've been using since Office 2000. Suddenly and tragically, I am having issues with Excel 2013 users. I need to make the workbook visible, otherwise when I save the Excel file, the workbook will be hidden when the user tries to open it. I have used the code below successfully for years to accomplish this. I have marked the offending line below in the code. When this is used with Excel 2013 on Win7 x64, I get the following error:

    Win32::OLE(0.1712) error 0x8002000b: "Invalid index" in METHOD/PROPERTYGET "Windows" at line 91. Can't use an undefined value as a HASH reference at line 91.

    Has anyone else run into this, or have any suggestions?

    use strict; use Win32::OLE qw(with); use Win32::OLE::Const 'Microsoft Excel'; sub open_sheet { my $file = shift; my $workbook = Win32::OLE->GetObject( "$file" ) || die "Could not locate $file.\n"; my $excelApp = $workbook->{Application}; $excelApp->{Visible} = 1; # ****this next line no longer works in Excel 2013**** $excelApp->Windows( $workbook->{Name} )->{Visible} = 1; # ^^^^bad! .... }


Why did @$ variable bite me in the ass?
3 direct replies — Read more / Contribute
by dmitri
on Feb 26, 2015 at 14:12
    So there I was, happily rolling along, writing my class, and using an array ref as my underlying data structure. I then wrote another method:
    sub another_method { my ($self, $magic_val) = @_; $$[MAGIC_VALS]{$magic_val} = 1; }
    and ran it. There were no compile-time or run-time errors, but the logic did not work. Can you spot the problem? That's right: it should be $$self[MAGIC_VALS]{$magic_val} instead! What is this magical @$? Perl does not complain about it and will happily use this array:
    use strict; use warnings; $$[0] = 1; $$[1] = 'dude!'; print "@$", "\n"; # Prints "1 dude!"
    It's not in perlvar... What's up with this variable?
HELP: Problem with PDF
2 direct replies — Read more / Contribute
by cristofayre
on Feb 26, 2015 at 13:24
    I'm tearing my hair out!!

    I generated a PDF with AP12. If I click the file locally, it loads into reader fine. But no matter what I do in perl, it stops loading data at around 4K of a 212K file. (Reading the data sent to page in a text editor, it seems to stop at %%EOF)

    I have tried the usual "While(<xx>)" and "@file=<ZZ>", and even tried the "local $/" to set to undefined size ... but still it stops reading at 4K / %%EOF

    << /Info 4 0 R /Root 1 0 R /Size 815 >> startxref 207080 %%EOF

    So I can't even 'grab' it to send it to a zip file, (everything is code based from generating file to delivery) 'cos I can't read the entire file ... My ultimate aim is to get the file to the browser, or to a save prompt - but can't do either.

    Annoying thing is, I've got it to work before using the "Content-disposition" "content-type: application/pdf" ... but I suspect those files are less than the buffer size (??)

    I even tried it with another file (8MB) Whilst I got the correct number of pages after 20 seconds - and having to switch to an older reader ... there was no text on any page!!

    I can't even email it to people ... cos I can't read the entire file to memory.

    Final try:

    open (QR, "<QR_labels.pdf"); binmode(QR); print "Content-type: application/pdf\n\n"; print 'Content-Disposition', "attachment; filename=Booth_guide.pdf\n\n +"; while(read(QR,$buffer,10000)){ print $buffer; } close(QR);

    Same thing. Only 4K delivered to browser from 212K, and breaking as shown above. This has me beaten! I don't know what to try next

XML Parsing
4 direct replies — Read more / Contribute
by munjalm
on Feb 26, 2015 at 09:47
    Hi Monks, I have an XML like below. I need to fetch only values Agreement ID, Cycle Code and Subscriber ID List values. However agreement id and cycle code contains always single values but subscriber id list can contain multiple values as in the snippet below. Kindly please help.
    <PerformanceIndicator type="VEL Allowance pooling occurrence recurring + rolling restricted period sensitive with bundling" version="00000000 +0"> <Attribute name="Item ID" basicType="Numeric"><Value value="23409056"/ +></Attribute> <Attribute name="Agreement ID" basicType="Numeric"><Value value="15827 +134"/></Attribute> <Attribute name="Cycle code" basicType="Numeric"><Value value="1301"/> +</Attribute> <Attribute name="Cycle year" basicType="Numeric"><Value value="2015"/> +</Attribute> <Attribute name="Cycle month" basicType="Numeric"><Value value="2"/></ +Attribute> <Attribute name="Customer ID" basicType="Numeric"><Value value="164922 +51"/></Attribute> <Attribute name="PI type ID" basicType="Numeric"><Value value="9436004 +9"/></Attribute> <Attribute name="Offer instance" basicType="Numeric"><Value value="140 +546178"/></Attribute> <Attribute name="Rerate type" basicType="String"><Value value="R"/></A +ttribute> <Attribute name="Last event date" basicType="DateTime"><Value value="2 +015-02-25 19:34:41"/></Attribute> <Attribute name="Offer ID" basicType="Numeric"><Value value="23408986" +/></Attribute> <Attribute name="Expiration date" basicType="DateTime"><Value value="2 +015-02-28 23:59:59"/></Attribute> <Attribute name="PI status" basicType="Numeric"><Value value="4"/></At +tribute> <Attribute name="Billing arrangement" basicType="Numeric"><Value value +="0"/></Attribute> <Attribute name="Pay channel" basicType="Numeric"><Value value="0"/></ +Attribute> <Attribute name="Dummy string" basicType="String"><Value value="0"/></ +Attribute> <Attribute name="Dummy numeric" basicType="Numeric"><Value value="0"/> +</Attribute> <Attribute name="Dummy date" basicType="DateTime"><Value value="2020-1 +2-31 19:50:00"/></Attribute> <Attribute name="Dummy boolean" basicType="Boolean"><Value value="Yes" +/></Attribute> <Attribute name="Proration factor" basicType="Numeric"><Value value="1 +.0000"/></Attribute> <Attribute name="Quota per period" basicType="Numeric"><Value value="7 +500.00"><DimensionKey dimensionName="Period" index="0" value="0" nam +e=""/></Value></Attribute> <Attribute name="Rolled from previous cycle per period" basicType="Num +eric"><Value value=""/></Attribute> <Attribute name="Remaining quota per month and period" basicType="Nume +ric"><Value value="7344.0000"><DimensionKey dimensionName="Month" in +dex="0" value="0" name=""/><DimensionK ey dimensionName="Period" index="0" value="0" name=""/></Value></Attr +ibute> <Attribute name="Utilized quota per month and period" basicType="Numer +ic"><Value value="156.0000"><DimensionKey dimensionName="Month" inde +x="0" value="0" name=""/><DimensionKey dimensionName="Period" index="0" value="0" name=""/></Value></Attrib +ute> <Attribute name="Remaining quota to be rolled" basicType="Numeric"><Va +lue value=""/></Attribute> <Attribute name="Number of rolled cycles" basicType="Numeric"><Value v +alue="0"/></Attribute> <Attribute name="Full quota per period" basicType="Numeric"><Value val +ue="7500"><DimensionKey dimensionName="Period" index="0" value="0" n +ame=""/></Value></Attribute> <Attribute name="Limit quota" basicType="Numeric"><Value value="-1"><D +imensionKey dimensionName="Index" index="0" value="0" name=""/></Val +ue><Value value="-1"><DimensionKey di mensionName="Index" index="1" value="1" name=""/></Value></Attribute> <Attribute name="Remaining quota per subscriber" basicType="Numeric">< +Value value="-1"><DimensionKey dimensionName="Index" index="0" value +="0" name=""/></Value><Value value="-1 "><DimensionKey dimensionName="Index" index="1" value="1" name=""/></ +Value></Attribute> <Attribute name="Utilized quota per subscriber" basicType="Numeric"><V +alue value="0"><DimensionKey dimensionName="Index" index="0" value=" +0" name=""/></Value><Value value="0">< DimensionKey dimensionName="Index" index="1" value="1" name=""/></Val +ue></Attribute> <Attribute name="Subscriber ID list" basicType="Numeric"><Value value= +"15846291"><DimensionKey dimensionName="Index" index="0" value="0" n +ame=""/></Value><Value value="15613501 "><DimensionKey dimensionName="Index" index="1" value="1" name=""/></ +Value></Attribute> </PerformanceIndicator>

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 perusing the Monastery: (6)
    As of 2015-02-27 06:29 GMT
    Find Nodes?
      Voting Booth?

      On my keyboard, Caps lock is:

      Results (440 votes), past polls