Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Multiple actions triggered by failure to open a file
1 direct reply — Read more / Contribute
by GreenLantern
on Jan 10, 2017 at 10:54

    Hello Monks,

    I'm having an issue i wasn't planning on running into this morning.... thought i've done it before in the past... but i guess i'm reducing to asking for master-help.

    I want to have multiple actions occur if an open statement doesn't work. Check out my code below and you'll see what i'm talking about.

    thanks!

    #!/tps/bin/perl -s open (my $log_FH, '>', './testlogfile.txt') || { print "failure to open log file..........."; die 'Failure to open log file.....'; } close($log_FH);

    The issue i get from the above is:

    syntax error at ./test.pl line 6, near "die"

    Execution of ./test.pl aborted due to compilation errors.

    i'm running perl, v5.8.3 built for sun4-solaris Thanks folks
Begginer's question: If loops one after the other. Is that code correct?
3 direct replies — Read more / Contribute
by predrag
on Jan 10, 2017 at 08:00

    Hi All, brand new to perlmonks here, from few days ago and it is my first post. I came to Perl last spring (from Linux and bash scripting), really love it, learned a bit and finished some small projects, but still a begginner. My question is: Is it correct to use few if loops one after the other, as I did in this code? So, loops are not nestled and the midle if loop is with else. All that is a content of a foreach loop.

    if ($char eq "a") { some code … } if ($k ==2) { some code … } else { some code … } if ($char eq "b") { some code … }

    I know standard use of loops, but somehow, while trying to make my script work, I've came to this solution, that works well in my case. There is only few similar examples of using if loops such way I've found on the web, so I don't know if it is ok and maybe it is considered something to avoid or a dirty code? If it is ok, I would like to hear your comments about the whole script, but maybe that should be sent with other title. If not ok, I will begin to write the other code.

DateCalc using Date::Manip
5 direct replies — Read more / Contribute
by tsdesai
on Jan 10, 2017 at 04:38
    Hi all, I have an old perl code running from perl 5 version 12 using DateManip and DateCalc on Solaris which runs as it should. We have recently migrated to linux perl 5 version 16 and the DateCalc function doesn't work at all. I am trying to count days between two dates for example, i have tried to output what are the inputs to the DateCalc function
    use Date::Manip; my $dt1='2016080100:00:00'; my $dt2='2016123100:00:00'; my $dtopt= DateCalc($dt1,$dt2); print $dtopt;
    The above code outputs 0:0:0:0:6576:0:0 where as the old version on Solaris outputs as +0:0:+21:5:1:0:0 I am expecting the output to be as old version of perl. I know there is some problem with the format but unable to figure it out. I would really appreciate any help. I have tried to use ParseDate,Delta_Format but the result is same. P.S this is not my code i am trying to fix this. Many Thanks, Teju
Inline::C Undefined subroutine with uint8_t C function parameter
1 direct reply — Read more / Contribute
by stevieb
on Jan 09, 2017 at 15:51

    This is more of a curiosity thing than anything as I must admit, I don't know a whole lot of C.

    Can someone explain what I'm missing in the below code? In the C num() function, I have a uint8_t argument, which causes an error:

    Undefined subroutine &main::num called at perl.pl line 6.

    The broken code:

    use warnings; use strict; use Inline 'C'; num(1); __END__ __C__ #include <stdio.h> #include <inttypes.h> void num(uint8_t number){ printf("%d\n", number); }

    However, if I accept the argument as an int, and then cast it to uint8_t, it works fine:

    void num(int number){ number = (uint8_t)number; printf("%d\n", number); }

    The code that breaks in Perl (uint8_t in the function definition) works fine when compiling/running it as a straight up C program.

"- |" pipe option doesn't work
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 09, 2017 at 10:11
    Dear Monks,

    I've tried to use "-|" pipe option on "perl v5.8.4 built for MSWin32-x86-multi-thread" as below :

    use strict; use warnings; use threads; #sub sub1{- my $pid = open(HCIDUMP, "-|"); defined($pid) || die "can't fork: $!"; if($pid){ while(<HCIDUMP>){ my $test = readline(<HCIDUMP>); print "this is priting from open - $test\n"; } close(HCIDUMP); }else{ exec("sdb shell hcidump") || die "can't exec program: $!"; }
    Then, perl returns
    "'-' is not recognized as an internal or external command,operable pro +gram or batch file."
    Is this usage wrong or just not working in window?

    Thanks Br

    David Lim

