Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
Choosing an AWS S3 CPAN module
1 direct reply — Read more / Contribute
by sm@sh
on Sep 29, 2014 at 04:54
    I have a simple requirement to read a file from an S3 bucket. We'll be using access key + secret IAM authentication. I can find these modules on CPAN: * Net::Amazon::S3 * Amazon::S3 * AWS::S3 The first of these seems to be most common, and well-maintained. The w +eight of dependencies isn't a major problem given we already have man +y of them in our app. However, they are all quite bloated for my requirements, and from the +AWS docs, it is simply a case of generating an auth token to access t +he file. Can anyone recommend any other modules, or has written their own acces +s code?
Best way to do this
2 direct replies — Read more / Contribute
by PetreAdi
on Sep 29, 2014 at 03:24

    I have a text file that looks like:

    /path/to/one/library /path/to/another /path/to/another/01 /path/to/another/s /path/to/another/02 /path/to/another/w1 /path/to/another/03 /path/to/another/1e /path/to/another/2014

    I need to get the LAST directory(whose last name is numeric) 01;02;03;2014

    ($b) = $line =~ m#([^/]+)$#; ($c) = $line =~ m#(\d+)$#; if ($b eq $c){print "Match \n";}
Tab-completion ... not perl related though
4 direct replies — Read more / Contribute
by Lowry76
on Sep 28, 2014 at 19:26
    Hi monks,

    I have to appologize that I'm posting a non-perl question. But I don't know whom else to ask and it is kind of perl related for me.
    Problem: When I'm calling a perl script with "perl -d path1 -o path2 ..." on some computers I can't use tab-completion for certain parameters. For example, on my work laptop the "-d" parameter disables bash tab-completion. I guess there is a bash config file of some sort that defines those parameters. Can somehone give me a hint what to search for?

How to make a timer with breaks
2 direct replies — Read more / Contribute
by xdfreaknikdx
on Sep 28, 2014 at 19:06

    (Hi I need to add countdown timer to my perl program which starts from 00:01:00(1 minute)when I start running the script and in every 00:00:10 (10 seconds) the timer stops and the program will asks if you want to continue or stop the countdown. Thanks in advance.)

Can you write a faster code to perform this task?
7 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 28, 2014 at 18:10
    Dear Monks,
    I am hereby seeking your expertise on the following problem:
    I have this snippet of code:
    $count=0; while($_=~/(M+)/g) { $count++; } print $count."\n";

    which reads lines like the following:

    and counts (obviously) the number of occurrences of M+ (in this example it's 3). Is there any faster way of writing the code I gave you? I am asking this because this code will be used in reading files with maybe even 80.000 lines like this and if I could find a faster way I could save up some time.
    Thank you in advance!
Variable not set properly in perl
3 direct replies — Read more / Contribute
by sar123
on Sep 28, 2014 at 11:50
    I have the following code:
    #!/usr/bin/perl -w use warnings; use diagnostics; open $fh , "<", "$ARGV[0]" or die "Could not open file: $!"; sub getsub{ my $sub = $_[0]; print "sub entered for $sub\n"; while (<$fh>) { if (/\.subckt $sub/../\.ends/) { print ; } } } while (<$fh>) { if ($_ =~ /^xa1/) { $line = $_; print "line found to be $line\n"; while ((my $nxt = readline($fh)) =~ /^\+/) { $line = $nxt; print "line changed to $line\n"; } $line =~ s/\s+$//; print "last line is $line\n"; my $sub = (split '\s', $line)[-1]; print "subcircuit found is $sub in $line\n"; getsub($sub); } }
    Here I am trying to print some text between two patterns inside the `getsub` routine. But when I try to run this I enter the subroutine but doesn't enter the if block inside the subroutine. I am trying to run it on the following file:
    .subckt a1 x y z xa a b c1 xb c d e1 xc f g h1 .ends .subckt c1 x y z xa a b f xb c d e xc f g h .ends .subckt e1 x y z xa a b c1 xb c d k1 xc f g h1 .ends xa1 a s f a1
    I want to print the contents of the file between ".subckt a1" till ".ends".
    I know this could be done by the one liners of perl on the command line, but I want to create some generalized script for different files so I need to go this way only. What is wrong with the above code.
Is there any efficient way i can take out a specific column from hundreds of files and put it in one file?
9 direct replies — Read more / Contribute
by coolda
on Sep 27, 2014 at 23:38

    As the title indicates, i have thousands of files. Each file follows the same format for example each file has a format of:

    Gene exp1 exp2 exp3 exp4 ... 1 2 3 4 5 6
    I want to take out third column only from every file and put it into one file so i can compare them. The code that i'm working on now requires too many codes. Is there any way i can make this work simpler? any insights tips, or advices will be appreciated. I've been working on this for a week, and i still am struggling..

State of the GUI widget art?
3 direct replies — Read more / Contribute
by over2sd
on Sep 27, 2014 at 18:34

    I've been doing some GUI programming on Linux using GTK2 (because I'm pretty familiar with it and like the API), but when I tried to install Gtk+ on my MSWin8 box, I ran into problems.

    Using CPAN to try to install Gtk2 dies because it can't find/install one of the dependencies, following the instructions in the download from got me nowhere (it/I couldn't find one of the programs listed in the instructions), and the installers for GTK I found on the net are all pretty stale.

    So, I'm looking at switching to a different toolkit.

    I've poked around a bit looking for suggestions, but all the questions I found on here and other forum sites were also fairly old.

    My main audience is, I think, going to be people who run Windows. If I, as a fairly experienced user, can't get the underlying GUI toolkit working, I can't expect Joe User to be able to do it, and I don't want to put any unnecessary barriers in the way of using my program.

    I'm looking pretty strongly at WxWidgets, but I wanted to get some input, in case there's something better. What's the best toolkit these days for end-user ease-of-use on MS-Windows, that also works under Linux?

