Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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.

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.

Post a new question!

User Questions
fetchall_arrayref hangs with placeholders query with no results
2 direct replies — Read more / Contribute
by TieUpYourCamel
on Apr 15, 2021 at 10:39
    I have a situation with DBI and the Sybase drivers. If I specify a TDS level, and I use a query with placeholders, and that query returns no results, the fetchall_arrayref() call hangs. If I don't do all three of those things, the call returns an array, as expected.

    Steps to reproduce on Ubuntu 20.04:

    Install perlbrew

    sudo apt install gcc make -y perlbrew install perl-5.33.8 perlbrew switch perl-5.33.8 perlbrew install-cpanm
    Install FreeTDS
    wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.2.20.tar.gz tar xfvz freetds-1.2.20.tar.gz cd freetds-1.2.20/ ./configure --prefix=/home/username/tdslib make make install
    Install DBI and Sybase
    export SYBASE=/home/username/tdslib cpanm install DBI cpanm DBD::Sybase --verbose --force
Mojolicious-Hypnotoad-Nginx - rendering images
4 direct replies — Read more / Contribute
by pdkakoba
on Apr 15, 2021 at 09:24

    Kindly seeking assistance in rendering images (any static file from /public) in a default Mojolicious-Hypnotoad-Nginx setup. Following the standard guides, this setup successfully renders the default welcome page. However adding one exception: a .png image to this page, it is not rendered, even the favicon.ico doesn't appear.

    There is mention of "hooks" needed to correct for the workings of a Reverse Proxy, but after a long search one doesn't find a clear full example -- particularly for Nginx.

    The welcome.html.ep has this simple addition: tree. It appears alone as an image when one enters this url: http://example.com/tree.png

SerialPort issues under Windows 10
2 direct replies — Read more / Contribute
by clinicalAD
on Apr 15, 2021 at 06:46
    Hello, Am trying to use Win32::SerialPort on a portable strawberry perl implementation, version 5-32-1. (32 bit). Using it to try and set up a datalogger on a Win10 machine, for an instrument that reports through a RS232 output. Also have a general test bed which is two windows PCs connected by a serial cable, and one sending messages (from Powershell) to the other - which testing has shown acts in the same way as the end use. Have come across a variety of scripts online. For some reason though, I can't get any Perl script to output the RS232 input on its own. However, if I open and close the port in powershell, suddenly the exact same Perl script DOES work! The perl script continues to work as expected until the PC is rebooted. So I'm wondering if there's a key parameter needing to be set, which is sorted by Powershell but not by Perl? Couldn't see anything obvious in the SerialPort documentation. An example script that only works when powershell has "activated" the port is:
    use Win32::SerialPort; # Use instead for Windows my $port=Win32::SerialPort->new("COM1") || die "couldn't open COM1"; $port->baudrate(9600); # Configure this to match your device $port->databits(8); $port->parity("none"); $port->stopbits(1); #$port->handshake("none"); $port->user_msg(ON); print "Port COM1 successfully opened\n"; $count = 0; my $chars=0; my $buffer=""; print "1\n"; do { print "2\n"; my ($count,$saw)=$port->read(255); # will read _up to_ 255 char +s print "3\n"; if ($count > 0) { $chars+=$count; $buffer.=$saw; print $saw; } print "4\n"; $port->lookclear; print "5\n"; } while 1; $port->close;
    This code stops after printing "2" if I've not opened/closed the port in Powershell yet - it's the reading of RS232 input where the script seems to get blocked quite consistently across different scripts. Powershell code to open and close is:
    $port = new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one $port.open() $port.close()
    Powershell to send RS232 on test bed is: $port.WriteLine("test") Unfortunately access permissions do not let me run a powershell script, and I'd rather not have the end user have to manually type in powershell code each time the PC is turned on... Many thanks for any advice provided.
Passing a value to Mojo::Template template
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 14, 2021 at 12:49
    Hi Monks!

    Trying to pass a variable value to the template from the Perl file:
    From the .ep file:
    <div> Number<%= $data->{ box } %> Test: <%= $test %> </div>

    The Perl file:
    ... my $mt = Mojo::Template->new( vars => 1, auto_escape => 1 ); my $data = ... # Not where the issues is. my $code = "PERL"; print $q->header( -charset => 'utf-8' ), $mt->render_file( myfile.html +.ep, { data => $data }, { test => $code } ); ...

    I am getting this error:
    Global symbol "$test" requires explicit package name at myfile.html.ep

    Cant get why, is there a better way of doing this?
    Thanks for taking a look!
Crypt-Random-Source: 0.14 fails to install
2 direct replies — Read more / Contribute
by chandantul
on Apr 14, 2021 at 09:02

    Hello All, I am getting the following error Crypt-Random-Source: 0.14 fails to install while installing OIDC::Lite. I am in Windows 10 platform

    Please check below errors

    t/sugar.t .............. 1/? Undef did not pass type constraint "Class +Name" (in $self->{"weak_source"}) at (eval 121) line 17 "ClassName" is a subtype of "Str" "Str" is a subtype of "Value" "Value" is a subtype of "Defined" Undef did not pass type constraint "Defined" (in $self->{"weak_sou +rce"}) "Defined" is defined as: (defined($_)) # Tests were run but no plan was declared and done_testing() was not s +een. # Looks like your test exited with 255 just after 3. t/sugar.t .............. Dubious, test returned 255 (wstat 65280, 0xff +00) All 3 subtests passed Test Summary Report ------------------- t/factory.t (Wstat: 65280 Tests: 1 Failed: 0) Non-zero exit status: 255 Parse errors: No plan found in TAP output t/sugar.t (Wstat: 65280 Tests: 3 Failed: 0) Non-zero exit status: 255 Parse errors: No plan found in TAP output Files=7, Tests=17, 4 wallclock secs ( 0.11 usr + 0.09 sys = 0.20 CP +U) Result: FAIL Lockfile removed. ETHER/Crypt-Random-Source-0.14.tar.gz C:\Strawberry\perl\bin\perl.exe ./Build test -- NOT OK //hint// to see the cpan-testers results for installing this module, t +ry: reports ETHER/Crypt-Random-Source-0.14.tar.gz Stopping: 'install' failed for 'Crypt::Random::Source'. Failed during this command: ETHER/Crypt-Random-Source-0.14.tar.gz : make_test NO

    Can you please help to fix?

