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
The error says the value is uninitialized, but it works anyway
1 direct reply — Read more / Contribute
by mizducky
on Aug 17, 2019 at 05:29
    This is a homework assignment, and it works. If I use strict, I don't get an error, but if I use warnings, it says "Use of uninitialized value in string eq at line 7", it prints this error twice (I assume its for the two times that the match is found).
    Here is the code I am turning in for my assignment, but I still want to know why its giving me those errors, and if I should do something better. So I'm really just hoping for someone to help me understand what is happening to cause this error.
    my @colors = qw(red green blue yellow pink purple brown); my $count = @colors; my @drop = qw(pink brown); my $num = 0; foreach $num (1..$count){ $num--; if ($colors[$num] eq $drop[0] or $colors[$num] eq $drop[1]){ splice (@colors, $num, 1); } } print "@colors \n";
Best practice for a module which can use one of multiple image modules
3 direct replies — Read more / Contribute
by Cody Fendant
on Aug 17, 2019 at 02:06

    I'm trying to write a module which turns images into data.

    I've got it working with Image::Magick but what if I wanted it to work with that or GD::Image, depending what the user has? Plus other things as well maybe.

    Should I try to detect what's on the system? Should I make MyModule::ImageMagick and MyModule::GD and make the user choose?

    What's the right way to do this kind of thing?

How to printout a Gtk2::ComboBox
3 direct replies — Read more / Contribute
by dabella12
on Aug 16, 2019 at 14:30

    Hello monks i'm trying to printout all the text in a Gtk2::ComboBox. I have searched and can not find how to do this anywhere in perl. in python all i have to do is

    model=combobox.get_model() for row in model: print row[0]
    but when i try this in perl it doesn't work. Any help/suggestion would be appreciated

Portable way to determine if two names refer to the same file?
1 direct reply — Read more / Contribute
by jcb
on Aug 15, 2019 at 23:31

    I have spent the past hour or so trying Super Search and not finding a clear answer, so I ask my fellow monks how best to portably determine if two seemingly different names actually refer to the same physical file?

    I am not concerned about copies of the same file, only links, such that the same physical file appears under multiple names.

    On POSIX, the solution is easy: compare dev:ino tuples from the stat builtin and declare "same file" if they match. I have no idea if this also works on Windows or even if the problem exists on Windows — how well does Windows handle symlinks anyway and does it even support hardlinks at all?

    And what of the less-common platforms?

    Cross-posted in Categorized Questions and Answers at How do I portably determine if two filenames refer to the same file? as a place to collect answers for future reference.

    Edited 2019-08-16 by jcb: Clarify that stat refers to the Perl builtin. I had forgotten about the shell command with the same name.

