Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
DBI: select distinct returns same values
3 direct replies — Read more / Contribute
by tukusejssirs
on Sep 21, 2019 at 06:45

    I have strange problem.

    I have Pg database table with 20,000+ rows. The only important column from this table is the timestamp column called date.

    I need to get all unique dates from the table, so I run the following code in the terminal:

    mydb=# select distinct cast(date as date) from myschema.mytable; -[ RECORD 1 ]----------- date | 2019-09-18 -[ RECORD 2 ]----------- date | 2019-09-20 -[ RECORD 3 ]----------- date | 2019-09-19

    which is correct. However, when I run the following code, it output three times the same date:

    $sth_dates_uniq = $dbh->prepare("select distinct cast(date as date) fr +om myschema.mytable;") or die; $sth_dates_uniq->execute(); my @all_rows; while ( my $ref = $sth_dates_uniq->fetchrow_arrayref()) { push(@all_rows, $ref); } foreach my $row_ref (@all_rows) { print "@{$row_ref}\n"; } # Output 2019-09-19 2019-09-19 2019-09-19

    Do I do something wrong or is this a bug? What else could I do? I could get all the data from the table, sort them and get all unique values, however, the table is quite big to do so. Id like to have it as fast as possible.

Restarting a Perl script on Windows 10
4 direct replies — Read more / Contribute
by petro4213
on Sep 20, 2019 at 06:06
    I have a Perl script with a GUI that restarts itself, if I press a button in the GUI. This is done through this simple call in the callback of the button:
    exec( $pathToMyself );
    This works fine on Windows 7. But on Windows 10, the script ends (and the GUI disappears), but it doesn't restart, unless I go to the command window, which I used to initially call the script, and press the Enter key there.
    I don't like this behavior. Can anyone explain, where this comes from or give me a hint, if there's something I can do from the Perl side to restart my script without having to press Enter?
    Thanks a lot!

    Here's a simple reproducible example:

    use Tk; use strict; my $pathToMyself = $0; my $mw = MainWindow->new(); my $bt = $mw->Button(-text=>"Restart", -command=>\&restartMe)->pack(); MainLoop; sub restartMe { print "Restarting $pathToMyself...\n"; exec( $pathToMyself ); }
Module name case typo silent failure
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 19, 2019 at 15:05
    From time to time I forget to camelcase "HiRes" and this happens:
    perl -MTime::Hires=time -Wle 'print time'
    Of course it should be:
    perl -MTime::HiRes=time -Wle 'print time'

    Why no warning for module name camelcase typos?
    Why no warning for the failed import of time function from bogus module?

    Same in scripts with strict and warnings...

Re-uploading the previous non-trial version of a distribution to CPAN
3 direct replies — Read more / Contribute
by stevieb
on Sep 19, 2019 at 12:09

    Hello all,

    I have recently come across an issue in my RPi::WiringPi distribution (version 2.3632). Somehow, through all of the testing I do before I create a release, I missed an issue in a test file which breaks installation.

    Now, since then, I've made dozens of updates in my repository across two new trial versions (2.3633_01, which is released to CPAN, and 2.3633_02, which hasn't been uploaded). Is there an effective, safe way that I can fix this issue in a branch based off of the release commit and re-publish it as 2.3632, which is the current valid version which is installed via cpan/cpanm?

    I'd rather not have my VCS logs all out of whack and having to change Changes and other things if possible. In PAUSE, under the Utils menu, I came across a "Reset Version" option which looks like it'll do what I want, but I've never used this feature and am hoping that there's someone here has, and can provide some guidance.