Breaking symmetry with a regex
6 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 13, 2021 at 18:50

    I have a string that has the form 'a:b:' where 'a' and 'b' are both numbers. How do I drop the second colon, or convert it to // ?

Should I use v5.10 because of named groups?
5 direct replies — Read more / Contribute
by hrcerq
on Apr 13, 2021 at 18:11

    Hello again.

    I've got a script that uses named capture groups to parse file records. The record format is always the same (four fields wide, colon separated), just like a passwd file (except for the number of fields).

    For example:

    0:1:bob:Bob
    1:1:bob:Bob
    1:0:alice:Alice
    

    You get the idea. At first I used to split it, but later I came to the conclusion that using a regex with named groups could provide a more understandable code. The (simplified) example below might help you get the picture.

    if (/^(?<type>[01]):(?<valid>[01]):(?<name>[^:]+):(?<comment>[^:]+)$/) + { print "Key name: ", $+{name}, "\n"; print "Key Comment: ", $+{comment}, "\n"; print "Not valid\n" unless $+{valid} || !$+{type}; } else { print "Malformed input: $_\n"; }

    Now, I've noticed (according to perlretut) that named capture groups were introduced in Perl 5.10. I don't expect the script to run in older versions, but would it be wise to use v5.10 pragma?

    Would it have any unwanted side effects? I'm not using any of the features of the bundle, just wanted to state that version 5.10 is required. Is that a good idea?

printing metadata Tag using ExifTool
1 direct reply — Read more / Contribute
by g_speran
on Apr 13, 2021 at 15:24

    Hello Perl Monks

    I am trying to use ExifTool to extract some Metadata TAGS. I've searched for a specific use case but was unsuccessful. Ive constructed some simple code to accomplish what I am trying to do, but I am looking for verification that I am doing it correctly to extract the necessary variables or is there a simpler way.

    use Image::ExifTool qw(:Public); use Data::Dumper; $file='d:/60486581538__715F9238-09E5-4806-8146-0626DAFB0E36.JPG'; $ExifTool = new Image::ExifTool; #$ExifTool->Options(DateFormat => '%Y-%m-%d'); $info = $ExifTool->ImageInfo($file,'SubSecCreateDate','LensModel'); #$info = $ExifTool->ImageInfo($file); print "INFO: $info\n"; $createdate=$$info{'SubSecCreateDate'}; $lens=$$info{'LensModel'}; print "\nCreate Date: $createdate\n"; print "Lens Model: $lens\n\n"; print Dumper($info); exit; D:\>metadata.pl INFO: HASH(0x2b9f148) Create Date: 2020:03:02 13:16:55.336-05:00 Lens Model: iPhone 7 back camera 3.99mm f/1.8 $VAR1 = { 'LensModel' => 'iPhone 7 back camera 3.99mm f/1.8', 'SubSecCreateDate' => '2020:03:02 13:16:55.336-05:00' }; D:\>
XML::XPath matching different file extension
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 13, 2021 at 11:27
    Hi there Monks!

    I have this line in my code and I am trying to see if there is a way to add more than one file match in this line, the files comes as "*.txt", "*.TXT", "*.png", "*.PNG".
    It breaks if in this line the file extension is "*.TXT" instead of "*.txt".
    my @files = $zip->membersMatching( '.*\.txt' );

    Thanks for looking!
Useless use of string in return statement
7 direct replies — Read more / Contribute
by Bod
on Apr 12, 2021 at 19:06

    Is there something strange about the way return treats conditions?

    I have this and it doesn't work as expected...

    sub get_ids { my ($self, %attrs) = @_; # Do stuff... my %result; # $result{'message'} = ''; if ($self->{'error'}) { $result{'status'} = 'error'; $result{'message'} = $self->{'error'}; } else { $result{'status'} = 'success'; $result{'api-key'} = $self->{'api-public'}; $result{'session'} = $intent_id; } return encode_json(\%result) if lc($attrs{'format'}) eq 'json'; return $result{'message'} or "$result{'api-key'}:$result{'session' +}"; # <- line 229 return "SOMETHING"; }
    If it is called as get_ids( 'format' => 'json' ); it works fine but asking it to return a text string returns undef and warns Useless use of string in void context at line 229. The way I think it should work is that if $result{'message'} evaluates as true, that will get returned but if it evaluates as false then "$result{'api-key'}:$result{'session'}" wil be returned instead.

    Can you explain why this is not behaving as expected?

    As an aside, in searching for an answer I found this post -> Useless use of string in void context
    There it is suggested that Perl reports the wrong line number for this warning so it is quite possible that I'm actually looking in the wrong place!


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 romping around the Monastery: (5)
    As of 2021-04-15 23:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?