Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Think about Loose Coupling

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
Can a defined value be of zero length?
6 direct replies — Read more / Contribute
by j0se
on Apr 19, 2014 at 05:06

    Dear Monks, why would I need to check for the length of the string? Is it not enough to check for the "definedness"?

    use strict; use warnings; my $dir = '/etc'; while (1) { print "Enter pattern> "; chomp( my $pattern = <STDIN> ); ## I'm talking about this line last unless defined $pattern and length $pattern; my @matched = eval { grep { /$pattern/ } glob "$dir/*"; }; print "Error: $@" if $@; print map { "$_\n" } @matched; }

    And like that ... he's gone. -- Verbal

Beginner's guide for Perl Dancer.
4 direct replies — Read more / Contribute
by pmu
on Apr 19, 2014 at 03:17

    Respected Monks,

    I have been through the Dancer documentation and have searched the net as well. Yet, I do not come across a document that explains Dancer in complete details or something that written for folks who know basic Perl.

    I have some basic Perl Scripting knowledge created a few scripts that capture, filter and e-mail outputs of certain commands. I now want to put the same reports on a webpage, where people can type in the serial number of the array and get the alerts. I want to build a webpage wherein, say for example, a user can put in the name of a server and get the details such as how much storage its accessing and from which storage arrays etc etc.

    I also feel that my lack of knowledge is preventing me from understanding the basic dancer concepts. So how do I go about it?

    Any pointers, suggestions would really help.

    -------------------------------------------------------------- Perspectum cognitio aeterna --------------------------------------------------------------
The 'eval "require $module; 1"' idiom
3 direct replies — Read more / Contribute
by sedusedan
on Apr 19, 2014 at 01:04

    Could someone refresh me on why the ; 1 part is necessary again? In other words, why is this idiom safer/better/more correct than just eval "require $module", especially in the context of using the expression in conditional (e.g.: eval "require $module; 1" or warn "Can't load $module";). I've used this idiom for years, and recently even submitted a patch to Log::Log4perl related to this. But for the love of heaven, I forgot why it's needed in the first place. Let's consider the cases:

    Case 1. Module cannot be not found on the filesystem. require dies with message "Can't locate $module in @INC ...", eval traps it and sets $@ and returns undef. ; 1 part in idiom is not needed (unreached).

    Case 1b. Like case 1, but previous attempt has been made. Ditto.

    Case 2. Module has compile errors. require dies with compile error message, eval traps it and sets $@ and returns undef. ; 1 part in idiom is not needed (unreached).

    Case 2b. Like case 2, but previous attempt has been made. After the first attempt, the entry in %INC is set to undef. At the second attempt, require will die with message "Attempt to reload $module aborted", eval traps it, sets $@, and returns undef. ; 1 part in idiom is not needed (unreached).

    Case 3. Module is loaded successfully and returns a true value (in other words, the normal success case). require will return 1. ; 1 part in idiom is not needed.

    Case 3b. Like Case 3, but previous loading has been made. require will return 1. ; 1 part in idiom is not needed.

    Case 4. Module is loaded successfully but returns a non-true value. require will die with message "$module did not return a true value ...". eval traps it, sets $@, and returns undef. ; 1 part in idiom is not needed (unreached).

    Case 4b. Like case 4, but previous attempt has been made. Ditto.

    There are other cases possible (like a permission error when reading source file, require() being overriden, etc) but the above are the common/majority cases. So what cases does the ; 1 part cover? Does this have something to do with older versions of perl (I'm testing this on 5.18.2).

Will script terminate with window closing
2 direct replies — Read more / Contribute
by htmanning
on Apr 19, 2014 at 00:28
    Monks, I have a database of 100 email addresses. I wrote a script that will present a form to be filled out. When the form is submitted, the contents get emailed to all email addresses through a while routine. My question is if I present a "success" HTML page after the form is submitted, will the script continue to run and complete the sending of all emails if the user closes the browser window or clicks to another page? If I make them wait until the emails are sent it could take a long time so I'd rather just present the success page. Thanks.
accessing subroutines of a module
2 direct replies — Read more / Contribute
by ghosh123
on Apr 18, 2014 at 23:20

    is it possible to access sub routines of a module without using Exporter and creating object ?

Printing without a newline?
3 direct replies — Read more / Contribute
by system("clear");
on Apr 18, 2014 at 20:51

    I am building a simple timer script and I wanted to make a loading bar that corresponded to the time. But unfortunately I can't get it to print without making a new line Thanks... I'm not to good a perl

    #!/usr/bin/perl use Math::Round qw(:all); print "Minutes?\n"; $minutes = <>; chop $minutes; $timeleft = 60*$minutes; system("clear"); while($timeleft >= 0){ $devided = $timeleft/60; $rounded = nlowmult (1, $devided); $seconds = $timeleft-($rounded*60); print "$rounded minutes and to $seconds seconds left\n"; $ran = 30/$timeleft; $count = $ran*$timeleft; while ($count >=0 ){ print "#"; $count--; } sleep (1); system("clear"); $timeleft--; } print "TIME UP!!!!!!\a"; while (1){ print "\a"; print "\a"; }
How to tell cpan where a missing library is?
1 direct reply — Read more / Contribute
by cantelow
on Apr 18, 2014 at 15:31

    On redhat-

    I'm coming up unsuccessful trying to install the SAVI module with the command:

    cpan SAVI

    -probably because of this message when I try:

       Warning (mostly harmless): No library found for -lsavi

    But is available at /opt/sophos-av/lib/ Can someone point me to how to tell cpan where this is? I tried softlinks in /usr/lib and /lib pointing to /opt/sophos-av/lib/, but that doesn't do the trick.

    Thanks tons!
Error running prove "Archive is not a known format type! at /usr/lib/perl5/5.8.8/App/ line 538."
2 direct replies — Read more / Contribute
by kamal
on Apr 18, 2014 at 15:19
    When i try to run prove, i get error, here is the command and stdout:
    find test/ -name '*.t' ! -name '*falcon*' | xargs prove -v --norc -r t +est/ -formatter=TAP::Formatter::JUnit | tee test_results.xml Archive is not a known format type! at /usr/lib/perl5/5.8.8/App/Prove. +pm line 538.
[Resolved]Perl inheritance
4 direct replies — Read more / Contribute
by ivanovic3001
on Apr 18, 2014 at 14:27
    Dear anyone,

    I need some serious help. Been trying to figure out inheritance. Was given an assignment to practice inheritance. the code given was.

    use v5.10.0; use warnings; use strict; package Animal; sub new { my $class = $_[0]; my $self = {text => "I am an abstract animal.\n"}; return bless($self, $class); } sub speak { my $self = shift; print $self->{"text"} } package Duck; our @ISA = qw(Animal); # qw(Animal) je ekvavilentno ("Animal") sub new { return bless({text => "I am a a duck.\n"}, shift); }

    So this is the given code and I'm just supposed to make another package named Cow, Horse, etc. However reading online this is different from the stuff I had and the code doesn't print out anything when running and testing it. From what I gathered it's supposed to print "I am a duck" but it does nothing at all.

    I apologize in advance if this is a newbie question but i really can't figure it out tried asking my peers but no one new anything. Thanks in advance for any help.

Simple algebraic calculations
8 direct replies — Read more / Contribute
by wanna_code_perl
on Apr 18, 2014 at 13:33


    Thanks for all the replies. Here's a bit more information to answer (some) of your questions and comments:

    This is not (really) a code factorization problem. Sure, purists will say "but, you can factor anything into a sub!", and basically rewrite Mathematica, but that would be a bit extreme for my purposes.

    It's also not that I keep writing very similar half-life routines, or acceleration routines, or "which brand/size of cola is the cheapest", whatever... it's more the meta issues of how to represent units (and convert, automatically, when you take something like 9.8 m/s/s * 1 hour, how fast am I going?) By themselves, most of these things are simple, but en masse, and in combination, they're still "simple", but inelegant in Perl (or most any other general purpose language for that matter).

    moritz and others, helped me decide that what I want is indeed probably a full symbolic math engine. It's either that or "factor" every formula and snippet I use into something that I'd probably end up being tempted writing a symbolic parser for anyway, which would be silly.

    Original post follows.

    Hello monks,

    Over and over, I find myself writing little Perl scripts to do relatively simple calculations (but usually too many steps to be reasonable for a conventional calculator or spreadsheet). Management (and conversion) of units would be very important. Short of using a dedicated math language like Mathematica (or one of the free ones), is there anything in between, so I can still take advantage of Perl syntax for output, loops, CPAN modules, etc.?

    Just one example of something I'd calculate would be various properties of the Half-life of a substance. Using the usual formulae, I'd want to calculate how much of the substance is left after N days (given a half life in hours), and then, given regular input of more substance, how long to reach a steady state. I won't bore you with the math, here; it's not especially difficult. See the above link for more information.

    Writing pure Perl for this isn't difficult, but it is tedious and repetitive, especially given management of different units and conversions between.

    My apologies for the rather vague specification of what I'm looking for, but that's part of the problem: I've thought about this more than once, and don't have a clear idea, aside from hiring a college student. :-)

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 examining the Monastery: (5)
    As of 2014-04-19 18:37 GMT
    Find Nodes?
      Voting Booth?

      April first is:

      Results (483 votes), past polls