Date/Format error
1 direct reply — Read more / Contribute
by oldcity
on Sep 27, 2014 at 08:00
    Had to reinstall Ubuntu 12.04.5. Previously installed sudo apt-get install libdate-calc-perl to run program.

    Can't locate Date/ in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at GasProg line 9. BEGIN failed--compilation aborted at GasProg line 9.

    Line 9 code> use Date::Format;<code> Please helpw/fix tia

Best practices for handling errors
7 direct replies — Read more / Contribute
by v_melnik
on Sep 27, 2014 at 07:31

    Dear colleagues,

    I think, it's a matter of religion, but I'd like to get to know more on how other people, more experienced, prefer to handle errors/exceptions in respect to the structure of your programs.

    Let me describe how I'm doing it now and, if you have some time to share your experience, I'd be very grateful to you for describing how do you prefer to do it.

    My own "rules" for myself are quite simple.

    1. Don't die() while executing a subrotine or method. Only the main module can die() if something goes wrong. Nobody can predict where the class will be used, so an unexpected die() can break the caller's logic.
    2. If I've got an exception inside of a subroutine, the subroutine may return(undef). If everything's fine, it return's some value (it can be true or false - no matter), but if some error has been occuried (e.g. if we can't get data from the database), the undef shall be returned.

    That's okay, but how to let the caller know what's happened with the subroutine? As I think, the caller must have some explaination to be able to write something to the log-file or to show the error message to the operator. So, there is one more rule.

    1. Any class may have the "errstr" attribute, so if its' methor returned undef, the caller may get the explaination from this attribute.

    So, usually it looks like this:

    package SomeClass; #... sub some_method { # ... eval { die("Oops!"); }; if($@) { $self->{'errstr'} = "Something has gone wrong: $@"; return(undef) } # ... } #... package main; #... my $result = $obj->some_method; unless(defined($result)) { die("Can't SomeClass->some_method(): $obj->{'errstr'}"); } #...

    And, when something goes wrong, I can get something like that:

    Can't SomeClass->some_method(): Can't AnotherClass->another_method(): Can't OtherClass->other_method(): Can't open(): No such file at line 666.

    Frankly speaking, I have a persistent feeling that there are some other, much more elegant way to do it. And I hate how the final error message looks like. Just like "can't A, because can't B, because can't C, because f*** you". Ugh... :(

    And there is another annoying thing: I have to use die() in the constructor of an object (I mean new()), because if the constructor returns undef, the caller doesn't have an access to the object's "errstr" attribute at all (as we don't have the object bless()ed). So I have to always call constructors from eval()-blocks and get the explaination from $@.

    package SomeClass; #... sub new { # ... eval { die("Oops!"); }; if($@) { die("Something has gone wrong: $@"); } # ... } #... package main; #... my $obj = eval { $obj->new }; unless(defined($obj)) { die("Can't SomeClass->new(): $@"); } #...

    I absolutely hate it, but I don't see better ways to let the caller know why the object hasn't been blessed.

    Maybe I should consider using some global variable to keep the reference to the stack of errors occuried? What do you think?

    Maybe I should always die() (or confess() - as a better way to get to know who has called whom) inside of any method and call every method inside of eval()- or try()-block?

    I'll be very grateful to each of you for sharing your best practices on this matter. It really makes me feel unsatisfacted. :)

    Have a nice time!


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 surveying the Monastery: (17)
    As of 2014-09-30 15:32 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (376 votes), past polls