How to use Perl_init_debugger in XS code?
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jan 09, 2017 at 06:46
    Monks, I have spent the morning trying to upgrade some working code from 5.8.9 to the latest version of perl (5.22/5.24) and I've hit a mini brick wall. The code is mainly for windows, but it also works under Linux.

    The code turns on the perl debugger in a separate thread at the XS level, this allows the debugger to be used to step through running perl code in that separate thread. It's hard to explain, but works well:)

    It seems (given my limited understanding) that the correct way to do this in later perl's is to use the Perl_init_debugger function, but it doesn't seem to be available? Using the raw source code for this function works well but that would mean that my code would only works on that specific version of perl, something that I want to avoid...

    An example of a module that uses this function is Enbugger:

    https://metacpan.org/source/JJORE/Enbugger-2.016/Enbugger.xs

    However, this module doesn't actually compile under ActiveState 5.22 or Stawberry 5.24

    Enbugger.o:Enbugger.c:(.text+0x852): undefined reference to `__imp_Perl_init_debugger'

    Does anyone know the 'correct' way to use this function?

    Any hints would be gratefully appreciated.

On error, how to show the regexp values?
3 direct replies — Read more / Contribute
by nikolay
on Jan 07, 2017 at 08:50

    In the script

    #!/usr/bin/perl use warnings; use strict; my $sod='ip6 is used for...'; my $i=0; my @baza_tek=qw( (?^u:ip(\d)) "internet_protocol-$1$2" ); $sod=~s#$baza_tek[$i]#$baza_tek[$i+1]#gee;

    i have such an error on execution:

    Use of uninitialized value $2 in concatenation (.) or string at...

    My question is,

    How to tell PERL to show the values of current baza_tek[$i] or $baza_tek[$i+1] at the time, when the error occured?

how to improve: use MODULE VERSION LIST
4 direct replies — Read more / Contribute
by smile4me
on Jan 06, 2017 at 19:49

    I curate a repository of cpan modules @WORK that is shared across the enterprise. Unfortunately, I am trying to serve two masters: "run the biz" and "change the biz". The RTB folks don't like changes to their environment, and fight vehemently when modules upgrade (b/c they have production systems dependent upon old code). By contrast, the CTB folks love the bleeding edge and want to try new things, build new tools, etc.

    So, when I built Perl for the next operating system, I added a new directory to the default @INC where I can put additional, non-core, modules (or new versions of existing modules) for the CTB folks. This works fine if the "newer version" of a module comes earliest in the @INC list. But what happens when something in the corelist is updated? DHOH!

    So after much GIYF, I found this very troubling statement:

    "Please recall that Perl interpreter will STOP trying to search [additional directories in @INC] once it finds the file in one of the locations, without trying to see if the file is in later locations as well. E.g. if /usr/lib/perl/This/Here/MyModule2.pm exists, then Perl will not look for, nor care about the existence, of /opt/custom/lib/This/Here/MyModule2.pm." Stack Overflow

    Is there a better solution than "use lib reverse @INC" to get the newer module that is further in the @INC list?

    For example, autodie v.2.23 is in the core for perl. So I build the newest autodie v.2.29, and it now lives in the non-core extras modules path, which is part of the default @INC.

    --> perl -MData::Dumper -E 'say "INC: ", Dumper( \@INC )' INC: $VAR1 = [ '/usr/foss/packages/perl/perl-5.20.3/lib/site_perl/5.20.3/x8 +6_64-linux-thread-multi', '/usr/foss/packages/perl/perl-5.20.3/lib/site_perl/5.20.3', '/usr/foss/packages/perl/perl-5.20.3/lib/5.20.3/x86_64-linux +-thread-multi-ld', '/usr/foss/packages/perl/perl-5.20.3/lib/5.20.3', '/usr/foss/packages/cpan-basics/perl-5.20.3/lib/perl5/x86_64 +-linux-thread-multi-ld', '/usr/foss/packages/cpan-basics/perl-5.20.3/lib/perl5', '/usr/foss/packages/cpan-basics/perl-5.20.3/lib/perl5/site_p +erl', '/usr/foss/packages/foss-perllib', '.' ]; --> perl -E 'use autodie; say q[version: ], $autodie::VERSION; say q[location: ], $INC{q[autodie.pm]}; ' version: 2.23 location: /usr/foss/packages/perl/perl-5.20.3/lib/5.20.3/autodie.pm --> perl -E 'use lib reverse @INC; use autodie; say q[version: ], $autodie::VERSION; say q[location: ], $INC{q[autodie.pm]};' version: 2.29 location: /usr/foss/packages/cpan-basics/perl-5.20.3/lib/perl5/autod +ie.pm --> perl -E 'use autodie 2.29; say q[version: ], $autodie::VERSION; say q[location: ], $INC{q[autodie.pm]}; ' autodie version 2.29 required--this is only version 2.23 at -e lin +e 1. BEGIN failed--compilation aborted at -e line 1.

    So again, I ask: is there a better solution than "use lib reverse @INC" to get the newer module that is further in the @INC list? Is there a way to change the module search heuristic in Perl so that all the locations are scanned when you specify a VERSION (and/or a LIST)? Am I missing a new feature that solves this problem?

    TIA for your wisdom and insights.

Perl modules hierarchy and composition
4 direct replies — Read more / Contribute
by tusker
on Jan 06, 2017 at 05:57

    Reading more and more about Perl, I'm having doubts about how I organized my modules on my current project.

    I have a main namespace - let's call it "MyProject". I have objects from class A that are composed of objects of class B, C and D. Classes B, C and D are used by class A only, so it made sense to me to organize the modules as follows:

    • MyProject::A
    • MyProject::A::B
    • MyProject::A::C
    • MyProject::A::D

    But I'm starting to think that it will make sense to me only if I ever dare to relase parts of my project in CPAN. I think people will believe that B, C and D are inheriting A and not composing it.

    So: shall I reorganize my modules this way?

    • MyProject::A
    • MyProject::B
    • MyProject::C
    • MyProject::D

    Or maybe indicate that B, C and D are parts of A by appending their names?

    • MyProject::A
    • MyProject::AB
    • MyProject::AC
    • MyProject::AD

    My question: What is the currently considered a best practices for module organization and naming when it comes to composition?

DynaLoader and data types
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jan 06, 2017 at 00:40
    Hi, All. Is there any way to process data-types defined in .so library ? I've used code like this :
    package SO; use DynaLoader; sub new { my $class = shift; my $libname = shift; my $so = DynaLoader::dl_findfile( $libname ); die 'Failed to find .so for '.$libname unless defined( $so ); my $lib = DynaLoader::dl_load_file( $so, @_ ); die 'Failed to load '.$so unless $lib; return bless [ $lib, $so ], $class; }; sub AUTOLOAD { my $self = shift; my $sub = $AUTOLOAD; $sub =~ s/.*\:\://; warn 'called DL'; return $self->call( $sub, @_ ); }; sub DESTROY { my $self = shift; DynaLoader::dl_unload_file( $self->[0] ); }; sub call { my $self = shift; my $sym = shift; my $symref = DynaLoader::dl_find_symbol( $self->[0], $sym ); die 'failed to find '.$sym.' in '.$self->[1] unless $symref; DynaLoader::dl_install_xsub( $sym, $symref ); return &$sym( @_ ); }; 1;
    ------ I've tried to install C::DynaLib but get failed on tests in "make test". At now I'm looks over code of C::DynaLib and many others. Showed ecample works good with method which is like that
    use SO; my $so = SO->new('some.so'); my $string = $so->get_some_string();#correct result my $x = { a => 'b', c => 123 }; my $r = $so->change_this_argument( $x );#get error from .so about wron +g arguments, but the arguments definetly correct. my $r2 = $so->accept_this_argument( $x );#get same error.
    My question is how I should to repack $x or it's components to make them clear for original .so library. .so library is third party library which sources I have but will not like to change ( it comes from yum official repositories ).
Converting -4.84800000E+001 to -48.48 doesn't work... but 48.49 does
4 direct replies — Read more / Contribute
by notameadow
on Jan 05, 2017 at 15:30
    Hello, I have just spent better part of the day trying to work this out. It looks like a bug, but that would be quite interesting in itself. Try this:
    perl -e '$a = "-4.84900000E+001"; $a += 0; $b = -48.49; print "EQ\n" i +f ($a == $b); print "$a\n$b\n";'
    it gives:
    EQ -48.49 -48.49
    All good. Now, try this:
    perl -e '$a = "-4.84800000E+001"; $a += 0; $b = -48.48; print "EQ\n" i +f ($a == $b); print "$a\n$b\n";'
    gives:
    -48.48 -48.48
    ? In my main code I am getting scientific notation values as strings, I add 0 and compare them as numbers. It all works, but not for -4.84800000E+001 (and 4.84800000E+001 too). Why? I tried some combinations like 37.37 and so on.
    perl -v This is perl 5, version 22, subversion 2 (v5.22.2) built for x86_64-li +nux-thread-multi (with 33 registered patches, see perl -V for more detail)
    I really do hope this is something silly on my part.
Update Sqlite References
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 05, 2017 at 15:16

    Dear Monks

    Given the following snippet which adds a record into a simple Sqlite Database perfectly working

    my $Tag1="hallo"; my $Tag2="bye"; my $dbh = DBI->connect( "dbi:SQLite:$DbFullPath", '', '', { sqlite_uni +code => 1 } ) || die "Cannot connect: $DBI::errstr"; $dbh->do('INSERT INTO Data (Tag1, Tag2) VALUES (?, ?)', undef, standardize($Tag1), standardize($Tag1)); $dbh->disconnect; sub standardize{ my $string=shift; #manipulating string $string=~ s/\'/\'\'/g; $string=~ s/^\s+|\s+$//g; $string=~ s/\t/ /g; return $string; }

    Why is the following (updatin an entry) not working? (Error message "syntax error")

    my $ID_read=1; my $dbh = DBI->connect( "dbi:SQLite:$DbFullPath", '', '', { sqlite_uni +code => 1 } ) || die "Cannot connect: $DBI::errstr"; $dbh->do ('UPDATE Data SET Tag1=?, Tag2=? WHERE ID=$ID_read VALUES (?, + ?)', undef, standardize($Tag1), standardize($Tag1)); $dbh->disconnect;
New Cool Uses for Perl
relink: Rewriting Symlinks with Perl
No replies — Read more | Post response
by haukex
on Jan 04, 2017 at 13:01

    Fellow Monks,

    I've just made the first public release of a tool I've been working on called relink (Update: it's GPLv3), and I wanted to tell you about it in the form of a short narrative, below. But first, just to whet your appetite:

    # Rewrite the targets of symlinks with Perl $ relink rewrite 's/foo/bar/' PATH(s) # Convert between absolute and relative targets $ relink abs2rel PATH(s) $ relink rel2abs PATH(s) # List links [only certain symlinks] $ relink list [-t '/foo/'] PATH(s)

    Regards,
    -- Hauke D

New Monk Discussion
Orphaned Page, Lost Content, Other Issues
2 direct replies — Read more / Contribute
by kcott
on Jan 08, 2017 at 00:05

    I just wrote a reply to "Re^2: how to improve: use MODULE VERSION LIST" (id://1179142). I previewed twice then created. All looked good and perfectly normal up to this point.

    When the "created" page appeared, I followed the link to the start of the thread: "how to improve: use MODULE VERSION LIST" (id://1179107). This appeared but, when I scrolled down, my post was nowhere to be seen.

    I went to Newest Nodes and a post by me was at the top of the Notes list. I followed that link "Reaped: Re^3: how to improve: use MODULE VERSION LIST (id://1179154) but found an orphaned page (i.e. no "in reply to" or "in thread" links at the top). The page also had no content. It did have what appeared to be the correct title ("Re^3: how to improve: use MODULE VERSION LIST"): small consolation.

    I went back to the tab where I'd created my response. I hit the back button and the content of my response was there BUT it said "by 1nickt" followed by "on Jan 08, 2017 at 09:00 AEDT ( #1179146=note: print w/replies, xml )". Note, that ID (1179146) is different from the one in Newest Nodes (1179154).

    I followed that link: it was an earlier reply by 1nickt to a different node in the same thread.

    Hitting the back button again (on the tab where I'd created my response) took me back to my final edit prior to posting. I've saved the content: I can post it again when the problem's fixed.

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[choroba]: Why do you think they don't meet the spec?
[choroba]: link would definitely help
[james28909]: but every verification/ parser i have ran it though has failed at '&'
[choroba]: & is special in XML, it starts an entity. To insert it literally, use &amp;

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-01-10 21:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (95 votes). Check out past polls.