Mojolicious, apache reverse proxy
4 direct replies — Read more / Contribute
by slatibart
on Sep 19, 2019 at 09:51

    I have apache running and need to run a mojolicious app under the path /rules

    This is the apache config part

    ProxyRequests Off ProxyPreserveHost On ProxyPass /rules http://localhost:3000/ keepalive=On ProxyPassReverse /rules http://localhost:3000/ RequestHeader set X-Forwarded-HTTPS "0"
    Very simply mojolicious script with a route to / and a route to /user
    app->hook(before_dispatch => sub { my ( $c ) = @_; my $url = $c->req->url; my $base = $url->base;# push @{ $base->path }, 'rules'; $base->path->trailing_slash(1); $url->path->leading_slash(0); }); get '/' => sub { my $self = shift; my $host = $self->tx->remote_address; my $content = "Host: $host"; $self->stash(content=>$content); $self->render('index'); }; get '/user' => sub { my $self = shift; my $host = $self->tx->remote_address; my $content = "Host: $host"; $self->stash(content=>$content); $self->render('index'); }; app->start; __DATA__ @@ index.html.ep % layout 'default'; <%== $content %> @@ layouts/default.html.ep <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> </head> <body> %= button_to Home => '/' %= button_to User => '/user' %= link_to Home => '/' %= link_to User => 'user' %= content </body> </html>
    Generated html looks good.
    <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> </head> <body> <form action="/rules"><input type="submit" value="Home"></form> <form action="/rules/user"><input type="submit" value="User"></form> <a href="/rules">Home</a> <a href="/rules/user">User</a> Host: </body> </html>
    Using the home button / links works but for route /user mojolicous throws an error.

    "None of these routes could generate a response for your GET request for //user, maybe you need to add a new one?"

    Where is that extra slash coming from and how to get rid of that ?

    Bonus question. If I add another layer   push @{ $base->path }, 'all/rules'; all slashes are transformed.

    <form action="/all%2Frules"><input type="submit" value="Home"></form> <form action="/all%2Frules/user"><input type="submit" value="User"></f +orm> <a href="/all%2Frules">Home</a> <a href="/all%2Frules/user">User</a>
    Any idea why ?

    Thanks for your help.

Update Primary Key with DBIx::Class
3 direct replies — Read more / Contribute
by phildeman
on Sep 18, 2019 at 15:43


    I was attempting to update a MySQL table's primary key using DBIx::Class. I do not get any errors executing the update.
    However, when I check the table using Workbench or MySQL Client, it shows the old value.

    Is DBIx::Class restricted from updating primary keys. It can be done with DBI.

    Here is a snippet of code to perform the update:

    use My::Data; my $schema = "My::Data"; my $acct = $schema->resultset( 'Accounts')->search({ acctid => 'pv001' + })->single; if($acct) { $acct->acctid( 'pv002' ); $acct->update; } else { print "Could not find account to update!\n\n"; }

    As I stated, when I check the table, the value 'pv001' is still in the database. I searched for 'pv002', I could not find any record
    with that value as the primary key.

    Has anyone encounter this issue where DBIx::Class will not update the primary key? If yes, how did you resolve this issue?
    I am hoping there is a resolution, before I use DBI.



Accessing IMAP-Server via Perl
3 direct replies — Read more / Contribute
by PerlingAround
on Sep 17, 2019 at 08:12


    I'm atm trying to write up a perl script, that I need to monitor my new Office 365 mailbox and I am having some problems. I am currently using the module "Net::IMAP::Simple as I have previously used it to query data from another older IMAP Server. So to test the basics I simply try to connect, login and logout to and from the server.

    But it seems like my script simply refuses to properly login to the Server, as anything I try produces the Error-String "Command received in invalid state". Browsing some other sites, I found out, that this Error usually is fixed by using "plain" authentication, an Option I can't seem to find within Net::IMAP::Simple, and all my other searches ended up leading me to modules for use on the IMAP-Server itself.

    So what I need help with, is to find out if there is a way to login to the server using the Net::IMAP::Simple Module, or if it is simply not possible that way. If that's the case, I'd be delighted if someone could point me in the direction of a way to actually do it.

    Finally, the End-Goal of this Operation is to search for the last unseen mail and read how old it is. Since I can't even login right now, I pushed that issue back a bit, and am focusing on getting the login to work first. If any of you have an idea of how to do this too, that would just be the cherry on top.

    Please keep the Solutions "Newb-friendly" though. I am only rarely forced to write Perl-Scripts and usually they are quite short or at the least quite easy to come by, so I am not 100% involved in the whole module-management part.

    Thank you for the help in advance

How to Order an Array's Elements to Match Another Array's Element Order
11 direct replies — Read more / Contribute
by ozboomer
on Sep 17, 2019 at 03:13


    I'm doing some (what I think is) more unusual things with some arrays.. and I'm looking to see if there's a more 'elegant' method than what I'm currently using...

    I have a 'reference' array that is processed, producing a 'subset' array. Generally, the elements of the 'subset' array are not in the same order as they appear in the 'reference' array... but that's the order I need to do some more processing.

    So, I've cobbled together some code that does the job, viz:-

    use Data::Dumper; @array = ( "APPLE", "ORANGE", "PEACH", # Use this for the ORDE +R "GRAPE", "BANANA", "PINEAPPLE" ); @subset = ( "PINEAPPLE", "GRAPE", "APPLE" ); # A required subset but + NOT # in the required order @results = (); # The subset with its e +lements in the # same order as the 're +ference' array # ---- Method 1: Using arrays directly @tmp = (); foreach my $check (@subset) { my ($index) = grep { $array[$_] eq $check } 0..$#array; push(@tmp, $index); } @index_list = sort {$a <=> $b} @tmp; # Sort the index values numerica +lly @results = @array[@index_list]; # Slice the index items out of ' +@array' printf("Method 1:\n"); print Dumper(@results); printf("\n"); # ---- Method 2: Using a sub Reset_Order(\@subset, \@array, \@results); printf("Method 2:\n"); print Dumper(@results); printf("\n"); # ========== end mainline ========== # ------------------------------------- # Reset_Order - ... # Uses Globals: # ------------------------------------- sub Reset_Order { my ($small_ref, $large_ref, $res_ref) = @_; my (@tmp); @tmp = (); foreach my $check (@$small_ref) { my ($index) = grep { $$large_ref[$_] eq $check } 0..$#$large_ref +; push(@tmp, $index); } my @index_list = sort {$a <=> $b} @tmp; # Sort the index values nu +merically @$res_ref = @$large_ref[@index_list]; # Slice the index items ou +t of the large array return(1); } # end Reset_Order

    ...but maybe there's a better way to do it, say, using grep in conjunction with map but I can't vizualize how that might be done -- I'm still not super flash with the 'more clever' ways of using these functions(!)

    I would search for something to help me out... but I don't even know how to describe what I'm trying to do(!)

    I'd appreciate any suggestions on where to go next...

New Meditations
Language design: direct attribute access and postponed mutators (Perl Vs Python)
5 direct replies — Read more / Contribute
by LanX
on Sep 15, 2019 at 13:42
    Dear monastery,

    In my never ending desire to study different language designs I had a closer look into Python's OO model ...

    ... and I was very surprised to see that there attributes are accessed directly.

    object.attribute = 10

    is very common "pythonic" code.

    Doing something like object->{attribute}=10 is not only very uncommon in Perl but also heavily frowned upon.

    This is only partly because Perl's inner implementation is not necessarily a blessed hash and because in Perl methods and attributes have separated namespaces. (In python much like JS they are inside the same hash)

    The main issue is that at the moment of object design you can't know how the nature of getting and setting attributes may evolve in the future...

    ... for instance you may later want to restrict allowed values to a range of values and the setter to act like a guard which can potentially report errors.

    Could it be that the python community is totally ignorant of this issue?

    Seems not, for this case they reserve a backdoor mechanism called property where a simple attribute value is replaced by another object with dedicated get, set, delete accessors. And to facilitate using it they provide a @property decorator as syntactic sugar. "@Decorators" are the python equivalent of ":attributes" (For deeper insights please see this SO discussion)

    This reminds me a lot to the :lvalue technique once discussed for Perl.

    We can easily define a getter mechanism for a attribute with

    sub attribute :lvalue { return $value }

    And returning a tied value could be used to extend it with a proper setter.

    sub attribute :lvalue { return $tied_value }

    This help provide a object->attribute = 10 interface, were the Store and Fetch of the tied value would act as getter and setters.

    I remember seeing this discussed by Damian - not sure if here or in his OO book.

    IIRC he dismissed this technique for being to slow.


    • Could it be possible that the python solution is much faster?
    • If yes why?
    • are there better options in Perl to upgrade the easier interface?
    • Update: is Variable::Magic an option?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    PS: this is a meditation seeking for insights... fanboys please spare me with "just use moose propaganda", I'll ignore it.

New Cool Uses for Perl
Perl Tk run on Android. It works
1 direct reply — Read more / Contribute
by zentara
on Sep 17, 2019 at 13:46
    Hi,this is a step by step guide, but your experience might differ.

    I am using Android 7.0 Nitrogen on a Nexus 7.

    (1) Go to google play store and install the termux app. Now follow the instructions for setting up a graphical environment at termux gui. I suggest using the tigervnc as the XSDL dosn't work on Android later than v4. Also install the VNC Viewer app. The instructions explain how to setup fluxbox and openbox but I had trouble getting them to load. The default TWM works fine. Basically the commands until now should be "pkg install x11-repo" followed by "pkg install tigervnc aterm"

    (2) Install the cc with "pkg install clang libxorgproto" I found that X11/X.h file was in libxorgproto and is needed.

    (3) While in termux, and at the prompt "pkg install mc" to naviagte and edit :-). Create and edit the file ~/.bashrc and add export DISPLAY=":1"

    (4) Edit the ~/.vnc/xstartup file to say this:
    aterm -geometry 80x24+10+150 -ls &
    twm &

    At the termux prompt, type "vncserver -localhost" and you should get a message saying :1 was started. If not do a "killall Xvnc" and try again. Once you are running, start the VNC Viewer app and enter for address. 5900 + display number

    You should see a black screen in your VNC Viewer with an aterm. In the aterm type "pkg install perl" then "pkg install Tk".

    The Japanese fonts don't seem to work, and the test takes long, but the Tk windows are opening.

    You may find that cpan's install fails at make test, so then just navigate to ~/.cpan/build/Tk-****/ and type "make install". Then you will find that the shebang line #!/usr/bin/perl needs to be symlinked or changed to /data/data/com.termux/files/usr/bin/perl. YMMV. As an easy solution, just start all Tk programs like "perl mytkapp"

    Good luck, have fun. Perl Tk is way easier than Android programming. :-)

    I'm not really a human, but I play one on earth. ..... an animated JAPH
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 taking refuge in the Monastery: (5)
As of 2019-09-21 21:42 GMT
Find Nodes?
    Voting Booth?
    The room is dark, and your next move is ...

    Results (273 votes). Check out past polls.