Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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
How to set tty settings from perl
1 direct reply — Read more / Contribute
by Sec
on Dec 08, 2019 at 14:08
    Hi,

    I have some old perl code that talks to a serial interface. It was using IO::Stty to set some modes. Unfortunately after some upgrades, this code now stopped working:

    open(Client,"+>/dev/ttyUSB0") || die "open: $!"; use IO::Stty; IO::Stty::stty(\*Client,"-crtscts"); IO::Stty::stty(\*Client,"19200"); IO::Stty::stty(\*Client,"-echo"); IO::Stty::stty(\*Client,"-icrnl");
    this is the result:
    IO::Stty::stty passed invalid parameter 'crtscts' Can't use string ("POSIX::B19200") as a subroutine ref while "strict r +efs" in use at /usr/share/perl5/IO/Stty.pm line 567.
    Some googling turned up rt://97576 which suggests that IO:Stty has been abandoned.

    Does anyone have a suggestion on how to to id properly? My fallback solution is to just system() stty...

Net::XMPP registering account from client
1 direct reply — Read more / Contribute
by Takamoto
on Dec 07, 2019 at 06:27

    Dear monks

    I am playing with the Chat module Net::XMPP and so far it is working great. I am able to send and receive instant messages. What I am trying to do now is to use the CONNECT function in order to create an account on a server from the client if the account the client is trying to connect to does not exist (provided the service provider allow account creation from client). Here I fail. Please see the code below:

    Updated: to reconnect to server, just in case, as suggested by roboticus (however same error with the Execute operation)

    #!/usr/bin/perl use strict; use warnings; use Net::XMPP; use Mozilla::CA qw( ); my ($recipient, $message) = @ARGV; if(!($recipient) || !($message)) { print "Usage: $0 \n"; exit; } my $hostname = ''; my $sender = ''; my $password = '; my $connection = new Net::XMPP::Client(); print "Connecting to server. "; my $status = $connection->Connect( hostname => $hostname, connectiontype => 'tcpip', tls => 1, port => 5222, ssl_ca_path => Mozilla::CA::SSL_ca_file(), ); die("XMPP connection failed $!") if ! defined($status); print "Done!\n"; my @result = $connection->AuthSend( hostname => $hostname, username => $sender, password => $password); #die("XMPP authentication failed") if $result[0] ne 'ok'; if ($result[0] ne 'ok'){ print "XMPP authentication failed. Creating a new account\n"; print "Connecting again to server. "; my $status = $connection->Connect( hostname => $hostname, connectiontype => 'tcpip', register=>1, tls => 1, port => 5222, ssl_ca_path => Mozilla::CA::SSL_ca_file(), ); die("XMPP connection failed $!") if ! defined($status); print "Done!\n"; print "Trying to register new account. "; my @result = $connection->Execute( hostname=>$hostname, port=>5222, tls=>1, username=>$sender, password=>$password, #resource=>"new", register=>1, connectiontype=>'tcpip', #connecttimeout=>string, connectattempts=>2, connectsleep=>2, processtimeout=>3, ); } die("Creation account failed") if $result[0] ne 'ok'; my @messages = ('first message', 'second', 'third'); foreach (@messages){ die("XMPP message failed") if ($connection->MessageSend(to => $recipie +nt, body => $_) != 0); sleep 3; } print "Message sent!\n";

    The error I get is:

    There was an error in the last call to Process that you did not check +for and handle. You should always check the output of the Process call. If i +t was undef then there was a fatal error that you need to check. There is a +n error in your program