List comparison problem
2 direct replies — Read more / Contribute
by perlmonkster
on Aug 15, 2019 at 22:12

    Hello

    I somehow lost a final version of a simple Perl script to compare 2 lists, and cannot seem to figure out what is causing a problem with the version I have.

    The simple .pl script takes ListL.txt, compares it to ListH.txt, and then "Flags" any entries from ListH.txt that are on ListL.txt (plus gives two separate Counts at the bottom of the output). Using two short sample lists, the Counts are both correct, but for some reason one of the ListL.txt items that *should* show a ListH.txt "Flag" in the output does not. I've tried switching around Count statements, etc., but am completely baffled. Any insight as to how to fix things would be greatly appreciated.

    Here's the short code

    use strict; use warnings; my %H_list; open my $H_list, '<', 'listH.txt' or die "Cannot open listH.txt: $!"; + while (my $line = <$H_list>) { chomp $line; $line =~ s/\r//g; # removes windows CR characters $line =~ s/\s+$//; # removes trailing white spaces $H_list{$line} = 1 } close $H_list; my ($L_count, $H_count); open my $L_list, '<', 'listL.txt' or die "Cannot open listL.txt: $!"; + while (<$L_list>) { chomp; s/\r//; s/\s+$//; $L_count ++; print; $H_count ++ and print ' On List H' if exists $H_list{$_}; print "\n"; } print "List L UNIQUES: $L_count; FLAGGED From List H: $H_count \n";

    Here are the two short Test Lists and Test output:

    (ListL.txt) ABC123 DEF456 GHI789 (ListH.txt) ABC123 GHI789 (Test Output) ABC123 DEF456 GHI789 On List H List L UNIQUES: 3; FLAGGED From List H: 2

    As you can see, ABC123 should be also "Flagged" as "On List H", and is driving me NUTS as to why not.

    Thanks very much.

    -perlmonkster
Regular expression for finding acronyms
2 direct replies — Read more / Contribute
by mldvx4
on Aug 15, 2019 at 22:07

    I'm trying to build a pattern that will extract acronyms. I've gotten fairly close, but it leaves off the final period where it should be saved. Notice that it is missing from the output. For example, "X.H.T.M.L" should be "X.H.T.M.L."

    I wrote the pattern below in a moment of insight and now that moment has passed. How may I modify it to preserve the trailing periods?

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; while( s/(?'foo' ( (?=([[:upper:]]\.\s){2})[[:upper:]\.\s]{2,} | (?=([[:upper:]]\s){2})[[:upper:]\s]{2,} | (?=([[:upper:]]\.){2})[[:upper:]\.]{2,} | [[:upper:]] ){2,} )//x ) { my $acronym = $+{foo}; print qq("$acronym"\n); } } exit(0); __DATA__ L F and LF and L.F. and L. F. and not L, F. some HTML some XML. or X.H.T.M.L. or X. H. T. M. L. or even X H T M L but not U and I, or You and I. ...

    Or if there is an existing function or module which does that already, I can use that instead.

Data coming from API
2 direct replies — Read more / Contribute
by Jacob_Kold
on Aug 15, 2019 at 08:24
    I am trying to webscrape this website: https://horsens.dk/Politik/PolitiskeUdvalg/47 The data in the table comes from some API. There is a div on the website that collects the data.  <div id="catalog-meetings" data-jsonurl="/-/api/MeetingApi/Meetings/{0748B00B-3E46-42BE-9D24-1061A2CD345B}"></div> Is there a way to webscrape this website with perl?
Why isn't my script reading from my file?
1 direct reply — Read more / Contribute
by Jannejannesson
on Aug 14, 2019 at 22:04

    Hello Monks! I have a script used for word frequency in text files. And the last time I used this script it worked great.. However now when I run it, I'm getting "empty replies". This is my code:

    #!usr/bin/local/perl #use strict; #use warnings; my %count; my $file_name = shift or die "Usage: perl $0 [FILE]\n", open my $fh,'<', $file_name or die "Could not open '$file_name' $!"; while (my $line = <$fh>) { chomp $line; foreach my $word (split/\s+/, $line) { $count{$word}++; } } foreach my $word (sort keys %count) { printf "%-31s %s\n",$word, $count{$word}; } .. my @sorted_by_count = sort { $count{$b} <=> $count{$a} } keys %count; print "These are the 10 most frequented words of $file_name :\n"; print "$_ occured $count{$_} times\n" for @sorted_by_count[0 .. 9];

    This is what I mean by "empty replies", it's as if it hasn't read the file, but I'm not getting any error messages..

    :~/projekt$ perl wfreq.pl janne.txt These are the 10 most frequented words of janne.txt : occured times occured times occured times occured times occured times occured times occured times occured times occured times occured times :~/projekt$

    Any help is greatly appreciated. Thanks in advance.

use Crypt::Eksblowfish::Bcrypt to create a password the same as password_hash in PHP
4 direct replies — Read more / Contribute
by DaisyLou
on Aug 14, 2019 at 11:38

    Greetings monks

    I want to create two passwords that are encrypted in the same way in Perl and PHP (so I can authenticate against them). In PHP, it's easy:

    <?php echo "$2y$" . password_hash("mypassword", PASSWORD_BCRYPT); ?>

    In Perl, not as easy. I've installed Crypt::Eksblowfish::Bcrypt, but can't figure out how to get it to provide the same hashed password as PHP. Any ideas?

    Thank you in advance for your Perl wisdom.

net::dbus example
1 direct reply — Read more / Contribute
by didiw
on Aug 14, 2019 at 08:12
    Hello,

    I want to write a script reacting on specific ipower events on dbus. Can someone please provide an example without polling dbus all the time=.

    I thought using net::dbus::reactor and the add_read method seems the way to go, but I have no idea, what to do with the fd variable that is the first argument to add_read, because it is not explanined in the man page.

    I also will stick to another other method, if it is working.

    Anyway, I would like to have an example of what this file descriptor is used for.

    Thanks in advance

    Didiw


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.