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

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
Simple way to skip spaces and # comments
2 direct replies — Read more / Contribute
by leszekdubiel
on Jul 31, 2020 at 06:43

    I parse string using /gc and \G regexes. Current solution for skipping white characters and comments starting from # to the end of line is:

    $$a =~ /\G(?=(\s|#))(?:\s++|#.*+)++/gc;

    Look ahead ?= assertion is necessary, because zero-length matches cause problems. That solution unfortunatelly gives error:

    Complex regular subexpression recursion limit (32766) exceeded

    I have changes to such one:

    while ($$a =~ /\G(?:\s++|#.*+)/gc) {};

    but this is not elegant. I have changed "+" to "*" after "\s" and this also solves the problem, but I don't know why...

    $$a =~ /\G(?=(\s|#))(?:\s*+|#.*+)++/gc;

    Questions: (1) what is the better solution to strip white chars and commments (2) why * instead of + solves the problem?

How the auto-increment operator works?
5 direct replies — Read more / Contribute
by zapdos
on Jul 30, 2020 at 21:09
    Auto increment and Auto decrement

    With the example print ++($foo = "Az"); # prints "Ba" The logic in my head is that it printed uppercase "B" because it's the letter that comes after the uppercase "A", and it printed lowercase "a" because it's what comes after lowercase "z" going back to the first letter of the alphabet that's "a" since "z" is the lastest.

    So why print ++($foo = "zz"); prints "aaa"? It makes no sense to me. Shouldn't it print just "aa" since there's two z characters?

    2020-08-04 Athanasius shortened link.

no ppm on ActiveState perl?
2 direct replies — Read more / Contribute
by guthrie
on Jul 30, 2020 at 20:39
    I am trying to port a perl program from linux to Windows, and it uses XML::XPath Trying to load it, most advice seems to be to use ppm, but AS doesn't seem to provide that anymore - is that right? And "pip install" fails, as I don't have a pip in the ASPerl installation. Trying to install it from CPAN gives lots of dmake errors like below.

    As does "cpan install"; fails with:

    " MANWAR/XML-XPath-1.44.tar.gz E:\PLang\ActivePerl\bin\perl.exe Makefile.PL INSTALLDIRS=site -- O +K Running make for M/MA/MANWAR/XML-XPath-1.44.tar.gz x[91m[DMAKE] error: the following arguments are required: command. +.[0m ... e:\Plang\Python\python36\scripts\dmake.exe -- NOT OK
    Lots of reports on this sort of thing, but I didn't find anything simple that works yet. Any hints? Should I dump AS perl and move to Strawberry? Please excuse any novice confusion here!
Use cases for 'sub Pckg::func { }' ?
4 direct replies — Read more / Contribute
by LanX
on Jul 30, 2020 at 19:08
    It's possible to define a fully qualified sub by adding the package to the name.

    So I played around with sub X::foo and noticed that sub resolution happens in the surrounding package.

    That looks consistent to me, it's analogue to BEGIN{ *X::foo = sub { } } where the anonymous sub carries its package context around.

    Hence calling foo() will work while bar() fails, because Data::Dump wasn't imported into X::

    use strict; use warnings; use Data::Dump qw/pp dd/; sub X::foo { pp(\@_) }; BEGIN { *X::baz = sub { pp(\@_) } }; package X; sub bar { pp(\@_) } ; foo(1..3); # [1, 2, 3] baz(1..3); # [1, 2, 3] bar(4..6); # Undefined subroutine &X::pp +called

    Question: What are the use cases of that pattern?

    The only thing which comes to mind is monkey patching a sub in another package without adding inner helper functions into that package.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

help with XS pointers
3 direct replies — Read more / Contribute
by Bpl
on Jul 30, 2020 at 18:01
    Hi monkers, in these days I was building an XS library, based on the librtlsdr library ( github link: https://github.com/librtlsdr/librtlsdr ) for now, some functions works fine but the "rtlsdr_open" call gives me some problems. For starting the code (from the github library) referred to the call is:
    RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index); # where rtlsdr_dev_t is initialized as: typedef struct rtlsdr_dev rtlsdr_dev_t;
    I think that my implementation of rtlsdr_dev is wrong, in the XS code I wrote
    int rtlsdr_open(dev, index) RTLSDRDevice_T **dev uint32_t index # where RTLSDRDEVICE_T is typedef rtlsdr_dev_t RTLSDRDevice_T;
    and from the TYPEMAP file the structure are declared as:
    RTLSDRDevice_T * T_PTRREF RTLSDRDevice_T ** T_PTRREF
    now, the problem is that, when I run this code:
    use SDR::RTLSDR qw(:all); # my library my $serial_number = "00000001"; # from dmesg output my $index = rtlsdr_get_index_by_serial( $serial_number ); # it returns + 0, this is not an error value so I think is good my $device = rtlsdr_get_device_name( $index ); print rtlsdr_open( $device , $index)
    I have the following error:
    SDR::RTLSDR::rtlsdr_close: dev is not of type RTLSDRDevice_TPtr at sdr +.pl line 7.
    Now, I think that the result is related to some pointer error but also with:
    print rtlsdr_open( \$device , $index)
    I had the same error code. hope in some helps Thanks Edoardo M.
Find Photos Taken During A Certain Time Range
3 direct replies — Read more / Contribute
by Limbic~Region
on Jul 30, 2020 at 14:51
    All,
    I am looking for a little help with a File::Find::Rule and Image::ExifTool script. My environment is Windows but I only have Perl available via Cygwin. I have a bunch of hard drives in this machine collected over the years so a manual search has proven to be untenable.
    • Search all files with a .jpg or .jpeg extension (case insensitive)
    • Once found, check to see when the photo was taken.
    • If taken in 2015, give me the path to the file and the date the picture was taken

    I haven't really coded in years so while this once seemed like a trivial task, I am having issues getting the expected results.

    #!/usr/bin/perl use strict; use warnings; use File::Find::Rule; use Image::ExifTool qw(:Public); my $tool = Image::ExifTool->new(); for my $file (File::Find::Rule->file()->name( qr/\.(jpeg|jpg)$/i)->in( +'/')) { my $info = ImageInfo($file, 'DateTimeOriginal'); my $date = $info->{DateTimeOriginal}; next if ! $date || $date !~ /^2015/; print "$file\t$date\n"; }

    When I tried it in my home directory as a test, it did exactly what I expected but when I changed '.' to '/' it started generating every file on the system (directories, text files, etc.) without the date.

    Cheers - L~R

Project in Raku
3 direct replies — Read more / Contribute
by ovedpo15
on Jul 30, 2020 at 07:13
    Hello monks!
    Recently I started studying Raku. I didn't find a Raku community so I hope it's ok to ask those questions here.
    After studying Raku for a while I want to do some project using the PL. I want to implement a code coverage utility for Raku. I have some questions:

    1. I may be wrong but it looks like the Raku community is not so active. I want to get information about the community and I'm not sure where to start.
    2. Is there really a use/need of the code coverage utility for Raku? I came across with moarvm and it looks like it provides a way to create a coverage report.
    3. How should I try solving this issue?

    I'm just trying to understand the problem I'm trying to solve, before solving it.
oop, variable that counts the number of objects created
7 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 29, 2020 at 23:21

    I have a package car. What i need is a variable that counts the number of objects derived from this class. Say i have a car rental company and want to keep track of the number of cars being rented, each time a car object is being created means 1 car has been rented. I have been looking on the internet but can't find anything. How can i do this(if possible at all)? Here is the car.pm file:

    use strict; use warnings; package car; sub new { my $class = shift; my $self = { name => shift, price => shift, speed => 0 }; print "Object car being created:\n"; print "Name: $self->{name}\n"; print "price: $self->{price}\n\n"; bless $self, $class; return $self; } sub speed_up{ my $self = shift; my $acc = shift; $self->{speed} += $acc; return $self->{speed}; } sub slow_down{ my $self = shift; my $acc = shift; $self->{speed} -= $acc; return $self->{speed}; } 1;

    Here is an example of a belonging .pl file:

    use strict; use warnings; use car; my $speed; my $auto = new car("Opel Vectra",50000); $speed = $auto->speed_up(25); print "Speed is: $speed km\n"; $speed = $auto->speed_up(10); print "Speed is: $speed km\n"; $speed = $auto->slow_down(20); print "Speed is: $speed km\n";
Not understanding 2 sentences in perldoc
6 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 29, 2020 at 15:13
    The link to the documentation is https://perldoc.perl.org/perlop.html#Assignment-Operators

    "Similarly, a list assignment in list context produces the list of lvalues assigned to, and a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment."

    I don't understand. Can you monks please explain to me and give me examples of what this sentence means?

    "Unlike in C, the scalar assignment operator produces a valid lvalue. Modifying an assignment is equivalent to doing the assignment and then modifying the variable that was assigned to."

    I don't get it either. Please clarify to me I beg.

Help checking program return values
3 direct replies — Read more / Contribute
by Special_K
on Jul 29, 2020 at 12:58

    I have the following code that calls an executable and checks the return value (OS is Linux):

    system($path_to_my_executable); my $exit_value = $? >> 8; my $signal_num = $? & 127; my $dumped_core = $? & 128; # check for segfaults if ($signal_num == 11) { if (!$dumped_core) { printf("ERROR: executable exited with segmentation fault.\n"); exit(-1); } else { printf("ERROR: executable exited with segmentation fault (core + dumped).\n"); } } elsif ($signal_num != 0) { printf("ERROR: executable exited abnormally with signal $signal_nu +m\n"); exit(-1); } elsif ($exit_value == 255) { printf("ERROR: executable exited abnormally with value -1, exiting +...\n"); exit(-1); }

    The original intention of the above code was to catch all segmentation faults from myexecutable and exit the script if one was found. The problem is that my program seg faulted but there was no signal, i.e. $? contained the value 0b1000101100000000. The upper 8 bits had an exit code of 139, which indicates a seg fault, but the signal for seg fault was not set. Consequently my code above did not catch the seg fault. I have two questions:

    1. Do segmentation faults not require that signal 11 be sent? Under what circumstances will signal 11 be sent in the event of a segmentation fault?

    2. Is there a module or library I can use to automatically catch all of the "bad" return values so I don't have to code all of the enumerations of signals and return values myself?


Add your question
Title:
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (2)
    As of 2020-08-05 04:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (35 votes). Check out past polls.

      Notices?