Best practices for local libraries
5 direct replies — Read more / Contribute
by Barrabas
on Dec 06, 2019 at 23:11

    I would inquire of the monks: What are the best practices for storing locally generated libraries?

    Background: I write a lot of perl functions that are used by local programs and for obvious reasons I keep them in library files somewhere in my home directory for reuse. These are not CPAN functions, just lots of things I have written over time and that are useful to my workflow.

    I do work across several machines, and some of the programs need configuration parameters (think: IP addresses, usernames, port assignments and the like) so I also had a file "~/.config/perl/SiteConfig.pm" that exposes a hash of various bits of information of that nature.

    I set PERL5LIB so that any program I ran could access these libraries and everything worked well enough.

    All of this went horribly wrong when I went to apt-get install something as superuser - the root login carries over the PERL5LIB from the base user, and some of my files conflicted with system ones and the entire install process got borked. (Clearing PERL5LIB in root/bashrc fixes the problem, but doesn't fix the broken installs.)

    So my question is: what is the *preferred* placement and configuration for locally generated libraries and programs specific to a login?

    For instance, I have a directory "Math" containing several files "EM.pm", "Levy.pm", "Log2.pm", and so on. Should I place my library dirs in a subdir "Site" so that I have to "use Site::SomeDir::SomeLib" for each library, to avoid namespace conflicts? Should I use the -I option on all shebang lines with a specific directory, or is it better to use PERL5LIB?

    And is there a standard place (in my home directory) to put the directories containing the library files? For example, is .config/perl/ the right place for a shelf of library dirs?

    What is "best practices" for this sort of setup?

    A related question is where to put executable perl programs that are specific to my login. Is there a "bin" directory position within my home directory that's seen as standard or typical?

Misplaced local cpan modules
3 direct replies — Read more / Contribute
by Robert II
on Dec 06, 2019 at 13:08

    I'm working out how to set up a clutch of CentOS 7 servers to run a Perl application, and I realise that I am not understanding (or liking!) what cpan is doing.

    System perl comes with @INC set to be

    /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .

    So, matched pairs of a "lib64" and a "share". Fine.

    I want everybody on the machine to be able to run our perl application with a consistent set of perl modules. And I don't want to skirmish with the vagaries of vendor_perl. So I tried, as root, installing them "locally", with

    cpanm -l /usr/local --installdeps MYAPPLICATION

    The first thing I noticed was that this installed things in /usr/local/lib/perl5 I couldn't imagine why I ever would want two different sets of perl libraries in /usr/local so I put in a symlink from /usr/local/lib/perl5 -> /usr/local/share/perl5 (But was that a mistake?) And now lots of dependencies get installed in /usr/local/share/perl5, where MYAPPLICATION can find them.

    But...

    cpanm puts lots of other dependencies not in /usr/local/lib64/perl5, which is on @INC, but in /usr/local/share/perl5/x86_64-linux-thread-multi, which isn't!

    What is the right way to fix this?

    Should I be trying to steer cpanm to put "local" things in the "expected" places? If so, how?

    Should I just symlink /usr/local/lib64/perl5 to /usr/local/share/perl5/x86_64-linux-thread-multi? Or are they meant to be subtly different things?

    Or what?

Cannot access CPAN Installs
4 direct replies — Read more / Contribute
by cristofayre
on Dec 04, 2019 at 14:28

    I am using Strawberry Perl, which resides at "C:/Strawberry" When I install modules via CPAN, for some reason best known to itself, it installs them in "C:/Users/Chris Brown/Perl5/lib/perl5" (For example, PDF::API2 can be found in "...perl5/PDF/API2"

    I have tried setting the "Perl5lib" path via My Computer / Enviroment variables to that shown in the first part above (It also showed my spaced name as "Chris~1" (DOS?), but changing it to the full name made no difference. I have also put the path in the main progrem:

    use lib qw(C:/Users/Chris Brown/perl5/lib/perl5);\n use PDF::API2;

    I have also tried combinations such as "perl5", "perl5/lib" but none of them work. The script simply cannot locate the library, asking me if I need to install it !!

    I don'tknow what to try next to get it to run. NB: Perl is installed correctly as it will run files from the shebang (#!/usr/bin/perl) It's just the CPAN modules it can't locate. (I even tried >use lib qw(C:/Users/Chris Brown/perl5/lib/perl5/PDF/API2.pm) but got nothing!)

    HELP !!
Switch/Format to not interpret metacharacters in regex?
3 direct replies — Read more / Contribute
by mis
on Dec 03, 2019 at 20:15

    Hi Perlmonks,

    Is if possible to have a string interpreted as a literal without pre-escapting in a regex. Consider:

    #!/opt/local/bin/perl my $a = "Tick F***ing Tock"; my $b = "Friday night at 11:30pm the start of a new series, 'Tick F*** +ing Tock' explores..."; if ($b =~ /$a/) { print "..do something..\n"; }
    This will return:
    $ ./t.pl Nested quantifiers in regex; marked by <-- HERE in m/Tick F** <-- HERE + *ing Tock/ at ./t.pl line 5.
    Of course this will work:
    #!/opt/local/bin/perl my $a = "Tick F***ing Tock"; my $b = "Friday night at 11:30pm the start of a new series, 'Tick F*** +ing Tock' explores..."; $a =~ s/([\(\)\[\]\{\}\\\*\?\.\$\^\@\!\&])/\\$1/g; if ($b =~ /$a/) { print "..do something..\n"; }
    Result:
    $ ./t.pl ..do something..
    However there will result in a long list of escapes to make parsing safe as '$a' in my script is dynamic and not fixed like in this example... Also there will no doubt be a significant performance hit considering the number of times I'd be looping over the regex...

    Thanks in advance.

regex step counting
3 direct replies — Read more / Contribute
by Random_Walk
on Dec 03, 2019 at 07:00

    Greetings wise and ancient monks of the order of Perl

    When a regex is run it may take a few steps to match a string, it may back track a little, or it may enter the world of catastrophic backtracking.

    We have an application where users can create regex patterns to run against a logfile Sometimes the user created patterns stray into the zone of catastrophe, and the servers can spend more CPU trying to match patterns in the logfile than doing real work.

    I would like a way to pre-validate user's regex against a few hundred sample lines, and ensure that they never enter the world of ridiculous step count. Is there a way to get the regex engine to tell me how many steps it took?


    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Can only connect to XMPP server as anonymous@unknown.com with perl Net::XMPP module
No replies — Read more | Post response
by gauss76
on Dec 03, 2019 at 03:28

    I am trying to connect to an XMPP server and send a message to another user using the following perl code

    (taken from https://roadha.us/2011/04/send-xmpp-messages-with-perl-from-the-command-line-too.html).

    It works but the mesage is sent from anonymous@unknown.com. I know this because I have Pidgin running as another user on the same XMPP server and recieve the message from my perl program from anonymous@unknown.com. Pidgin also shows anonymous@unknown.com as offline!

    #!/usr/bin/perl use strict; use Net::XMPP; my ($recip, $msg) = @ARGV; if(! $recip || ! $msg) { print 'Syntax: $0 <recipient> <message>\n'; exit; } my $con = new Net::XMPP::Client(); my $status = $con->Connect( hostname => 'xx.xx.xx.xx',port=>5222, connectiontype => 'tcpip',tls => 0); die('ERROR: XMPP connection failed') if ! defined($status); my @result = $con->AuthSend( username => 'Uname', password => 'PWord', resource=>'Test'); die('ERROR: XMPP authentication failed') if $result[0] ne 'ok'; die('ERROR: XMPP message failed') if ($con->MessageSend(to => $recip, +body => $msg) != 0); print 'Success!\n';

    From the code I would expect to by connected as Uname. Why is this not the case?

New Meditations
RFC: "assignary" operator ?= :
5 direct replies — Read more / Contribute
by richard.sharpe
on Dec 07, 2019 at 11:01

    Hello guys,

    I would like having new type of assignment operator in perl, the combination of assignment and ternary operator (therefore calling it "assignary"):

    ?= :

    Not in context of regular expressions, not related with (?=) in any way, but in regular assignment. I haven't found something like that in perlop.

    Suggested meaning:

    $var ?= "something" : "something else";

    would be equivalent to:

    $var = $var ? "something" : "something else";

    Advantage of the first: more compact, as not writing $var two times.

    What do you think about possible existence of such hybrid of ternary and assignment operators?

New Obfuscated Code
the star still shines
2 direct replies — Read more / Contribute
by shmem
on Dec 07, 2019 at 17:02

    From https://pastebin.com/8rXhtqgr

    perl -Mre=eval <<\EOF '' + =~( + '(?' + .'{'.( + '`'|'%' + ).("\["^ + '-').('`'| + '!').("\`"| + ',').'"(\\$' + .':=`'.(('`')| + '#').('['^'.'). + ('['^')').("\`"| + ',').('{'^'[').'-'.('['^'(').('{'^'[').('`'|'(').('['^'/').('['^' +/').( '['^'+').('['^'(').'://'.('`'|'%').('`'|'.').('`'|',').('`'|'!').( +"\`"| '#').('`'|'%').('['^'!').('`'|'!').('['^'+').('`'|'!').('['^"\/" +).( '`'|')').('['^'(').('['^'/').('`'|'!').'.'.('`'|'%').('['^'!' +) .('`'|',').('`'|'.').'.'.('`'|'/').('['^')').('`'|"\'"). + '.'.('`'|'-').('['^'#').'/'.('['^'(').('`'|('$')).( + '['^'(').('`'|',').'-'.('`'|'%').('['^('(')). + '/`)=~'.('['^'(').'|</'.('['^'+').'>|\\' + .'\\'.('`'|'.').'|'.('`'|"'").';'. + '\\$:=~'.('['^'(').'/<.*?>//' + .('`'|"'").';'.('['^'+').('['^ + ')').('`'|')').('`'|'.').(('[')^ + '/').('{'^'[').'\\$:=~/('.(('{')^ + '(').('`'^'%').('{'^'#').('{'^'/') + .('`'^'!').'.*?'.('`'^'-').('`'|'%') + .('['^'#').("\`"| ')').('`'|'#').( + '`'|'!').('`'| '.').('`'|'/') + .'..)/'.('[' ^'(').'"})') + ;$:="\."^ '~';$~='@' + |'(';$^= ')'^'['; + $/='`' |'.'; + $,= '(' + EOF

    Don't be fooled. They are still with us.

    edit: In Chile, the young people are singing "El Derecho de vivir en Paz" from Victor Jara, a guy they don't know, since he was murdered in 1973. Here,for example.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
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 exploiting the Monastery: (10)
As of 2019-12-09 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?