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
Perl module path not found
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 07, 2015 at 09:18
    Hi Monks!

    My code runs fine, I can even run it from the browser and it does what it is supposed do to, but I can’t get it to work running from a “cron job”, I tried everything, I had the "” locally, meaning in the same directory level as the “”, now I have it inside of "lib" directory, but it is still giving me this error:
    Can't locate in @INC (@INC contains: .. /etc/perl /usr/loca +l/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/li +b/site_perl .) at /home/ line 10. BEGIN failed--compilation aborted at /home/ line 10.

    The directory structure looks like this:
    .. /cgi-bin /lib/ /scripts/ ..
    Here is the part of the code:
    #!/usr/bin/perl use strict; use warnings; use CGI qw(escapeHTML); use Data::Dumper; use Date::Calc qw( Today_and_Now ); use lib '..'; # Add private library to path use DBStuff; ####>>>>>>>>>>>>>> This is my line 10 use MyLib::Log qw(do_log); if ($@) { warn "Cron job failed: $@\n"; } my $q = new CGI; $| = 1; BEGIN { my $log_error = 'error.txt'; use CGI::Carp qw(carpout); # Send all warnings to the log_file open STDERR, '>>', $log_error; } ...
    Any suggestions?
    Thanks for looking!
[Moose] extends(...) throws "Subroutine redefined" warnings
2 direct replies — Read more / Contribute
by muba
on Oct 07, 2015 at 08:16

    I have a superclass (MyClass), which creates instances of a subclass on demand. MyClass implements a custom method "get_subclass_instance" for this. The subclass (SubClass) extends 'MyClass';, however, this generates a "Subroutine get_subclass_instance redefined" warning.

    Minimal example:

    # package MyClass; use SubClass; use Moose; sub get_subclass_object { return SubClass->new; } 1;
    # package SubClass; use Moose; extends 'MyClass'; 1;
    $ perl -MMyClass -e 0 Subroutine get_subclass_object redefined at line 5. $

    I understand that extends(...) tries to load the class(es) passed to it. Consequently, if I remove the Extends 'MyClass' line from, the warning disappears.
    But I don't want to remove that line, because SubClass does extend MyClass!

    Of course, I could simply ignore the warning, but that doesn't feel clean to me. So what am I doing wrong, and how can I have MyClass load and instantiate SubClass, while SubClass explicitly extends MyClass?

Dynamically create the hash reference
2 direct replies — Read more / Contribute
by k_manimuthu
on Oct 07, 2015 at 06:16

    Hi All,

    I am trying to create dynamic references in a recursion function. Below I place code which I try, and it return '{}' only. How to get the initial level reference in else part?

    use Data::Dumper; sub insert { my ($ref, $head, @tail) = @_; return unless ( @tail ); if ( @tail and $head ne 'break') { insert( \%{$ref->{$head}}, @tail +) } else { # Here $ref gives '{}' value only. # How to get values of previous head values (ie. f1,f2,f3) print "\n==>", Dumper $ref; # More commands . . . #insert( \%{$ref->{f1}}, @tail ) } } my %hash; while (<DATA>) { chomp and insert \%hash, split( '/', $_ ) ; } print "\nDump\n", Dumper %hash; __DATA__ f1/f2/f3/f4/break/f1/f2/f5/break/f1/f2/f6
Can't use string as an ARRAY ref
1 direct reply — Read more / Contribute
by OldMonkRum
on Oct 07, 2015 at 02:16
    Hi Monks,

    In Getopt I have "tlef:s@" => \$optsHash->{tlef}, which could be single or array. I moved to 5.20.2

    if($self->{tlef}){ if(@{$self->{tlef}}) {

    If tlef is defined once then I want to check if its an array. Previously if if(defined @{$self->{tlef}}) { was working. Now its deprecated, so if I remove define, I get the cant use string as ref issue. Is there an easy way to check if the command line option from GetOpt is array or single element?

Subroutine naming convention/style
4 direct replies — Read more / Contribute
by Cody Fendant
on Oct 07, 2015 at 00:20

    I'm trying to write code to access an API.

    In that API there's an "either add or update" method for an object.

    Whether you're adding or updating that object, you provide an ID. If that ID is found in their database, then you're updating that object. If not, you're adding it.

    What would you call your subroutine which either added or updated a Widget?

    Would you call it: addOrUpdateWidget() because that at least matches their API call (longwinded names are all over the place).

    Would you have two subroutines addWidget() and updateWidget() to make it more transparent what the user was trying to do, and let you give a warning "you thought you were updating a Widget! Turns out you added it instead."?

    Would you call it just widget()?

[OT] HTTP downloads and caching
1 direct reply — Read more / Contribute
by syphilis
on Oct 07, 2015 at 00:00

    A couple of days back I uploaded a new version of Math-Float.ppd. (It's only 4kb, so feel free to download it and tell me if you get the same version of that file as I do - detailed below.)

    I've verified that the upload was successful - if I download it via scp I get the "new" version of the file back, as expected.
    But if I download it in a browser (including IE on Windows, FF on Ubuntu, and IceWeasel on Debian, or if I download it with LWP::Simple, or if I download it using wget, then I get the "old" version of that file.
    The 2 files differ only in that the contents of the "old" file match the string 07 and don't match the string 08 - and vice versa for the "new" version of the file.


    1) If you perform a http download, which version of Math-Float128.ppd do you get ? ... the "old" version that matches 07 (as I do) ? ... or the "new" version that matches 08 (as should happen) ?

    2) Why am I seeing the behaviour that I do ?

    I've deleted histories and emptied the browser caches on both IE and FF.
    And, on the Windows 7 box, I've cleared the DNS cache (ipconfig /flushdns). What's the corresponding command for Linux ?
    Yet I still get the "old" superceded version for each of the above-mentioned http download methods.

    Where can this cache be held ? (The company that hosts the website says it can't possibly be their fault - and when they use wget they get the "new" version of the file.)
    Could it be something that my ISP is doing ?
    What else can I do to try and address this annoyance ?

    Thanks for any help (but also feel free to ignore).

duplicating the rows and joining three lines into one using perl script
2 direct replies — Read more / Contribute
by rpinnam
on Oct 06, 2015 at 21:56
    I have my input csv file as below:



    RC,CNDY,1,5657,WM 85PLY,112,43200

    RC,VN,1,6633,WM 8PL5L,389,2010

    RC,VN,1,1921,WMNS T40PL ,289,2010

    I want my output into two parts. First part is duplicating the entire row of RS header and RAd header with the number that matches with the entire row of RC. Suppose header RC has 3 rows , I want RS and RAd to have 3 rows and then combine each line as below:



    RC,CNDY,1,5657,WM 85PLY,112,43200



    RC,VN,1,6633,WM 8PL5L,389,2010



    RC,VN,1,1921,WMNS T40PL ,289,2010

    Lastly I want to join the above lines of RS,RAd and RC header.Final output should look like the one below:

    RS,52,,,P,,402,,,,,,,,HK,,900,G,,,,,,,1,1,1,CM,,,,,Wearing,,,44,DD,,,JPYK, RAd,CON,,St,567,JPYK,,,Kana,Kana,456,JPKY,,1111111111, RC,CNDY,1,5657,WM 85PLY,112,43200

    RS,52,,,P,,402,,,,,,,,HK,,900,G,,,,,,,1,1,1,CM,,,,,Wearing,,,44,DD,,,JPYK, RAd,CON,,St,567,JPYK,,,Kana,Kana,456,JPKY,,1111111111, RC,VN,1,6633,WM 8PL5L,389,2010

    RS,52,,,P,,402,,,,,,,,HK,,900,G,,,,,,,1,1,1,CM,,,,,Wearing,,,44,DD,,,JPYK, RAd,CON,,St,567,JPYK,,,Kana,Kana,456,JPKY,,1111111111, RC,VN,1,1921,WMNS T40PL ,289,2010

Recommendation on an Excel parser
3 direct replies — Read more / Contribute
by stevieb
on Oct 06, 2015 at 18:52

    This has been asked before, but with so many options out there, I would like current recommendations. I have an MS Excel file (may as well be a csv as there isn't any functions or magic in it) that I need to read and convert into Tex format. Without the ability to first convert to csv, which is the most convenient (least dependencies) Excel module to read in the data? I'll do the conversion myself.



XS: exposing C++ library constant as package variable
1 direct reply — Read more / Contribute
by wisnij
on Oct 06, 2015 at 17:38

    I'm working on a module which is a Perl interface to an existing C++ library at my work. In particular, there's a constant in the C++ header that I want to expose in Perl, rather than duplicating the definition and risking them drift out of sync if someone forgets to update the code in one place or the other. My XS experience to date is limited, but after looking through perlguts, perlapi and some Googling, here's what I've come up with so far:

    package MyModule; use vars qw(@ISA $VERSION $CONSTANT_NAME); BEGIN { @ISA = qw(DynaLoader); $VERSION = '0.01'; $CONSTANT_NAME = -1; } bootstrap MyModule $VERSION;


    #include "my_constants.h" MODULE = MyModule PACKAGE = MyModule BOOT: { SV* const_sv = get_sv( "MyModule::CONSTANT_NAME", 0 ); SvIV_set( const_sv, CONSTANT_VALUE_FROM_C_LIBRARY ); SvIOK_on( const_sv ); SvREADONLY_on( const_sv ); }

    This works in the sense that it compiles and provides the expected value in my simple test one-liner (basically just "use MyModule; print $MyModule::CONSTANT_NAME"). However, I'm not sure whether this is actually the best way to go about it -- in particular, setting $CONSTANT_NAME in the BEGIN block and then changing it again in the XS seems clunky. It's also possible that this is subtly broken in some way that I haven't discerned yet (e.g. threads or something).

    My question, then, is (a) whether this as written is incorrect in any specific way, and (b) whether there's a better approach that I ought to be using instead. What say ye, monks?

checking return from Net::FTPSSL
2 direct replies — Read more / Contribute
by csgonan
on Oct 06, 2015 at 17:34
    I've written a script that uses net::FTPSSL from unix to windows, to upload a file to make sure ftp is working. It works fine. I was trying to generate an error where the file to upload was not found an instead of emailing me an error, it just printed one to the screen that the file wasn't found. I am trying to check the return code from the ftps but I am very new to this. This statement works if it can't put the file up but not if the file doesn't exist. $ftps->put("/temp/testuploadfile.txt") or emailerr("Error: $0 could upload testftpsfile to FTPS server at IP $ftpsip with FTPS p rotocol.", $ftps->$last_message() ); I was researching and checking debug output and see 226 is a successful put, but I don't know how to check that. How do I check the return code? I was trying to use something like this but it didn't work my $rtncode=$ftps->code; if ($rtncode != 226 && $rtncode != 250) { my $msg=$ftp->message; print "FTP Failed: $msg\n"; $ftp->quit; } OR ###print "error $Net::FTPSSL::ERRSTR"; Any help wourl be 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!
  • 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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (15)
    As of 2015-10-07 13:40 GMT
    Find Nodes?
      Voting Booth?

      Does Humor Belong in Programming?

      Results (185 votes), past polls