Re: use string in "use"

2020-12-15 Thread Chas. Owens
The use function happens at compile time and must take a bareword, but it
is functionally equivalent to

BEGIN { require Module; Module->import( LIST ); }

And the require function allows you to pass a string, but be aware there
are lots of differences in behavior when passing a string vs a bareword:
https://perldoc.perl.org/functions/require



On Mon, Dec 14, 2020, 10:57 Jim Gibson  wrote:

> I have not used it myself, but the ‘if’ module allows one to load modules
> conditionally:
>
> use if CONDITION, MODULE => ARGUMENTS;
>
> See ‘perldoc if’ for more details.
>
>
> > On Dec 14, 2020, at 7:23 AM, Gary Stainburn <
> gary.stainb...@ringways.co.uk> wrote:
> >
> > I've written my first re-usable modules in Perl, and all goes well.
> However, I want to know if / how I can use a string variable in the 'use'
> clause.
> >
> > In PHP I have a simple system of turning on/off debugging and version
> control.  Any file *1.html is development version.  I then have
> >
> > $DEBUG=(preg_match('/1.htm/',$_SERVER['REQUEST_URI'])) ? '1' : '';
> > include_once("sql$DEBUG.inc");
> > include_once("security$DEBUG.inc");
> >
> > This way I have a live and a development version of every HTML and every
> inc file, and putting any one file is simply a case of copying that file
> over.
> >
> > I'm looking to replicate  this in Perl.  How can I do the following?
> >
> > #!/usr/bin/perl -w
> >
> > use warnings;
> > use strict;
> >
> > my $DEBUG=($0=~/1$/) ? '1' : '';
> > use RW::Sql$DEBUG;
> > use RW::Docs$DEBUG;
> >
> > I've found that you can use "require" and pass a path.  I understand
> that require is run time, while use is compile time.  Are there any
> down-sides to using require?
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> > For additional commands, e-mail: beginners-h...@perl.org
> > http://learn.perl.org/
> >
> >
>
> Jim Gibson
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: perl-5.32.0 - Failed test ''S' is set in PERL_UNICODE, or in -C, honor it, utf8 should be on'

2020-09-29 Thread Chas. Owens
The correct answer (as I am sure you know) is not to be using EOL
software.  That said you may be jumping too far forward with your Perl
release.  It looks like OpenSSL only requires 5.10 (from 2007) and you are
trying to install 5.32 (from June).  Your current system is only running
5.8.8 (which wasn't even the last of the 5.8 line, 5.8.9 came out in
2008).  I think your best bet (assuming you can't just upgrade to a
non-EOLed version of the OS) is to only upgrade to the last version of 5.10
(https://cpan.metacpan.org/authors/id/D/DA/DAPM/perl-5.10.1.tar.bz2). That
will likely have the least amount of breakage on your system.

On Mon, Sep 28, 2020 at 12:25 AM Robbi Nespu  wrote:

> Hello everyone, I am new with perl.
>
> My test server are using SLES11SP4 and it already EOL.
> The latest openssl I can get from official repos it too old
>
> > $ openssl version
> > OpenSSL 0.9.8j-fips 07 Jan 2009
>
>
> I plan to update with newer openssl
> > # cd /usr/local/src/openssl-1.1.1h
> > # ./config Wl,enablenewdtags prefix=/usr/local/ssl
> openssldir=/usr/local/ssl
> > Operating system: x86_64-whatever-linux2
> > Perl v5.10.0 required--this is only v5.8.8, stopped at ./Configure line
> 12.
> > BEGIN failed--compilation aborted at ./Configure line 12.
> > Perl v5.10.0 required--this is only v5.8.8, stopped at ./Configure line
> 12.
> > BEGIN failed--compilation aborted at ./Configure line 12.
> > This system (linux-x86_64) is not supported. See file INSTALL for
> details.
>
>
> so.. seem I need to update perl (here i come)
> > # cd /usr/local/src/
> > # wget https://www.cpan.org/src/5.0/perl-5.32.0.tar.gz
> > # tar -xvzf (/usr/local/src/.tar.gz
> > # cd perl-5.32.0
> > # sh Configure -de -Dusethreads
> > # make
> > # make test
> > .
> > .
> > .
> > cpan/Test-Simple/t/Test2/modules/Formatter/TAP . #
> Failed test ''S' is set in PERL_UNICODE, or in -C, honor it, utf8 should be
> on'
> > # at t/Test2/modules/Formatter/TAP.t line 65.
> > # Failed test 'IO handle stuff'
> > # at t/Test2/modules/Formatter/TAP.t line 93.
> > FAILED at test 1
> > .
> > .
> > Failed 1 test out of 2553, 99.96% okay.
> > ../cpan/Test-Simple/t/Test2/modules/Formatter/TAP.t
> > ### Since not all tests were successful, you may want to run some of
> > ### them individually and examine any diagnostic messages they produce.
> > ### See the INSTALL document's section on "make test".
> > ### You have a good chance to get more information by running
> > ###   ./perl harness
> > ### in the 't' directory since most (>=80%) of the tests succeeded.
> > ### You may have to set your dynamic library search path,
> > ### LD_LIBRARY_PATH, to point to the build directory:
> > ###   setenv LD_LIBRARY_PATH `pwd`; cd t; ./perl harness
> > ###   LD_LIBRARY_PATH=`pwd`; export LD_LIBRARY_PATH; cd t; ./perl harness
> > ###   export LD_LIBRARY_PATH=`pwd`; cd t; ./perl harness
> > ### for csh-style shells, like tcsh; or for traditional/modern
> > ### Bourne-style shells, like bash, ksh, and zsh, respectively.
> > Elapsed: 1567 sec
> > u=15.41  s=3.92  cu=967.80  cs=74.34  scripts=2553  tests=1220626
> > make: *** [test] Error 1
>
> at this point I have no idea where to check and fix it. checking
> ../cpan/Test-Simple/t/Test2/modules/Formatter/TAP.t  but I don't
> understand.
>
> Could you give me some advice?
>
>
>


Re: covert perl code to binary

2019-01-17 Thread Chas. Owens
The author of that module is Reini Urban. He has a long standing feud
with the Perl 5 Porters (the team that writes Perl).

On Fri, Jan 11, 2019 at 11:05 AM Mike Flannigan  wrote:
>
>
> Thanks.
>
> My Strawberry install finally failed with
> Stopping: 'install' failed for 'B::C'.
>
> I am running ver 5.26 of Strawberry.
>
>
>
> I suspect you mean this Readme file:
>
> INSTALL
>
> cpan B::C
>
> On strawberry I needed
>   perl Makefile.PL FIXIN="perl -S pl2bat.bat"
>
> On Windows and AIX for 5.12 and 5.14 you need to patch and rebuild CORE perl:
>   ramblings/Export-store_cop_label-for-the-perl-compiler.patch
>
> For 5.14 and 5.15 I recommend also the following patches:
>   ramblings/revert-B-load-BEGIN.patch (The 5.14.1 version)
>   ramblings/Carp-wo-B.patch
>
> We generally discourage the use of perl since 5.16 unhandled and
> unacknowledged security problems with the implementation of "unicode"
> symbols and packagenames, where they really implemented binary names
> for all symbols without any checks and further support of such binary
> names. Identifiers are not identifiable anymore.
> See the warning at perl Makefile.PL.
> 5.20 improved support for binary names for most syscalls, dumpers and
> APIs, but TR39 and strict names are still not handled, the problems
> are not understood and reactions are generally hostile.
> 5.18 made it 20% bigger with the implementation of COW strings.
> Most strings cannot be stored statically anymore since then.
> The best perl versions for the compiler are 5.14.4, 5.22.1 and cperl,
> since these are the most tested versions, where the coretest suite passes.
>
>
> The above is only part of the Readme file.
>
>
> I'm not sure what to make of that.
> Especially that part that says:
>
> 'We generally discourage the use of perl since
> 5.16 unhandled and unacknowledged security problems'
>
>
> Mike
>
>
>
> On 1/11/2019 9:28 AM, Bruce Ferrell wrote:
>
> I built it on a Linux box, it blew up so I read the README... that has 
> instructions for how to do it on Windows
>
> As noted by another poster, it seems to work in spite of the test blowing up
>
> On 1/11/19 5:37 AM, Mike Flannigan wrote:
>
>
> I could use some more explanation.
>
> B::C is a module that you install from CPAN.
> I assume I don't put Use B::C at the top of
> my perl script, but instead perlcc uses it.
>
> Is perlcc also a module?
> Or is that an executable?
> I think I see that it is composed of 5 files:
> assemble
> cc_harness
> disassemble
> perlcc.PL
> pl2exe.pl
>
> After installing perlcc, do I just open a command
> prompt and type perlcc -o hello.exe hello.pl?
>
> Does all this work on Windows?  My guess is Yes.
>
>
> Mike
>
>
> On 1/11/2019 6:57 AM, Andrew Solomon wrote:
>
> Just a warning - I'm no expert on this topic, but it was such an interesting 
> question I decided to find out for myself :-)
>
> I installed B::C and ran 
> https://metacpan.org/pod/distribution/B-C/script/perlcc.PL
>
> It actually does the compilation - as opposed to pp which is just packaging 
> it - so you don't see the perl code of the source file.
>
> Does that resolve your question?
>
> Andrew
>
>
>
> On Fri, Jan 11, 2019 at 9:59 AM Uday Vernekar  wrote:
>>
>> Hi all,
>>
>> I have a perl code which I need to covert to binary so that nobody can see 
>> the code.
>>
>> we used pp package to make the perl code binary but here the user can see 
>> the code which gets created in tmp.
>>
>> Need help if anybody knows kindly reply
>>
>> With regards
>> Uday V G
>
>
>
> --
> Andrew Solomon
>
> Perl Trainer, Geekuni http://geekuni.com/
> and...@geekuni.com // +44 7931 946 062
>
>
>
>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: covert perl code to binary

2019-01-17 Thread Chas. Owens
The only way to truly hide code is to not give the code to the person
you don't want to see it.  Even languages like C have decompilers. If
you truly need to prevent people from seeing code, then your only real
option is to run a server and distribute a client that connects to the
server.  If all you need to is to discourage casual lookers from
seeing the source, then you only need something like Acme::Bleach.
Yes, you can unbleach the code, but it takes effort and knowledge and
with those no code is safe behind any form of obfuscation (including
compilation to binary).

If you are trying to hide the code because you are distributing
secrets (like passwords, api keys, etc) in your code, then you have a
fundamental design issue that needs to be fixed.

On Mon, Jan 14, 2019 at 12:56 AM Uday Vernekar  wrote:
>
> Nobody is very smart :) so only we want him not to see the code..we tried 
> bleach as well..one can see the code by replacing end of  first line of 
> bleached code with print and after executing we can see the code.
>
> On 11-Jan-2019 6:29 pm, "David Mertens"  wrote:
>>
>> How dumb is your "nobody"? Would Acme::Bleach do the trick? Or something 
>> similar?
>>
>> :)
>>
>> On Fri, Jan 11, 2019, 5:01 AM Uday Vernekar >>
>>> Hi all,
>>>
>>> I have a perl code which I need to covert to binary so that nobody can see 
>>> the code.
>>>
>>> we used pp package to make the perl code binary but here the user can see 
>>> the code which gets created in tmp.
>>>
>>> Need help if anybody knows kindly reply
>>>
>>> With regards
>>> Uday V G

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: What hash function to use

2018-08-23 Thread Chas. Owens
It looks like https://metacpan.org/pod/Crypt::Password might implement
Modular Crypt Format.

On Thu, Aug 23, 2018 at 11:38 AM Chas. Owens  wrote:

> That prefix appears to for the modular crypt format (see below) and lets
> the shadow file use different hashing functions for different passwords.
> It is not part of the Bcrypt spec, but you should be able to just add it
> on.  Does the password hash correctly otherwise?
>
> https://passlib.readthedocs.io/en/stable/modular_crypt_format.html
>
> On Thu, Aug 23, 2018 at 8:30 AM Mike Flannigan  wrote:
>
>>
>> I hope somebody helps you with this.
>>
>> I don't think this will help you.  It sounds
>> like you already know all of this:
>> https://crackstation.net/hashing-security.htm
>>
>> I don't know what $2b$ is.  Still trying to
>> figure that out.
>>
>> It looks like Digest::Bcrypt was last updated 3/2017.
>> I hope it hasn't been abandoned.
>>
>>
>>
>> Mike
>>
>>
>>
>> On 8/23/2018 3:00 AM, beginners-digest-h...@perl.org wrote:
>>
>> Hello,
>>
>> I am not sure if my question is really of beginer level.
>>
>> On a system, I have set my password to "t410" and it has ben hashed to:
>> $2b$10$OQBll77HJqnOR.zqK2jx8ukE6m68Azc7nrsgRdcT6bVfERRmzFV4.
>>
>> What could I use with Perl to get the same result?
>>
>> I tried Digest::Bcrypt but it does not support $2b$ as far as I can
>> tell.
>>
>> Thanks in advance,
>>
>> Olivier
>>
>>
>>


Re: What hash function to use

2018-08-23 Thread Chas. Owens
That prefix appears to for the modular crypt format (see below) and lets
the shadow file use different hashing functions for different passwords.
It is not part of the Bcrypt spec, but you should be able to just add it
on.  Does the password hash correctly otherwise?

https://passlib.readthedocs.io/en/stable/modular_crypt_format.html

On Thu, Aug 23, 2018 at 8:30 AM Mike Flannigan  wrote:

>
> I hope somebody helps you with this.
>
> I don't think this will help you.  It sounds
> like you already know all of this:
> https://crackstation.net/hashing-security.htm
>
> I don't know what $2b$ is.  Still trying to
> figure that out.
>
> It looks like Digest::Bcrypt was last updated 3/2017.
> I hope it hasn't been abandoned.
>
>
>
> Mike
>
>
>
> On 8/23/2018 3:00 AM, beginners-digest-h...@perl.org wrote:
>
> Hello,
>
> I am not sure if my question is really of beginer level.
>
> On a system, I have set my password to "t410" and it has ben hashed to:
> $2b$10$OQBll77HJqnOR.zqK2jx8ukE6m68Azc7nrsgRdcT6bVfERRmzFV4.
>
> What could I use with Perl to get the same result?
>
> I tried Digest::Bcrypt but it does not support $2b$ as far as I can
> tell.
>
> Thanks in advance,
>
> Olivier
>
>
>


Re: regex to get the rpm name version

2018-07-27 Thread Chas. Owens
But if you have to use a regex, I suggest using the /x  modifier to make it
easier to read an maintain the regex:

#!/usr/bin/perl

use strict;
use warnings;

for my $s (qw/binutils-2.23.52.0.1-12.el7.x86_64
compat-libcap1-1.10-3.el7.x86_64 compat-libstdc++-33-3.2.3-71.el7.i686/) {
my ($name, $version, $build) = $s =~ m{
^ (.*) # name
- (.*) # version
- ([0-9]+) # build
[.] [^.]+  # os
[.] [^.]+ \z  # architecture
}x;
print "n $name v $version b $build\n";
}

On Fri, Jul 27, 2018 at 9:14 AM Chas. Owens  wrote:

> I don't think a regex is the simplest and most maintainable way to get
> this information.  I think it is probably better to take advantage of the
> structure of the string to discard and find information:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> for my $s (qw/binutils-2.23.52.0.1-12.el7.x86_64
> compat-libcap1-1.10-3.el7.x86_64 compat-libstdc++-33-3.2.3-71.el7.i686/) {
> my @dots = split /\,/, $s;
> pop @dots; #get rid of architecture
> pop @dots; #get rid of os
> my $name_and_version = join "", @dots;
> my @dashes = split /-/, $s;
> my $build = pop @dashes;
> my $version = pop @dashes;
> my $name = join "-", @dashes;
> print "n $name v $version b $build\n";
> }
>
>
>
> On Fri, Jul 27, 2018 at 8:57 AM Asad  wrote:
>
>> Hi All ,
>>
>>  I want to get a regex to actually get the rpm name and version
>> for comparison :
>>
>>
>> binutils-2.23.52.0.1-12.el7.x86_64",
>> compat-libcap1-1.10-3.el7.x86_64"
>> compat-libstdc++-33-3.2.3-71.el7.i686
>>
>> (^[a-zA-Z0-9\-]*)\-\d'
>>
>> First part of the regular expression is ^[a-zA-Z0-9\-]
>>
>> which means search for anything that begins with a letter
>>
>> (lower or upper) or a number up until you reach an
>>
>> hyphen sign (‘-‘).
>>
>> but it fails to match
>>
>> compat-libstdc++-33-3.2.3-71.el7.i686
>>
>> Please let me know what regex should i use to extract all 3
>>
>> rpms.
>>
>> Also let me know if there are web tools to build regex
>>
>> Good websites for regex tutorials.
>>
>>
>>
>>
>> Thanks,
>>
>>
>>
>>
>>
>> --
>> Asad Hasan
>> +91 9582111698 <+91%2095821%2011698>
>>
>


Re: regex to get the rpm name version

2018-07-27 Thread Chas. Owens
I don't think a regex is the simplest and most maintainable way to get this
information.  I think it is probably better to take advantage of the
structure of the string to discard and find information:

#!/usr/bin/perl

use strict;
use warnings;

for my $s (qw/binutils-2.23.52.0.1-12.el7.x86_64
compat-libcap1-1.10-3.el7.x86_64 compat-libstdc++-33-3.2.3-71.el7.i686/) {
my @dots = split /\,/, $s;
pop @dots; #get rid of architecture
pop @dots; #get rid of os
my $name_and_version = join "", @dots;
my @dashes = split /-/, $s;
my $build = pop @dashes;
my $version = pop @dashes;
my $name = join "-", @dashes;
print "n $name v $version b $build\n";
}



On Fri, Jul 27, 2018 at 8:57 AM Asad  wrote:

> Hi All ,
>
>  I want to get a regex to actually get the rpm name and version
> for comparison :
>
>
> binutils-2.23.52.0.1-12.el7.x86_64",
> compat-libcap1-1.10-3.el7.x86_64"
> compat-libstdc++-33-3.2.3-71.el7.i686
>
> (^[a-zA-Z0-9\-]*)\-\d'
>
> First part of the regular expression is ^[a-zA-Z0-9\-]
>
> which means search for anything that begins with a letter
>
> (lower or upper) or a number up until you reach an
>
> hyphen sign (‘-‘).
>
> but it fails to match
>
> compat-libstdc++-33-3.2.3-71.el7.i686
>
> Please let me know what regex should i use to extract all 3
>
> rpms.
>
> Also let me know if there are web tools to build regex
>
> Good websites for regex tutorials.
>
>
>
>
> Thanks,
>
>
>
>
>
> --
> Asad Hasan
> +91 9582111698 <+91%2095821%2011698>
>


Re: about system() call

2018-07-24 Thread Chas. Owens
The first spawns a shell and can handle things like globs.  This is less
efficient, more powerful, and more dangerous (susceptible to code injection
attacks)

The second does not spawn a shell and therefore cannot handle globs.  It is
also less susceptible to code injection attacks.

system "ls *.pl";

is equivalent to typing ls *.pl on the commandline.

system "ls", "*.pl";

is equivalent to typing ls '*.pl' on the commandline (it the glob does not
expand).

Code injection attacks can occur when you use untrusted data in a something
that runs code (like string eval) or executes programs (like system):

my $user = untrusted_source();
system  "ls /home/$user > /tmp/userfiles";

If the untrusted source returns "; cat /etc/passwd" then you will copy the
passwd file to /tmp/userfiles instead of the intended output.  It is always
good to sanitize  user inputs (ensure that the values are within the
expected values) that are untrusted for this reason.  If you are handling
untrusted data often, it is a good idea to turn on taint mode in Perl.  It
will throw a runtime error if you try to use untrusted data without
sanitizing it with a regex or other sanitizing function.  See
https://perldoc.perl.org/perlsec.html#Taint-mode for more information.

On Tue, Jul 24, 2018 at 8:37 AM Lauren C.  wrote:

> Hi,
>
> $ perl -le 'system "df -h"'
>
> $ perl -le 'system "df","-h"'
>
> The both two styles work fine.
> what's the difference between them and which is better usage?
>
> thanks.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Unfamiliar syntax

2018-07-20 Thread Chas. Owens
All of this is supposition since I can't see anything you haven't shown us.

It sounds like this code is part of a larger program that is going to call

   do "EPrints";

which will bring the source of EPrints into the larger program. The $c
variable is probably setup there.  What the code in EPrints is doing is
setting up a handler named can_request_view_document.  At some point in the
larger program, it is going to say something like

my $document = $r->param(doc);
my $can_request = $c->{can_request_view_document}->($document, $r);

return 403, "You are not allowed to access $document" unless $can_request;
return 400, "$document doesn't exist" unless -f $document;
return 200, $document;


On Fri, Jul 20, 2018 at 11:06 AM James Kerwin  wrote:

> Afternoon all,
>
> I have been asked to take a look at a .pl file which is part of a set-up
> called "EPrints". The particular file controls who can access documents on
> a server.
>
> Excluding some comments, the file starts like:
>
> $c->{can_request_view_document} = sub
> {
> my( $doc, $r ) = @_;
>
> It then goes on to complete the subroutine and return a value.
>
> I understand that $doc and $r are populated via @_ but I have never before
> seen the structure of the first line starting with "$c". Additionally, the
> file doesn't look like a typical Perl file (eg.#!/usr/bin/perl -w at the
> top and some use statements).
>
> I know it's very vague, but could anybody explain this syntax to me? My
> suspicion is that something else is calling the subroutines in this file?
> Am I at least on the right track?
>
> Thanks,
> James
>


Re: Net::XMPP::Client

2018-06-15 Thread Chas. Owens
Look at the SetMessageCallBacks method in Net::XMPP::Protocol.
  It lets you set the function to run when a message of a specific type
comes in.

On Fri, Jun 15, 2018 at 5:34 AM hw  wrote:

> On 06/15/2018 02:21 PM, Chas. Owens wrote:
> > In Net::XMPP::Client it says
> >
> > For a full list of high level functions available please see
> > Net::XMPP::Protocol.
> >
> > In that documentation it says
> >
> > $Con = new Net::XMPP::Client(); # From
> > $status = $Con->Connect(hostname=>"jabber.org <http://jabber.org>"); #
> > Net::XMPP::Client
> >
> > $Con->Send("XML");
>
> Sending a message was easy, using Net::XMPP::Client and MessageSend().
>
> The documentation of Net::XMPP::Client says "Net::XMPP::Client provides
> functions to connect to an XMPP server, login, send and receive messages
> [...]".
>
> Hence I´m wondering how to receive messages with this client module.  I
> don´t want to write my own client if that can be avoided.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Net::XMPP::Client

2018-06-15 Thread Chas. Owens
In Net::XMPP::Client it says

For a full list of high level functions available please see
Net::XMPP::Protocol.

In that documentation it says

$Con = new Net::XMPP::Client(); # From
$status = $Con->Connect(hostname=>"jabber.org"); # Net::XMPP::Client

$Con->Send("XML");



On Fri, Jun 15, 2018, 03:40 hw  wrote:

>
> Hi,
>
> using Net::XMPP::Client, I am able to send messages, but I don´t see a
> way to receive messages.  The documentation leaves me entirely in the
> dark as to how to do this, and I couldn´t find an example using this
> module to receive messages.
>
> So how can I receive messages?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Read a huge text file in perl

2018-05-24 Thread Chas. Owens
A lot depends on the contents of "some task;".   If the task does not leave
stuff in memory for each iteration, then it is just taking a long time.  If
it does leave stuff in memory, then you could easily run out of memory.

Also, a lot depends on the OS.  For instance, the defaults for filesystems
in AIX is (or at least used to be) to have a read-ahead buffer. Putting
stuff in that buffer trumped everything else, so just doing a simple wc -l
bigfile could fill memory and make it hard to ssh into the box (that was a
lot of fun to track down).

A good practice when working with really large things is to provide some
form of feedback to the user that things aren't completely hung.  Try this:

my $i = 0;
while (<$FILE>) {
 chomp;
 some task;
 ...
} continue {
if ((++$i % 10_000) == 0) {
print "handled $i records\n";
}
}

This will print out a message every 10,000 lines.  If it does blow up, at
least you will know about how many records were processed and may be able
to either split the file or restart at a given position (see the pos and
seek functions).

On Thu, May 24, 2018 at 11:00 AM SATYABRATA KARAN <
satyabratakara...@gmail.com> wrote:

> Hello,
> Does anyone guide me how to handle a huge file (>50GB) in perl?
>
> I tried open method this way, but it's hanging the terminal. I think that
> is out of memory error internally.
>
> open my $FILE, "<", "Log_file.txt";
>
> while (<$FILE>) {
>  chomp;
>  some task;
>  ...
> }
>
> close $FILE;
>
>
> Thanks,
> Satya
>


Re: ssh::command

2018-02-13 Thread Chas. Owens
On Tue, Feb 13, 2018 at 1:19 AM Lancelot Mak 
wrote:

> #!/usr/bin/perl -W
>
> use SSH::Command;
>
> $cmdln = `grep $ARGV[0] list.txt`;
> chomp($cmdln);
> ($cmdhost,$user,$pass) = split(':',$cmdln);
> $p = `echo $pass|base64 -d`;
> chomp($p);
>
> $cmdlog = ssh_execute(
> host => $cmdhost,
> username => $user,
>
password => $p,, i
>
command => "$ARGV[1]",
> );
>
>
> print $cmdlog;
>
> print "\n";
>

Testing on a Mac OS machine, the I found it consistently prints out 8192
characters.  This should be immediately recognizable as a magic number (8k
or 2**13).  This tells me that SSH::Command (or the underlying libssh2
library) has an 8k buffer and once it is full, it no longer returns any
data.  Looking at the code for SSH::Command, I see the following function:

# Execute command and get answer as text
sub execute_command_and_get_answer {
my ($ssh2, $command) = @_;

my $chan = $ssh2->channel();

$chan->exec($command);
$chan->read(my $result, 102400);
chomp $result; # remove \n on string tail

return $result;
}

This looks like a fixed read (albeit a longer one that I expected, so there
is probably a fixed buffer in libssh2 too), so that is likely the problem.
Changing the function to read until an end of file is detected:

sub execute_command_and_get_answer {
my ($ssh2, $command) = @_;

my $chan = $ssh2->channel();

$chan->exec($command);

my $result = "";
until ($chan->eof) {
$chan->read(my $buf, 4_096);
$result .= $buf;
}
chomp $result; # remove \n on string tail

return $result;
}

seems to fix the problem.  I am filling a bug against the module, but given
that the last release was in 2009 and the most current version is less than
1.0 (0.7), you may want to find a different module to use.


Re: ssh::command

2018-02-13 Thread Chas. Owens
Can you simplify your code to a short program that had the issue and post
it? Often the act of shortening the program reveals the problem on its own.

On Mon, Feb 12, 2018, 22:37 Lancelot Mak  wrote:

> Hi all,
> I am using SSH::Command module to do ssh stuff but it does not return
> full reply from server. Any clue? It just returns part of it. Is it
> timeout? or what?
> Thanks.
>
>
> --
> --
> Lancelot Mak
>


Re: `$#array` vs `scalar @array`

2018-01-29 Thread Chas. Owens
Luckily in these cases, the faster answer is also the clearest answer in
either case.

On Mon, Jan 29, 2018 at 5:32 PM Paul Johnson  wrote:

> On Sun, Jan 28, 2018 at 10:57:25PM +0000, Chas. Owens wrote:
> > $#array is the index of the last element of @array, so it will be one
> less
> > than scalar @array which is the number of elements in @array (since Perl
> > arrays start with index 0). Therefore, to get the number of elements, you
> > would need to add one to $#array, which makes scalar @array faster. To
> get
> > the last index you would need to say @array - 1, which would make $#array
> > faster.
>
> But it doesn't matter, because this is not a bottleneck in your code.
>
> So code for clarity.
>
>
>
> > On Sun, Jan 28, 2018, 13:39 Peng Yu  wrote:
> >
> > > Hi,
> > >
> > > For the following two expressions, are they of the same speed or one
> > > of them is faster?
> > >
> > > `$#array` vs `scalar @array`
>
> --
> Paul Johnson - p...@pjcj.net
> http://www.pjcj.net
>


Re: `$#array` vs `scalar @array`

2018-01-28 Thread Chas. Owens
$#array is the index of the last element of @array, so it will be one less
than scalar @array which is the number of elements in @array (since Perl
arrays start with index 0). Therefore, to get the number of elements, you
would need to add one to $#array, which makes scalar @array faster. To get
the last index you would need to say @array - 1, which would make $#array
faster.

On Sun, Jan 28, 2018, 13:39 Peng Yu  wrote:

> Hi,
>
> For the following two expressions, are they of the same speed or one
> of them is faster?
>
> `$#array` vs `scalar @array`
>
> --
> Regards,
> Peng
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: What is the substitute for $#?

2018-01-26 Thread Chas. Owens
Before it was removed, the docs say it was:

The output format for printed numbers. This variable is a half-hearted
attempt to emulate awk's OFMT variable. There are times, however, when awk
and Perl have differing notions of what counts as numeric. The initial
value is "%.ng", where n is the value of the macro DBL_DIG from your
system's float.h. This is different from awk's default OFMT setting of
"%.6g", so you need to set $# explicitly to get awk's value. (Mnemonic: #
is the number sign.)

So, you can probably get away with

printf "%.15g\n", $var;

You can find the exact value of DBL_DIG on your system with the following C
code:

#include 
#include 

int main(int argc, char** argv) {
printf("%d\n", DBL_DIG);
return 0;
}

Just put that in a file named dbl_dig.c and run

cc dbl_dig.c -o dbl_dig

and then run it:

./dbl_dig



On Fri, Jan 26, 2018 at 4:36 PM Peng Yu  wrote:

> Hi, $# is deprecated. Could anybody let me know what is the substitute for
> it?
>
> https://perldoc.perl.org/perlvar.html
>
> $# was a variable that could be used to format printed numbers. After
> a deprecation cycle, its magic was removed in Perl v5.10.0 and using
> it now triggers a warning: $# is no longer supported.
>
> --
> Regards,
> Peng
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Problem with perlbrew and LiteSpeed

2017-12-05 Thread Chas. Owens
Test one: does the file actually exist.

It is possible that the user is different, or something else in the park is
wrong

Test two: are the permissions on the file and the directories leading up to
the file correct.

If the process can't see the file, then there will be a problem.

Test three: Is there config right?

It is possible that the server defines a location the cgi script must be in
and you have put the file in the wrong place (or used an absolute path
instead of a relative path in the config).

On Tue, Dec 5, 2017, 10:18 SSC_perl  wrote:

> I'm hoping someone has seen this before and can point me in the
> right direction.  I'm moving my site to a VPS to be able to use a more
> modern version of Perl, but I've run into a problem.
>
> I installed perlbrew along with Perl 5.26.1.  That went smoothly.
> However, when I try to run even a small test script in a browser, the
> server returns this:
>
> lscgid: execve():/home/user/www/cgi-bin/test.pl: No such file or directory
>
> The perlbrew installation on my Mac works perfectly, but on this
> *nix server, it doesn't**.  The VPS is using LiteSpeed, but I've been told
> it's a drop-in replacement for Apache, so everything *should* work the same
> as before.
>
> Do I need to do something more than just putting the perlbrew perl
> path in the shebang line?
>
> Has anyone seen anything like this before?  I'm working with my
> web host on this, but so far they don't seem to know what's going on, so I
> thought I'd ask here just in case.
>
> Thanks,
> Frank
>
> ** Running the script in terminal works properly, just not in the browser.
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: alternative to feature 'signatures'?

2017-11-19 Thread Chas. Owens
What no one has said so far is the importance of using Carp when throwing
errors related to how the function was called.  The Carp module provides
versions of warn (carp) and die (croak) that give the line and file where
the call to the function occurred rather than the line of the carp or croak:

#!/usr/bin/perl

use strict;
use Carp;
use warnings;

sub takes_two {
if (@_ != 2) {
croak "bad number of arguments: [", join(", ", @_), "]";
}
}

sub not_as_good {
if (@_ != 2) {
die "bad number of arguments: [", join(", ", @_), "]";
}
}

eval { takes_two(1, 2, 3); 1 } or warn $@;
eval { not_as_good(1, 2, 3); 1 } or warn $@;


On Sun, Nov 19, 2017 at 1:55 PM Gil Magno  wrote:

> On 19/11/17 15:40, Gil Magno wrote:
> > $num_of_params = scalar @_;
>
> Complementing... In order to get the number of params, you could do
>
> $num_of_params = @_;
>
> without using "scalar @_", because this assignment is already in scalar
> context.
>
> But if you're in list context, you have to use "scalar", as in these
>
> say scalar @_;
> print scalar @_;
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Distinguish module loading error: Compilation fail vs. not existing

2017-11-17 Thread Chas. Owens
Try::Tiny is a good, if flaky option. It has several obscure failure modes.
You might want to look at some of the newer keyword based modules. I
haven't tried them yet, so I don't have a strong opinion.

On Fri, Nov 17, 2017, 13:45 Simon Reinhardt  wrote:

> Thanks for your answer.
> Maybe I should use Try::Tiny:
>
> #!/usr/bin/env perl
> use 5.020;
> use warnings;
> use strict;
>
> use Try::Tiny;
> use Module::Load;
>
> my $module= 'AB::CD';
>
> try {
> autoload($module);
> }
> catch {
> if ($_ =~ /Compilation failed in require/) {
> say "compilation failed";
> }
> elsif ($_ =~ /Can't locate .* in \@INC/) {
> say "module not found";
> }
> };
>
>
> Am 17.11.2017 um 18:06 schrieb Chas. Owens:
> > This is probably the best technique to use.  I would note that your code
> > is not handling exceptions in the safest way.
> >
> > You can increase the safety of your code by saying:
> >
> > eval {
> > autoload($module);
> > 1; #force true value on success
> > } or do {
> > if ($@ =~ /Compilation failed in require/) {
> > say "compilation failed";
> > }
> > elsif ($@ =~ /Can't locate (.*) in \@INC/) {
> > say "module $1 not found";
> > } else {
> > say "unknown error: $@"
> > };
> >
> > This isn't 100% safe, but it covers the most common issue.  You can read
> > more at https://metacpan.org/pod/Try::Tiny#BACKGROUND
> >
> > On Fri, Nov 17, 2017 at 11:31 AM Simon Reinhardt  > <mailto:si...@keinstein.org>> wrote:
> >
> > Hi list,
> >
> > I need to check the cause of a module loading error.
> > Currently I'm parsing the text of the thrown exception (see below).
> This
> > seems to work, but is there a more idiomatic way?
> >
> > Best,
> > Simon
> >
> > #!/usr/bin/env perl
> > use 5.020;
> > use warnings;
> > use strict;
> >
> > use Module::Load;
> >
> > my $module= 'AB::CD';
> >
> > eval {
> > autoload($module);
> > };
> >
> > if ($@) {
> > if ($@ =~ /Compilation failed in require/) {
> > say "compilation failed";
> > }
> > elsif ($@ =~ /Can't locate .* in \@INC/) {
> > say "module not found";
> > }
> > }
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> > <mailto:beginners-unsubscr...@perl.org>
> > For additional commands, e-mail: beginners-h...@perl.org
> > <mailto:beginners-h...@perl.org>
> > http://learn.perl.org/
> >
> >
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Distinguish module loading error: Compilation fail vs. not existing

2017-11-17 Thread Chas. Owens
This is probably the best technique to use.  I would note that your code is
not handling exceptions in the safest way.

You can increase the safety of your code by saying:

eval {
autoload($module);
1; #force true value on success
} or do {
if ($@ =~ /Compilation failed in require/) {
say "compilation failed";
}
elsif ($@ =~ /Can't locate (.*) in \@INC/) {
say "module $1 not found";
} else {
say "unknown error: $@"
};

This isn't 100% safe, but it covers the most common issue.  You can read
more at https://metacpan.org/pod/Try::Tiny#BACKGROUND

On Fri, Nov 17, 2017 at 11:31 AM Simon Reinhardt 
wrote:

> Hi list,
>
> I need to check the cause of a module loading error.
> Currently I'm parsing the text of the thrown exception (see below). This
> seems to work, but is there a more idiomatic way?
>
> Best,
> Simon
>
> #!/usr/bin/env perl
> use 5.020;
> use warnings;
> use strict;
>
> use Module::Load;
>
> my $module= 'AB::CD';
>
> eval {
> autoload($module);
> };
>
> if ($@) {
> if ($@ =~ /Compilation failed in require/) {
> say "compilation failed";
> }
> elsif ($@ =~ /Can't locate .* in \@INC/) {
> say "module not found";
> }
> }
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Perl invocations

2017-10-29 Thread Chas. Owens
Shawn Corey misstated the issue, it isn't that -w can't be turned off, the
problem is that it is turned on globally rather than lexically. That is, it
forces warnings onto modules that may have been designed to not use
warnings:

$ cat T.pm
package T;

sub foo {
my $x = shift;
# undef or string is perfectly cromulant here
return $x + 1;
}

1;
$ cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

use T;

print T::foo, "\n";
$ perl t.pl
1
$ perl -w t.pl
Use of uninitialized value $x in addition (+) at T.pm line 6.
1




On Sun, Oct 29, 2017 at 7:29 PM John W. Krahn  wrote:

> On Sun, 2017-07-02 at 11:16 -0400, Shawn H Corey wrote:
> > On Sun, 2 Jul 2017 14:29:25 +0200
> > Eric de Hont  wrote:
> >
> > > What it boils down to: use warnings as well as -w works, but -w is
> > > considered old fashioned.
> >
> > The problem with -w is that it can't be turned off.
>
> $ perl -le'
> use warnings;
> my $x;
> {   no warnings;
> print $x;
> }
> print $x;
> '
>
> Use of uninitialized value $x in print at -e line 7.
>
> $ perl -wle'
> my $x;
> {   local $^W = 0;
> print $x;
> }
> print $x;
> '
>
> Use of uninitialized value $x in print at -e line 6.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: do something when using Gtk3

2017-09-05 Thread Chas. Owens
Here is how one of the example programs access the Glib timeout:

https://github.com/dave-theunsub/gtk3-perl-demos/blob/master/search_entry.pl#L126

I would have pointed to docs, but they don't seem to be as easy to find as
they used to be in the Gtk1/Gtk2 days.

On Tue, Sep 5, 2017 at 1:58 AM hw  wrote:

> Shlomi Fish  writes:
>
> > Hi hw,
> >
> > On Mon, 28 Aug 2017 04:54:04 +0200
> > hw  wrote:
> >
> >> Hi,
> >>
> >> when writing a program that uses Gtk3, how can I make it so that the
> >> program does something at regular time intervals?
> >>
> >
> > This DDG search - https://duckduckgo.com/?q=gtk3+timer&ia=qa - has some
> > relevant results including
> >
> https://stackoverflow.com/questions/8352027/gtk-timer-how-to-make-a-timer-within-a-frame
> .
> > There are examples for using it in Perl in t/9.t here -
> > https://git.gnome.org//browse/perl-Glib .
>
> Thanks, at least this some indication that it might be possible.  Now I
> need to figure out how to use it with a perl program that uses gtk3 ...
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: reading from (TCP) sockets

2017-08-12 Thread Chas. Owens
Sockets cannot tell you how much data will come in any language.  You HAVE
to rely on Content-Length, that is what it is for.  Why do you think "It
doesn´t
seem wise to rely [it]"?

It is not possible for a loop to both be blocking (which means it is using
no CPU until signaled there is data) and use 100% of CPU.

Why are you reading data directly from a socket yourself?  Especially if it
is HTTP data?  There are modules for this that handle all of the
intricacies for you.  For example, if you want to fetch a webpage, you
should say

use strict;
use LWP::UserAgent;
use warnings;

my $ua = LWP::UserAgent->new;

my $response = $ua->get("http://ifconfig.io/ip";);

unless ($response->is_success) {
die "could not fetch IP address from ifconfig.io: ",
$response->status_line;
}

print $response->decoded_content, "\n";



On Sat, Aug 12, 2017 at 11:20 PM hw  wrote:

> Hi,
>
> when reading data from a TCP socket, how do I tell when I have received
> all the data I should have received?
>
> Particularly, I´m trying to read data sent by a web server, so I´m
> opening a socket, send an HTTP request and receive an answer.
>
> Apparently I need to read from the socket in an endless loop.  I really
> don´t like that because it blocks the program and creates 100% CPU load,
> and I can´t tell if there is more data to be read or not.  It doesn´t
> seem wise to rely on the Content-Length: header to figure out whether I
> received all data or not.
>
> I guess I´m doing it all wrong because I couldn´t find a documentation
> about using sockets which isn´t anything but confusing and leaves lots
> of questions.  I also guess I need something event-driven, i. e. send a
> request, and in the event that the server sends data, somehow receive it
> in the background.
>
> The application would need to do a few things in the background because
> there would be several types of events.  Forking a process for every
> event doesn´t seem right, and it provides me with the problem of
> exchanging data between the various processes (at least unless I let
> every process do its job and exit and fork another one to do basically
> the same thing again).
>
> Is perl the wrong language to do this?  What would you suggest?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: OOP: a class using a class that is descended from it?

2017-08-04 Thread Chas. Owens
>
> What happens when you bless something in a module?
>

In Perl 5 an object is a reference that has been blessed (with a package
name).  The namespace of that package is used to find the methods
associated with the object.  If no methods can be found, then the @ISA
package variable is checked to see if any other packages should be searched
(this is how inheritance works).

So,

my $ref = { foo => 1 };
my $object = bless $ref, "Some::Class";

Will instantiate an object of class Some::Class.  It is convention (but not
required) for a class to contain a method named new that performs the
proper set of steps to create an object:

package Some::Class;

sub new {
my $class = shift;
my %self = @_;

return bless \%self, $class;
}

package main;

my $object = Some::Class->new(foo => 1);

How do I implement interfaces in perl?
>

Interfaces are needed in languages that have strong typing.  Perl 5 has
duck typing (if it walks like a duck, quacks like a duck, etc), so if you
want to treat two classes the same way, you can just manually make sure the
both have the same method names.

A better way to deal with this are roles, but that isn't part of the
vanilla language.


> BTW, why are classes based on upside-down inheritance called virtual
> classes?  They´re as real as others.
>

To enforce a contract, you can create a virtual class (note, unlike in C++,
Java, etc, there is no real difference between virtual and real classes in
Perl 5) that both inherit from.  The class should look something like this:

package Duck::Interface;

sub quack {
my $class = ref $_[0] ? ref $_[0] : %_[0];

die "$class is very naughty and didn't implement quack";
}

In languages with strong typing (like C++, Java, etc), you cannot
instantiate class that have been marked virtual or contain methods that are
marked virtual.  Perl 5 does not have this concept, so in truth, there is
no such thing as a virtual class in Perl 5 (we just have things that sort
of look like one as I showed above).


Re: OOP: a class using a class that is descended from it?

2017-08-04 Thread Chas. Owens
On Fri, Aug 4, 2017 at 11:52 AM hw  wrote:

> > Often you will see a module that contains one package statement which
> leads to the confusion.
>
> Huh? How many package statements is a module supposed to contain?
> And doesn´t a package statement turn a module into a package?
>

No, package statements are not required to make a module. All that is
required to make a module is a file that is valid Perl code whose last line
evaluates to true.

It is convention for a module to start with a package statement declaring a
package with the same name as the module, but that is not a requirement.

Package statements just create a namespace.  You don't even really need the
package statement to that though.  This is a module with its own namespace
with no package statements:

use strict;
use warnings;

sub Odd::Module::foo {
print "this is an odd module with its own namespace\n";
}

1;

The package statement just makes it so all functions and package variables
declared after it don't have to be fully qualified.

A not uncommon pattern is to have a module that has multiple packages
inside it:

package Main::Module;

package Main::Module::Helper;

1;


Re: OOP: a class using a class that is descended from it?

2017-08-04 Thread Chas. Owens
On Fri, Aug 4, 2017 at 9:25 AM hw  wrote:
snip

> Now I´m confused as to what is a module and a package.  Both are files.
>

No, packages are not files.  A package is created by a package statement:

package Foo;

If you do not explicitly create a package with a package statement, then
you are in the main package. A package is a namespace.  Functions and
package variables created in one are associated with it.

A module is a file that is loaded by a require or use statement.  The only
other requirement is that it end with a true value (usually 1).

Often you will see a module that contains one package statement which leads
to the confusion.


Re: perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'

2017-08-03 Thread Chas. Owens
On Thu, Aug 3, 2017 at 3:29 PM hw  wrote:

> David Mertens wrote:
> It is nonsense to logically negate a string, and it is nonsense to convert
> undefined values into 'false'.


Negating strings is a well defined operation in Perl 5.  The following
values in Perl 5 are false: undef, 0, 0.0, "", "0", and the empty list.
Negation turns any value that is true into a false value (PL_sv_no) and any
value that is false into a true value (PL_sv_yes).


> Either are neither false, nor true.
>

You are coming to Perl 5 with assumptions that do not hold water here.  As
I said above (and in earlier emails), there are two strings ("0" and "")
that are false and all other strings are true.  The undef value is also
false.


> For undefined values, there is no way of deciding whether they are true or
> false
> because they are undefined.
>

That would be true if Perl 5 used a trinary logic system, but if you peruse
the documentation, you will find no mention of it.  The undef value in Perl
5 is not analogous to NULL in SQL.  It is merely a sentinel value (sort of
like NULL in C, but with no defined value, so it doesn't take up a value
that could otherwise be used).  Perl 5 happily coerces this sentinel value
into 0 or "" depending on context (though it will throw a warning if use
the warnings pragma).

Perl 5 has no boolean type.  Instead it has true values and false values.
This works very well in most cases.  It would be nearly impossible to
retrofit a boolean type at this point and maintain backwards compatibility,
so the chances of you getting what you want is very unlikely.

If this is a real problem for you, then I suggest you chose a language that
more accurately reflects the way you expect it to work.  If you are
required to use Perl 5 by your work, I suggest you find a job that lets you
use the language you want to use.  Life is too short to beat your head
against the wall and you don't seem to be listening to the people here.


Re: Trap undefined barewords in subroutines

2017-08-02 Thread Chas. Owens
I believe your best bet is to use string eval instead of do and injecting
"no strict qw/vars/;" into the code (to prevent your use strict from
requiring the variables be declared).  I would spend a bit of time trying
to understand where these files come from and fixing that though.  There
has to be a better way of transmitting the data.

#!/usr/bin/perl

use strict;
use JSON;
use warnings;

my $perl_file = shift;
my $perl_code = "no strict qw/vars/;" . do {
local $/;
open my $fh, "<", $perl_file or die "could not open $perl_file: $!";
<$fh>;
};

my $result = eval $perl_code or do {
print "The perl code:\n\n$perl_code\n\ndied with $@";
exit;
};

print encode_json $return;


On Wed, Aug 2, 2017 at 2:55 PM Dyer, Dustin E. (KSC-VAH10) <
dustin.e.d...@nasa.gov> wrote:

> How can I detect and/or error on undefined barewords in perl subroutines?
>
>
>
> I receive data files in perl, and I’d like to read them into Matlab.  Some
> of the files contain barewords that I don’t have an implementation for.
> When I process these files using the *do* command, the undefined
> barewords are silently treated as 0.  This is undesirable because I’d have
> to look through all files provided to me, detect undefined barewords. And
> if I miss one, my data is invalid.
>
>
>
> From what I can tell, Matlab’s perl command returns a character array
> that’s printed to the terminal in perl.  So my process is: run the provided
> file with do, encode the result as json, print the result for use in
> Matlab, and convert the json string into a Matlab struct.
>
>
>
> So is there something different I can do to cause an error when provided
> files contain undefined barewords?
>
>
>
> #start Received file
>
> $someVar = [map {$_ - 250 * FT2M} 1,2,3,];  #FT2M is undef
>
> {
>
>   variable => "SomethingCool",
>
>   data => $ someVar
>
> }
>
>
>
> #end Received file=
>
>
>
> #start my script 
>
> use strict;
>
> use warnings;
>
> use JSON;
>
>
>
> my $json = JSON->new;
>
> my $return = do $ARGV[0];  #using ARGV to get file from user
>
> print encode_json $return;
>
>
>
> #end my script =
>
>
>
> Thanks,
>
> Dustin
>
>
>


Re: debug perl package

2017-07-17 Thread Chas. Owens
Perl has a built in debugger.  You can say

perl -d abc.pl

And it will stop at the first executable line (ignoring BEGIN blocks and
use statements).  You can then step through or over the code.  See
https://perldoc.perl.org/perldebug.html or perldoc perldebug for more
information.

On Mon, Jul 17, 2017 at 3:48 PM Asad  wrote:

> Hi All ,
>
>   I am new to perl , I a have a abc.pl script and abc.pm module .
> I want to understand when I execute abc.pl hw to get to a debug state to
> identify what values does it take . Any GUI interface available to see the
> flow of events.
>
>
> --
> Asad Hasan
> +91 9582111698 <+91%2095821%2011698>
>


Re: Filehandle within foreach loop

2017-07-12 Thread Chas. Owens
That code will read each line from each file.  The problem is likely in the
part that says:

#[process the lines & hash construction.]

What are you doing there?

On Wed, Jul 12, 2017 at 3:23 PM perl kamal  wrote:

> Hello All,
>
> I would like to read multiple files and process them.But we could read the
> first file alone and the rest are skipped from the while loop. Please
> correct me where am i missing the logic.Thanks.
>
> use strict;
> use warnings;
> my @files=qw(Alpha.txt Beta.txt Gama.txt);
>
> foreach my $file (@files)
> {
> open (my $FH, $file) or die "could not open file\n";
>
> while( my $line = <$FH> ){
> #[process the lines & hash construction.]
> }
> close $FH;
> }
>
> Regards,
> Kamal.
>


Re: IPC::Open3 and output

2017-07-10 Thread Chas. Owens
I don't think I understand what you are saying. Neither provides data to
perl until you read from $file; however, it is important to note that the
results of the two functions are not the same.  The filehandle resulting
from the open contains both STDOUT and STDERR from the command (due to the
shell redirecting STDERR into STDOUT), but the filehandle resulting from
the open3 will only have the STDERR from the command.  This might have
something to do with the amount/timing of output available.

On Mon, Jul 10, 2017 at 10:09 AM Mike Martin  wrote:

> Thanks for that, now I have another issue.
> Unlike a piped open, open 3 does not seem to produce output immediately
>
> Is there any way to use pipes with open3 so that the FH has content before
> looping
> ie: this produces output
>
> my $pid=open($file, "-|","$cmd{$sopts->{subname}} 2>&1")
>
> while
> $pid=open3(undef,undef,$file,$cmdprog, @args)
>
> does not until you iterate over the FH
>
> thanks
>
>
>
>
>
> On 10 July 2017 at 07:13, Chas. Owens  wrote:
>
>> On Sun, Jul 9, 2017, 19:37 Mike Martin  wrote:
>>
>>> Hi
>>> I am trying to use Open3 to capture stderr and avoiding the shell ie:
>>> my $str="-v 35 -i /data/Downloads/testinput.mp4 -c:v libx264 -preset
>>> fast -crf 28 -g 25  -c:a  libmp3lame -b:a 128000  -threads 4 -af
>>> volume=2.5  -vf scale='352:trunc(ow/((1/sar)*dar)/2)*2',fps='fps=
>>> 20',setsar='1/1'   -profile:a aac_he_v2 -strict -2 -y
>>> /home/mike/testopen.mp4";
>>>   use Text::ParseWords;
>>> @args = quotewords('\s+', "'", $str);
>>>
>>> use IPC::Open3;
>>>
>>> local(*HIS_IN, *HIS_OUT, *ERR);
>>> my $cmd='ffmpeg';
>>> my $pid=open3(undef, undef, *ERR,$cmd, @args) or die print "$!";
>>> my $line;
>>>
>>> while (sysread ERR, $line, 256){
>>> print $line;
>>> }
>>> waitpid($pid, 0);
>>>
>>> However the output is buffered for around 2 minutes at a time rather
>>> immediately being printed
>>>
>>> Is there any way around this
>>>
>>
>> Are you certain the buffering is in the read from ERR and not in the
>> print? STDERR is not supposed to be buffered. Try adding
>>
>> $| = 1;
>> Near the top of your program. That will turn off any buffering of the
>> Perl 5 script's STDOUT.
>>
>> By default, Perl 5 will buffer STDOUT if it isn't writing to a
>> pseudoterminal (eg if you are piping the output of the Perl 5 script to
>> something else like tee.)
>>
>>
>>
>
>


Re: IPC::Open3 and output

2017-07-09 Thread Chas. Owens
On Sun, Jul 9, 2017, 19:37 Mike Martin  wrote:

> Hi
> I am trying to use Open3 to capture stderr and avoiding the shell ie:
> my $str="-v 35 -i /data/Downloads/testinput.mp4 -c:v libx264 -preset fast
> -crf 28 -g 25  -c:a  libmp3lame -b:a 128000  -threads 4 -af volume=2.5  -vf
> scale='352:trunc(ow/((1/sar)*dar)/2)*2',fps='fps= 20',setsar='1/1'
> -profile:a aac_he_v2 -strict -2 -y /home/mike/testopen.mp4";
>   use Text::ParseWords;
> @args = quotewords('\s+', "'", $str);
>
> use IPC::Open3;
>
> local(*HIS_IN, *HIS_OUT, *ERR);
> my $cmd='ffmpeg';
> my $pid=open3(undef, undef, *ERR,$cmd, @args) or die print "$!";
> my $line;
>
> while (sysread ERR, $line, 256){
> print $line;
> }
> waitpid($pid, 0);
>
> However the output is buffered for around 2 minutes at a time rather
> immediately being printed
>
> Is there any way around this
>

Are you certain the buffering is in the read from ERR and not in the print?
STDERR is not supposed to be buffered. Try adding

$| = 1;
Near the top of your program. That will turn off any buffering of the Perl
5 script's STDOUT.

By default, Perl 5 will buffer STDOUT if it isn't writing to a
pseudoterminal (eg if you are piping the output of the Perl 5 script to
something else like tee.)


Re: perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'

2017-07-06 Thread Chas. Owens
On Thu, Jul 6, 2017 at 9:33 AM hw  wrote:

> False and true are genuinely numeric.  You can´t say for a string
> whether it is true or false; it is a string.
>

This is not a true statement in Perl.  All values in Perl can be true or
false.  And the prototypical true and false values, PL_sv_yes and PL_sv_no,
are dualvars that hold multiple values (string, int, and double).  These
values are returned by the various logical operators (and any other XS code
that wants to use them).

The following values in Perl are false: the empty list, undef, "", 0, 0.0,
and "0".

All other values are true.

These give you different results, and that is just wrong.  I did
> assign a /number/ to $i and never a string.


No, you assigned the result of the negation operator which returns
PL_sv_yes if the operand is false (see above for the list of false values)
or PL_sv_no if the operand is true.  Since 0 is false, !0 return PL_sv_yes
(which contains "1", 1, and 1.0).  Since since PL_sv_yes is true, !!0
returns PL_sv_no (which contains the values "", 0, and 0.0).  If you want
to ensure the result is an integer value 0 or 1, then you need to say:

$i = defined $i ? 0+!!$i : 0;

The venus secret operator (0+) forces the result to be an integer or double
(depending on what $i contained).  Another option is to use the int
operator:

$i = defined $i ? int !!0 : 0;


Re: perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'

2017-07-06 Thread Chas. Owens
On Thu, Jul 6, 2017 at 9:38 AM hw  wrote:

> Chas. Owens wrote:
> >
> >
> > On Sat, Jul 1, 2017, 12:44 Shlomi Fish  shlo...@shlomifish.org>> wrote:
> >
> > Hi Shawn!
> >
> > On Sat, 1 Jul 2017 11:32:30 -0400
> > Shawn H Corey mailto:shawnhco...@gmail.com>>
> wrote:
> >
> > > !!$i which is !(!(0)) which is !(1) which is 0
> > >
> >
> > I suspect !1 returns an empty string in scalar context.
> >
> >
> > !1 returns PL_sv_no (an internal scalar variable). It is a dualvar that
> contains the empty string, the int 0 and the double 0.0. depending on the
> context the value is used in, it will be one of those values.
> >
> > Many people think it is an empty string because the print function
> forces string context.
> >
>
>
> perl -e 'my $i =0; $i = defined($i) ? (!!$i) : 0; use Data::Dumper; print
> Dumper($i);'
>
>
> A printing function should not force a context but use the context it is
> operating in.
>
> How do you print something without having a string context enforced upon
> you?
>

String context will always be force by printing (you don't print integers,
you print characters).  If you want to ensure that a dualvar uses the
double (or integer if the double doesn't exist), you use the venus secret
operator (so named because it looks like the Greek symbol for Venus  ♀):
0+.  Secret operators aren't really operators (or secret), but are just a
set of operators and arguments that are used idiomatically.

for example:

perl -E 'my $i = !!0; print "\$i as a string: [$i]\n\$i as an int: [",
0+$i, "]\n"'
$i as a string: []
$i as an int: [0]

Data::Dumper is not the best way to see what a variable holds if you care
about types. The Devel::Peek module gives you a much better picture, but
you do need to know a bit about perl (ie the interpreter) to understand it
(because Perl, the language, doesn't care about types):

perl -MDevel::Peek -E 'my $i = !!0; Dump $i'
SV = PVNV(0x7f88488036b0) at 0x7f884882a0b8
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x7f8848405180 ""\0
  CUR = 0
  LEN = 10

Here we can see that $i is a PVNV, that is it holds a string (Pointer
Value) and a number (Number Value).  The flags IOK, NOK, and POK tell us
that the IV (integer), NV (double), and PV (string) portions are all safe
to use.

Now let's look at $i after the venus secret operator runs:

perl -MDevel::Peek -E 'my $i = 0+!!0; Dump $i'
SV = IV(0x7febe102a0a8) at 0x7febe102a0b8
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 0

Now we can see that $i is an IV and it only has an IV section (and the
corresponding flag).

But be careful, you can taint this be using $i in a string context:

 perl -MDevel::Peek -E 'my $i = 0+!!0; "$i"; Dump $i'
SV = PVIV(0x7fd841004e20) at 0x7fd841002eb8
  REFCNT = 1
  FLAGS = (IOK,POK,pIOK,pPOK)
  IV = 0
  PV = 0x7fd84070b3f0 "0"\0
  CUR = 1
  LEN = 10

>From this we can see that using an IV in string context will cause perl to
cache the string version of the IV.  But note that, unlike PL_sv_no, the
string value is actually "0" not "".  This is mostly an issue for
serialization (JSON in particular).

Just for illustration of why the flags are important:

perl -MDevel::Peek -E 'my $i = 0+!!0; Dump $i; "$i"; Dump $i; $i++; Dump $i'
SV = IV(0x7f941402a0a8) at 0x7f941402a0b8
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 0
SV = PVIV(0x7f941402c020) at 0x7f941402a0b8
  REFCNT = 1
  FLAGS = (IOK,POK,pIOK,pPOK)
  IV = 0
  PV = 0x7f9413c04eb0 "0"\0
  CUR = 1
  LEN = 10
SV = PVIV(0x7f941402c020) at 0x7f941402a0b8
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 1
  PV = 0x7f9413c04eb0 "0"\0
  CUR = 1
  LEN = 10

$i started off as an IV, but gets promoted to a PVIV by being used in
string context.  Then math is done on the IV portion which invalidates the
cached string, so the POK flag is removed.  The cached value of "0" is
still present in the variable, but properly behaving code will not access
it (only XS code could even see it).

Just don't ask me about the pIOK, pNOK, and pPOK flags, 'cause I have never
understood what they represent.


Re: perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'

2017-07-01 Thread Chas. Owens
On Sat, Jul 1, 2017, 12:44 Shlomi Fish  wrote:

> Hi Shawn!
>
> On Sat, 1 Jul 2017 11:32:30 -0400
> Shawn H Corey  wrote:
>
> > !!$i which is !(!(0)) which is !(1) which is 0
> >
>
> I suspect !1 returns an empty string in scalar context.
>

!1 returns PL_sv_no (an internal scalar variable). It is a dualvar that
contains the empty string, the int 0 and the double 0.0. depending on the
context the value is used in, it will be one of those values.

Many people think it is an empty string because the print function forces
string context.

>


Re: [solved] Re: Module to extract patterns

2017-06-13 Thread Chas. Owens
You can also look for the =~ operator and then print the next significant
token:

#!/usr/bin/perl

use strict;
use PPI;

# warnings should always go last
# https://stackoverflow.com/a/38639882/78259
use warnings;

my $file = shift or die "Need a file name!\n";

my $document = PPI::Document->new( $file );

my $tokens = $document->find(
sub {
my ($self, $token) = @_;
$token->isa('PPI::Token::Regexp') or
$token->isa('PPI::Token::Operator');
}
) || [];

print "\n$file :\n";

for my $token ( @$tokens ) {
if ($token->isa("PPI::Token::Regexp")) {
print "\t", $token->content, "\n";
next;
}

next unless $token->content eq "=~";

my $next_token = $token->snext_sibling;

next if $next_token->content =~ m{^(?:[ms].|/)};

print "\t", $next_token->content, " at line ",
$next_token->location->[3], "\n";
}


On Tue, Jun 13, 2017 at 10:47 AM Lars Noodén  wrote:

> On 06/13/2017 05:14 PM, Chas. Owens wrote:
> > Two notes:
> >
> > Firstly, $document->find will return undef if no regexes are found, not
> an
> > empty arrayref, so you should say
> [snip]
>
> > Secondly, PPI does not catch all things that can be considered regexes,
> for
> > instance:
> [snip]
>
> Thanks.  I appreciate the observations.  On that second one, I figure it
> is all but impossible to catch some cases and am simply hoping that they
> are not going to be in the code base I scan.  I will try some other
> searches on the side to see if something like this shows up:
>
> m/ =~ \s+ \$\w+ \s+ \? /x
>
> Regards
> Lars
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: [solved] Re: Module to extract patterns

2017-06-13 Thread Chas. Owens
Two notes:

Firstly, $document->find will return undef if no regexes are found, not an
empty arrayref, so you should say

my $regex = $document->find('PPI::Token::Regexp') || [];

or

for my $expr ( @[ $regex || [] ] ) {

or even

print qq(\n$file :\n);

if (my $regex = $document->find('PPI::Token::Regexp')) {
for my $expr ( @$regex ) {
print qq(\t),$expr->content,qq(\n);
}
} else {
print "\tno matches\n";
}

Whatever makes the most sense to you.

Secondly, PPI does not catch all things that can be considered regexes, for
instance:

#!/usr/bin/perl

use strict;
use warnings;

my $regex = "foo";

print "foo" =~ $regex ? "matched" : "no match", "\n";

This is perfectly valid Perl 5 code, but PPI won't recognize it as
containing a regex.


On Tue, Jun 13, 2017 at 8:49 AM Lars Noodén  wrote:

> Ok. Thanks, Paul and David.
>
> I think I see how I can benefit from Text::Balanced but I now have my
> start with PPI and can list the expressions.
>
> Regards,
> Lars
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use PPI;
> use Data::Dumper;
>
> my $file = shift or ( die("Need a file name!\n") );
>
> my $document = PPI::Document->new( $file );
>
> my $regex = $document->find('PPI::Token::Regexp');
>
> # print Data::Dumper->Dump($regex),qq(\n);
>
> print qq(\n$file :\n);
>
> for my $expr ( @$regex ) {
> print qq(\t),$expr->content,qq(\n);
> }
>
> exit( 0 );
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Time::HiRes output

2017-05-24 Thread Chas. Owens
You can use printf or sprintf to control the format, but what you are doing
is called profiling and it is better to use an actual profiler. Take a look
at Devel::NYTProf

http://search.cpan.org/~timb/Devel-NYTProf-6.04/lib/Devel/NYTProf.pm

https://www.perl.org/about/whitepapers/perl-profiling.html

On Wed, May 24, 2017, 22:12 SSC_perl  wrote:

> I’m timing sub routines to get an idea of where my scripts spend
> the most of their time.  This is an example of what I’m doing:
>
> use Time::HiRes qw( clock );
> my $clock0 = clock();
> ... # Do something.
> my $clock1 = clock();
> my $clockd = $clock1 - $clock0;
>
> I’m getting values like $clock1 = 0.030259 and $clock0 =
> 0.030212.  However, instead of $clockd being 0.47, it’s returning
> 4.700019e-05.  I take it that’s exponential notation (?) but it’s
> actually more difficult to read than the regular number.  Is there a way to
> get it to print out differently?
>
> Thanks,
> Frank
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Getting a specific page using WWW::Mechanize

2017-04-25 Thread Chas. Owens
Are you using JavaScript to redirect the user to the sorry page?  If so
then WWW::Mechanise won't redirect.  It doesn't understand JavaScript.
Happily, there are drop in replacements for it that do:

http://search.cpan.org/~oalders/WWW-Mechanize-1.84/lib/WWW/Mechanize/FAQ.pod#Which_modules_work_like_Mechanize_and_have_JavaScript_support
?

On Tue, Apr 25, 2017 at 12:06 PM SSC_perl  wrote:

> Here’s more information on this situation.  SurfShop uses two main
> HTML templates, main.htm and secure.htm.  Then there are sub-templates that
> get loaded as the content of those two main templates.
>
> During checkout, secure.htm is used.  If the order is successful,
> receipt.htm is used as the sub-template, if not, then sorry.htm is shown
> with the error message.  This, of course, works as expected when placing an
> order by hand.
>
> When placing a successful order with WWW::Mechanize, receipt.htm
> is shown correctly in $mech->content().  However, if the order was
> declined, that’s when Mech returns the same page it was on when the
> ‘processTransaction’ button was clicked, which is confirm.htm.
>
> Up until this point, Mech performs just like when I manually place
> an order, so what could be causing it to get mixed up at the end?  I don’t
> think I'm doing anything special, but you never know.  Here’s a snippet of
> the code where the process fails:
>
>
> $mech->submit_form(
> form_name => 'ccForm',
> fields=> {
>   Ecom_Payment_Card_Type  => 'VISA',
>   Ecom_Payment_Card_Number=> ‘',
>   Ecom_Payment_Card_Verification  => '123',
>   Ecom_Payment_Card_ExpDate_Month => '05',
>   Ecom_Payment_Card_ExpDate_Year  => '2024',
>  },
> button=> 'processTransaction',
> );
>
> As you can see, it’s pretty standard stuff.
>
> Is there another method I can use besides $mech->content() to see
> what’s being returned?  The more tests I can do on this, the more it will
> help me find where I went wrong.
>
> Frank
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: are state objects ok?

2017-04-24 Thread Chas. Owens
State variables are just like my variables but with a different lifetime,
so it is safe (assuming it would be safe to use my variables that life for
the lifetime of the program). In this case, what happens if you lose
database access and then reconnect? What happens if you have two database
handles? Your statement handles are going to be bad or refer to the wrong
DB. What you really want here is the $dbh->prepare_cached method call. It
will correctly handle both scenarios and avoid reparsing the SQL.

On Mon, Apr 24, 2017, 20:02 lee  wrote:

> Hi,
>
> is it ok to assign an object to a state variable?  Or does doing so
> involve a chance of causing problems because objects are not supposed or
> designed to be used with the state-feature?
>
> Example:
>
>
> use feature qw(state);
> use DBI;
>
>
> sub foo {
>   my ($dbh, $q, $finish) = @_;
>
>   state $sth = $dbh->prepare("SELECT foo FROM bar WHERE baz = ? LIMIT 1");
>   if($finish) {
> $sth->finish();
> return 0;
>   }
>
>   $sth->execute($q);
>   my ($ret) = $dbh->selectrow_array($sth);
>
>   return $ret;
> }
>
>
> #
> # do some stuff like connecting to database etc.
> #
>
> foreach (1..10) {
>   my $z = foo($dbh, $_, 0);
>   #
>   # do more stuff with $z
>   #
>   my $x = foo($dbh, $z, 0);
>   #
>   # ...
>   #
> }
>
> foo($dbh, 1);
> exit 0;
>
>
> I think it would be nicer to keep statement handles ($sth) contained within
> the functions that use them instead of defining them in the main part and
> handing them over to the functions.  Their very purpose is that you define
> a handle once and use it multiple times, thus saving the overhead of
> interpreting the statement every time it is used.
>
> But can I safely use a state-variable like this?  If not, then what?
>
>
> --
> "Didn't work" is an error.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: why moose

2017-04-24 Thread Chas. Owens
The main benefits I see are

1. You have to write less code
2. Roles provide the benefits of multiple inheritance without the insanity
3. Introspection of Moose classes is easier
4. Type safety (which is really just points 1 and 3 again)

The biggest one is 1. Moose is basically a declarative language for
creating classes. It creates a lot of the code for you based on what you
say you want. Sure you could write that code, but it is all boilerplate
stuff and I would rather spend my time working logic than boilerplate. It
also provides nice abstract concepts like before, after, and around
methods. Sure, you could just use $self-SUPER::method(@_) at the right, but
the new keywords provide a more self documenting way of expressing the
relationship.

Sure, Moose can't do anything that Perl 5's OO can't do because Moose it's
written using Perl 5's OO, but, in the end, Moose is already written and
provides a lot of shortcuts (once you learn how to use it)

On Mon, Apr 24, 2017, 20:40 Peng Yonghua  wrote:

> Hi,
>
> I saw many modules begin to write with Moose.
>
> For me I wrote my perl modules most time with OO style, I think perl's
> native OO works just fine.
>
> So why needs moose? thanks.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Scalar References - oxymoron?

2017-04-04 Thread Chas. Owens
On Mon, Apr 3, 2017 at 6:55 PM SSC_perl  wrote:

> Reading http://perldoc.perl.org/perlreftut.html I see it’s
> possible to create a scalar reference.  What situation would require
> someone to create a reference to a scalar?  I thought refs were only useful
> for passing complex data structures.  Is it just because Perl lets you, or
> is there a reason I’m not aware of?  It seems like it would just be
> redundant.


It is mostly used to avoid copying large amounts of data.  If you are using
Perl 5.20.0 or newer, this is not necessary as long as you aren't changing
the data as strings are now COW (copy-on-write).  Prior to Perl 5.20.0 if
you had code like:

my $big = "a" x (1024*1024*1024); # a gigabyte of 'a's

sub foo (
my $arg = shift;
);

foo($big)

then it had to copy the whole gigabyte into a new string.  If this was
something you expected, you could (and probably still should if you need to
modify the string) say

sub foo (
my $ref = shift;

# do stuff with $$ref
);

and that would not create a copy.

Other uses include

* flyweight/inside-out objects:
http://www.perl.com/pub/2003/06/13/design1.html#flyweight
* Signaling/metadata (eg the open function will open an in-memory file if
passed a scalar ref of the file's contents)
* in/out function parameters without having to use the clunky $_[index]
aliasing (not recommended, really only useful in languages that don't have
multiple return values)
* storing the same scalar in more than data structure (warning this can
lead to spooky-action-at-a-distance bugs)


Re: how to recursion

2017-03-29 Thread Chas. Owens
On Tue, Mar 28, 2017 at 9:27 PM PYH  wrote:

> Hi,
>
> what's the better way to write a recursion in perl's class?
>
> sub my_recursion {
>  my $self = shift;
>  
>  if (...) {
>  $self->my_recursion;
>  }
> }
>
> this one?
>

Define better. In general that is the right form (assuming there is some
side effect to calling my_recursion that will cause the if statement to be
false).  If your function is tail recursive and it has the potential to be
deeply nested, then you can take advantage of a quirk of goto to make it
faster and use less memory:

#!/usr/bin/perl

use strict;
use warnings;

{ package A;
sub new { bless {}, shift }

sub recur {
my ($self, $n) = @_;

return "recur done" if $n <= 0;
return $self->recur($n - 1);
}

sub tail_recur {
my ($self, $n) = @_;

return "tail_recur done" if $n <= 0;
@_ = ($self, $n - 1);
goto &tail_recur;
}
}

my $o = A->new;

print $o->recur(1_000_000), "\n";
print $o->tail_recur(1_000_000), "\n";

NOTE: this only works if the code is tail recursive.  That is, the
recursive function does nothing but return the next call's return value.
It works by replacing the function with itself rather than pushing another
copy of the function on the stack.


Re: Calling Perl from AppleScript [SOLVED]

2017-03-07 Thread Chas. Owens
It is entirely possible that Applescript is intentionally changing
directory to a temporary directory before executing external programs as a
security measure.

On Tue, Mar 7, 2017, 00:41 debt  wrote:

> Please disregard my last email.  Apparently you have to supply
> full paths (as Thomas mentioned) to everything when dealing with
> inter-language communications.  After I added the full path to the file
> name in the Perl script, it worked as it should.  I don’t understand why,
> but at least I know now.
>
> In case anyone else is dealing with this, Perl (or any other
> language, I'm guessing) will need the full path to any file when being
> called from AppleScript.  Even though the Perl script worked fine without
> it when run directly, it's required when being called from AppleScript.
>
> Thanks again,
> Marc
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Uninitialized Value, but It Isn't, so Why?

2017-03-02 Thread Chas. Owens
On Thu, Mar 2, 2017, 19:32 Shawn H Corey  wrote:

> On Thu, 2 Mar 2017 16:35:17 -0600
> Andy Bach  wrote:
> > Hah! "undef" is an uninitialized value !
>
> $ perl -we 'if (not $interdest5) {$interdest5 = "";} print
> "|$interdest5|\n"'
> ||
> $ perl -we 'if (! $interdest5) {$interdest5 = "";} print
> "|$interdest5|\n"'
> ||
>

Or

$ perl -we 'unless ($interdest5) {$interdest5 = ""}
print "|$interdest5|\n";'
||

Or

$ perl -we '$interdest5 //= ""; print "|$interdest5|\n";'
||

TIMTOWTDI


Re: multiple named captures with a single regexp

2017-03-01 Thread Chas. Owens
/(\w+)/g gets the command as well and only the args are wanted, so it would
need to be

my @args = $s =~ / (\w+)/g;
shift @args;

also,

my VAR if TEST;

is deprecated IIRC and slated to be removed soon (as it's behavior is
surprising).  It would probably be better to say

my @args = $s =~ /^\w+\s/ && $s =~ /(?:\s+(\w+))/g;

or (if you don't like using && like that)

my @args = $s =~ /^\w+\s/ ? $s =~ /(?:\s+(\w+))/g : ();



On Wed, Mar 1, 2017 at 9:34 AM X Dungeness  wrote:

> On Wed, Mar 1, 2017 at 2:52 AM, Chas. Owens  wrote:
> > Sadly, Perl will only capture the last match of capture with a
> qualifier, so
> > that just won't work.  The split function really is the simplest and most
> > elegant solution for this sort of problem (you have a string with a
> > delimiter and you want the pieces).  All of that said, if you are
> willing to
> > modify the regex you can say
> >
> > my $s = "command arg1 arg2 arg3 arg4";
> > my @args = $s =~ /(?:\s+(\w+))/g;
> >
>
> Hm, I'd write it as:
>  my @args = $s =~ / (\w+)/g;
>
> or, if the command check isn't too inelegant:
>
>  my @args =  $s =~ / (\w+)/g if $str =~ /^command\s/;
>
>
> > for my $arg (@args) {
> > print "$arg\n";
> > }
> >
> > However, this does not allow you to check the command is correct.
> >
>


Re: multiple named captures with a single regexp

2017-03-01 Thread Chas. Owens
Sadly, Perl will only capture the last match of capture with a qualifier,
so that just won't work.  The split function really is the simplest and
most elegant solution for this sort of problem (you have a string with a
delimiter and you want the pieces).  All of that said, if you are willing
to modify the regex you can say

my $s = "command arg1 arg2 arg3 arg4";
my @args = $s =~ /(?:\s+(\w+))/g;

for my $arg (@args) {
print "$arg\n";
}

However, this does not allow you to check the command is correct.

Another option, and I would in no way claim this is an elegant solution, is
to use code execution in the middle of the regex with (?{}) to pull out the
matched fields:

@args = ();

my $start;
$s =~ m{
\w+ # command
\s
(?{$start = pos;}) # capture the first start position
(?:
\w+ # the argument
# capture the argument
(?{ push @args, substr $s, $start, pos() - $start; })
# optional delimiter and capture the next start
(?: \s+ (?{ $start = pos; }))?
)+
}x;

for my $arg (@args) {
print "$arg\n";
}

Of course, all of these solutions are bound to fail when you hit the real
world (assuming the command is a Unix command) as arguments are allowed to
have spaces in them if they are quoted.  There is a way to do this with
regex, but balancing the quotes is far more pain than it is worth.  A
simple regex to tokenize the string plus some logic to put the quoted
sections back together will allow you to extract the arguments from the
string:

#!/usr/bin/perl

use strict;
use warnings;

my $s = qq("command with space" arg1 "arg 2" "arg3");

my @parts = $s =~ /([ ]+|"|\w+)/g;

my @args;
my $in_string = 0;
my $buf = "";
while (@parts) {
my $part = shift @parts;

# ditch the delimiters if not in a string
next if not $in_string and $part =~ / /;

# in strings, a " means end the string
# otherwise, just build up a buffer of the things
# in the string
if ($in_string) {
if ($part eq '"') {
$in_string = 0;
push @args, $buf;
$buf = "";
} else {
$buf .= $part;
}
next;
}

# if not in a string, " means start a string
if ($part eq '"') {
$in_string = 1;
next;
}

# if not a delimiter or a ", then this is just a normal token
push @args, $part;
}

shift @args; #ditch the command

for my $arg (@args) {
print "$arg\n";
}

Of course, this still doesn't handle Unix commands properly as you can
escape " and use ' to create strings, but those details are left as an
exercise for the reader.




On Wed, Mar 1, 2017 at 4:04 AM Luca Ferrari  wrote:

> Hi all,
> I'm not sure if this is possible, but imagine I've got a line as follows:
>
> command arg1 arg2 arg3 arg4 ...
>
> I would like to capture all args with a single regexp, possibly with a
> named capture, but I don't know exactly how to do:
>
> my $re = qr/command\s+(?\w+)+/;
>
> the above of course is going to capture only the first one (one shoot)
> or the last one within a loop.
> How can I extract the whole array of arguments?
>
> Please note, a raw solution is to remove the command and split, but
> I'm asking for a more elegant solution.
>
> Thanks,
> Luca
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: interesting regex delimiters

2017-02-24 Thread Chas. Owens
Be careful, it isn't actually a regex; it is a string that will be compiled
to a regex.  You can see one difference here:

#!/usr/bin/perl

use v5.20;
use warnings;

say "string matches:";
for my $s ("foo", "AfooZ") {
say "\t$s: ", $s =~ "\Afoo\Z" ? "true" : "false";
}

say "regex matches:";
for my $s ("foo", "AfooZ") {
say "\t$s: ", $s =~ /\Afoo\Z/ ? "true" : "false";
}

which outputs

Unrecognized escape \A passed through at t.pl line 8.
Unrecognized escape \Z passed through at t.pl line 8.
string matches:
foo: false
AfooZ: true
regex matches:
foo: true
AfooZ: false

To my knowledge, the only delimiters that do not require m or qr before
them are // and ??; however, they are not equivalent (and ?? must be m?? as
of Perl 5.22).  The m?? operator only matches the first time it sees a
pattern and then will not match again until reset is called:

#!/usr/bin/perl

use v5.18;
use warnings;

for ("fo", "foo", "fooo", "f") {
my ($match) = ?(fo+)?;
say $match // "no match";
if (/fooo/) {
reset;
}
}

Which outputs

Use of ?PATTERN? without explicit operator is deprecated at t.pl line 7.
fo
no match
no match
f


On Thu, Feb 23, 2017 at 6:53 PM Andrew Solomon  wrote:

> Thanks Uri!
>
> On Thu, Feb 23, 2017 at 10:32 PM, Uri Guttman  wrote:
>
> On 02/23/2017 05:19 PM, Andrew Solomon wrote:
>
> Running Perl 18.2 I was surprised to discover that I can use single and
> double quotes as regex delimiters without the 'm' operator.
>
> For example, instead of writing
>
> "/usr/bin/perl" =~ m"/perl"
>
> I can just write
>
> "/usr/bin/perl" =~ "/perl"
>
> Can anyone point me to the documentation indicating which delimiters don't
> need the 'm' operator?
>
>
> you actually are thinking in the wrong direction. the =~ operator causes
> its right side to always be a regex unless the s/// or m// or tr/// ops are
> seen there. you can even use an expression or sub call or anything on the
> right of =~ and it will be parsed as a regex (if no op is there as i just
> said).
>
> you can easily check this out with something simple like "/usr/bin/perl"
> =~ "/pe" . "rl".
>
> so it isn't the delimiters as you think but the =~ op itself that makes it
> a regex.
>
> uri
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>
>
>
> --
> Andrew Solomon
>
> Mentor@Geekuni http://geekuni.com/
> http://www.linkedin.com/in/asolomon
>


Re: Extra text printed by Data::Dumper

2017-02-03 Thread Chas. Owens
You are splitting on /,/ but your name and value are separated by a space.
This means $var_name is being set to "database $database" not "database"
and $var_value is undef (the undefined value).  This whole routine is
suspect though.  What if you want to pass an array or a hash?  You can't
pass them as a string.  Also, what is up with turning off strict?  Anytime
you feel turning off strict is a good idea, it is time revisit what you are
doing and why (there are reasons, but the chances of you stumbling into
them are slim; when time comes, you will know).

A better option is to pass either a hashref or (as I have done below) a
list into your logging function:

#!/usr/bin/perl

use strict;
use Data::Dumper;
use File::Path;
use warnings;

my $database = "db1";
my @matches = ("foo", "bar", "baz");
my %image = ( height => 10, width => 10, imgdata =>
"12122349129321938127387123" );

datadump('log_name', '>>',
database => $database,
matches  => \@matches,
image=> \%image,
);

sub datadump {
my $filename = shift;
my $mode = shift;
my %vars = @_;

mkpath "temp_logs";

local $Data::Dumper::Useqq = 1;

my $file = "temp_logs/TESTS-$filename.log";
open my $log, $mode, $file or die "could not open $file for writing: $!";

print $log Dumper \%vars or die "could not write to $file";

close $log or die "could not close $file: $!";
}


Which writes the following file:

$VAR1 = {
  "database" => "db1",
  "matches" => [
   "foo",
   "bar",
   "baz"
 ],
  "image" => {
 "width" => 10,
 "height" => 10,
 "imgdata" => "12122349129321938127387123"
   }
};



On Fri, Feb 3, 2017 at 7:20 PM SSC_perl  wrote:

> I have the following sub that creates a log file using
> Data::Dumper.  It also lists the name of each of the variables that it’s
> fed.  It works great except for one problem - every line is appended with “
> = undef;” (minus the quotes).
>
> I’ve tried stripping it out of $var_value with regex but that
> doesn’t work, so it appears that Data::Dumper is adding that text during
> the print command.
>
> Does anyone know how I can eliminate that extra text from being
> printed?  My searches have come up empty.
>
> Thanks,
> Frank
>
> 
>
> datadump('log_name', '>>',
> "database $database",
> "matches $matches",
> "image $image",
> );
>
> sub datadump {
> my $filename = shift;
> my $write= shift;
> my @var_info = @_;
> no strict;
> open(my $log, $write, "temp_logs/TESTS-$filename.log");
> use Data::Dumper;
> foreach my $var (@var_info) {
> my @vars = split (/,/ => $var);
> my $var_name = '$'.$vars[0];
> my $var_value = $vars[1];
> print {$log} Data::Dumper->Dump( [$var_value], [$var_name] );
> $count++;
> }
> close($log);
> use strict;
> }
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: application starter kit

2017-01-23 Thread Chas. Owens
I am still not quite understanding what you want. Are you saying you want
to create a CPAN module that installs a script (and possible some modules
as well)? If so, then take a look at any of the existing modules that do
that like App::cpanminus. I believe App is the namespace for that sort of
module. In particular, look at the EXE_FILES section of its Makefile.PL and
the modules layout.

http://cpansearch.perl.org/src/MIYAGAWA/App-cpanminus-1.7042/Makefile.PL

On Mon, Jan 23, 2017, 04:44 Luca Ferrari  wrote:

> On Mon, Jan 23, 2017 at 10:39 AM, Chas. Owens 
> wrote:
> > I think you are saying you want to be able to create one file you can
> give
> > to someone else and have them run a Perl 5 program you have written
> without
> > having to install all of the modules (and possibly even perl itself).  If
> > this is the case, then you are in luck.  There are a couple of solutions
> > that exist today:
>
> No, sorry, I misexplained my aim: I want to be able to do all normal
> CPAN stuff like installing my module, testing, getting dependencies,
> but with the final result of installing an executable instead of a
> module.
> Your solutions are really interesting, and while I already knew about
> par and docker, fatpack has been an interesting hint, but I'm looking
> for something simpler.
>
> Luca
>


Re: application starter kit

2017-01-23 Thread Chas. Owens
I think you are saying you want to be able to create one file you can give
to someone else and have them run a Perl 5 program you have written without
having to install all of the modules (and possibly even perl itself).  If
this is the case, then you are in luck.  There are a couple of solutions
that exist today:

PAR and PAR::Packer

PAR (Perl Archive) is similar in concept to JAR (Java Archive).  The script
and all of the modules needed to run it are combined into one file.
PAR::Packer takes this a step farther and bundles the perl executable as
well.

http://search.cpan.org/dist/PAR/lib/PAR.pm
http://search.cpan.org/dist/PAR-Packer/lib/pp.pm

App::FatPacker

This is another take on the idea of bundling the script and all of its
dependencies.   I do not believe it bundles the perl executable though.

http://search.cpan.org/dist/App-FatPacker/bin/fatpack

Another popular solution is to use a Linux Container like Docker.  With a
container, you build a as much of a machine as you need to run your app and
then distribute that.  The users then run the container in a special
sandboxed area of the OS.

http://www.infoworld.com/article/3072929/linux/containers-101-linux-containers-and-docker-explained.html

On Mon, Jan 23, 2017 at 3:52 AM Luca Ferrari  wrote:

> Hi all,
> now this should be really trivial, so please point me to documentation
> if I'm missing something.
> I have collected a bunch of perl 5 applications to do some tasks, and
> I would like to make them modular, in the sense make them as
> individual distributions.
> I would like to have some module-starter like application to
> build/import the structure for having a distro with executables
> (pretty much no modules at all).
> Any suggestion?
>
> Thanks,
> Luca
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: How to get a called perl script to write output to a file with DIFFERENT user:group than the calling app?

2017-01-15 Thread Chas. Owens
snip

> So I guess the question is -
>
> - is there a way in perl to authorize the callED perl script to have
> higher perms than the callING app's, so that it can write to the file I'm
> targeting?
>
> Or do I have to to this OUTSIDE of the perl script?
>

The short answer is that this is OS dependent. The longer answer is in
Linux, only if the script is running as root (directly, through suid, or
via sudo) and you probably don't want to do that. A better question is the
script is writing to a file owned by a different user.  That is outside of
the normal way of doing things in UNIX.


Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread Chas. Owens
On Sun, Jan 15, 2017, 16:19  wrote:

Hi,

On Sun, Jan 15, 2017, at 01:01 PM, Shawn H Corey wrote:
> > Is there a different, recommended way?
>
> Nothing's wrong. perlcritic does not this valid method, that's all.
>
> TIMTOWTDI (There Is More Than One Way To Do It.)

Hm, ok.  As long as it's not wrong/broken in some weird way.

I kept getting scolded to "check your code with perlcritic" as if that's
the *right* way to do it.  So when I kept getting that perlcritic-ism, I
started looking around for why.  LOTS of post telling you different things
NOT to do, but nothing that explained in a way I could understand why what
I was doing wasn't cool.

I must've read & parsed that "--verbose 11" output  a dozen times.  Just
isn't understandble :-(

So, anyway, sticking with what I got until if/when I understand the point!

Thanks alot.

AJ


In this case, what you are doing "wrong" is trusting the caller of the
function.  Good defensive practices will save you a ton of headache in the
future.  Your code expects the first (and only) argument to be a hashref.
Proper checking of the arguments will save maintainers tons of time.  For
instance, examine these error messages:

Can't use string ("FN") as a HASH ref while "strict refs" in use at sig.pl
line 9.
Not a HASH reference at sig.pl line 9.

And now look at these error messages:

wrong number of arguments at sig.pl line 13.
main::better("FN", 1, "AR", 1, "AD", 1, "DR", 1) called at sig.pl
line 34
eval {...} called at sig.pl line 36
argument isn't a hashref at sig.pl line 15.
main::better(ARRAY(0x7fc08b0292a8)) called at sig.pl line 41
eval {...} called at sig.pl line 43

These error messages were produced by the following code.  In both cases
(calling with a list instead of a hashref and calling with an arrayref
instead of a hashref), the first set of error messages only tell you where
the code blew up, not where the programmer made an error.  In the second
set of error messages, you get better location information and better
overall information.

Now, this is obviously better for the maintainer, but it comes at the cost
of more verbose code.  If this is a one-off script, then it probably isn't
worthwhile; however, if this is code that is meant to live for any
appreciable length of time, then verbode handling is best.

#!/usr/bin/perl

use strict;
use Carp;
use warnings;


sub not_so_good {
my %args = %{ shift @_ };
}

sub better {
croak "wrong number of arguments" unless @_ == 1;
my $arg = shift @_;
croak "argument isn't a hashref" unless ref $arg eq ref {};
my %args = %$arg;
}

eval {
not_so_good(FN => 1, AR => 1, AD => 1, DR => 1);
1;
} or do {
print "got error: $@";
};

eval {
not_so_good([FN => 1, AR => 1, AD => 1, DR => 1]);
1;
} or do {
print "got error: $@";
};

eval {
better(FN => 1, AR => 1, AD => 1, DR => 1);
1;
} or do {
print "got error: $@";
};

eval {
better([FN => 1, AR => 1, AD => 1, DR => 1]);
1;
} or do {
print "got error: $@";
};


Re: dereferencing an object

2016-11-08 Thread Chas. Owens
On Tue, Nov 8, 2016 at 5:38 AM  wrote:

> Hi,
>
> Yes, I was using a ref with my dumper. When i don't do that I don't get
> $VAR1 = \{ .
> I am still getting an error when I dereference.
>
> my $name = ${$hash_ref_decode}->{items}[0]{content};

snip

This says dereference $hash_ref_decode as a scalar then dereference that
value as a hash.  Your dumper output (and the name of the variable) seems
to show that $hash_ref_decode holds a hashref.  In this case, you should
probably say

my $name = $hash_ref_decode->{items}[0]{content};


Re: api request

2016-10-12 Thread Chas. Owens
Looking at the documentation for curl, it says:

-d/--data 
(HTTP) Sends the specified data in a POST request to the HTTP server, in
the same way that a browser does when a user has filled in an HTML form and
presses
the submit button. This will cause curl to pass the data to the server
using the content-type application/x-www-form-urlencoded. Compare to
-F/--form.

-d/--data is the same as --data-ascii. To post data purely binary, you
should instead use the --data-binary option. To URL-encode the value of a
form field
you may use --data-urlencode.

If any of these options is used more than once on the same command line,
the data pieces specified will be merged together with a separating
&-symbol. Thus,
using ’-d name=daniel -d skill=lousy’ would generate a post chunk that
looks like ’name=daniel&skill=lousy’.

If you start the data with the letter @, the rest should be a file name to
read the data from, or - if you want curl to read the data from stdin. The
con-
tents of the file must already be URL-encoded. Multiple files can also be
specified. Posting data from a file named ’foobar’ would thus be done with
--data
@foobar.

This means that the content-type header must be
application/x-www-form-urlencoded and the parameters token, sync_token, and
resource_types must be should be in the request body.

The documentation for LWP::UserAgent says:

$ua->post( $url, \%form )

This method will use the POST() function from HTTP::Request::Common to
build the request. See HTTP::Request::Common
 for a
details on how to pass form content and other advanced features.

POST $url, $form_ref, Header => Value,...

The $form_ref argument can be used to pass key/value pairs for the form
content. By default we will initialize a request using the
application/x-www-form-urlencodedcontent type.

So, the following should be identical to what the curl command does:

#!/usr/bin/perl

use strict;
use LWP::UserAgent;
use warnings;

my $ua = LWP::UserAgent->new;

$ua->post("https://todoist.com/API/v7/sync";, {
token => "yourtokengoeshere",
sync_token => "*",
resource_types => '["projects"]',
});

die $ua->status_line unless $ua->is_success;

print $ua->decoded_content;


On Wed, Oct 12, 2016 at 12:03 PM  wrote:

>
>
>
>
> I would like to get some info from my todoist account with a perl script.
>
> The website provides an example using python and bash.
>
> curl https://todoist.com/API/v7/sync -d token=yourtokengoeshere
>-d sync_token='*' -d resource_types='["projects"]'
>
>
>
> The curl command works but I would like to use perl and make a todo list
> script that will sync with todoist. There is a cpan module but it doesn't
> work.
>
>
>
> How would I use perl to get the info? I have this
>
>
>
> #!/usr/bin/perl
>
> use strict;
>
> use warnings;
>
> use LWP::UserAgent;
>
>
>
> my $url = 'https://todoist.com/API/v7/sync';
>
> my $token  = 'mytoken';
>
> my $ua = new LWP::UserAgent();
>
> my $response = $ua->post($url, token => $token);
>
>
>
> I have no idea how to pass my token or resource types to the api.
>
> Thank you for any help
>
>
>
> --
>
> Derrick Cope
>
>
>
> --from my mutt
>
>
>
> --
>
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>
> For additional commands, e-mail: beginners-h...@perl.org
>
> http://learn.perl.org/
>
>
>
>
>
>


Re: Counting elements returned from expression

2016-10-05 Thread Chas. Owens
Yep, that is pretty much it.  Infix operators can make things like that
confusing.  For instance,

#!/usr/bin/perl

use strict;
use warnings;

my @a = qw/a b c/;
my @b = qw/d e f/;

my $x = ("a", "b", "c", "d", "e", "f");
my $y = (@a, @b);
my $z = (@a[0 .. $#a], @b[0 .. $#b]);

print "x $x y $y z $z\n";


This prints "x f y 3 z f".  What is going on is a little more clear when
you look at the warnings:

Useless use of a constant ("a") in void context at comma.pl line 9.
Useless use of a constant ("b") in void context at comma.pl line 9.
Useless use of a constant ("c") in void context at comma.pl line 9.
Useless use of a constant ("d") in void context at comma.pl line 9.
Useless use of a constant ("e") in void context at comma.pl line 9.
Useless use of private array in void context at comma.pl line 10.
Useless use of array slice in void context at comma.pl line 11.

This is one of the ways you can prove that you can't have a list in scalar
context.  Lists flatten inside other lists (ie you can't have nested
lists), but because the comma operator is being acted upon by the scalar
context, you aren't getting lists, you are getting sequences.  The sequence
operator (a comma in scalar context) evaluates its left hand side in void
context*, then its right hand side in scalar context.  So, the first
assignment looks like this:

my $x = ("a", ("b", ("c", ("d", ("e", ("f"));

"a"  is evaluated in void context (producing a warning) then ("b", ("c",
("d", ("e", ("f")) is evaluated in scalar context
"b"  is evaluated in void context (producing a warning) then ("c", ("d",
("e", ("f") is evaluated in scalar context
"c"  is evaluated in void context (producing a warning) then ("d", ("e",
("f"))) is evaluated in scalar context
...
"f" is evaluated in scalar context (returning the value "f")

The second assignment looks like this:

@a is evaluated in void context (producing a warning) then @b is evaluated
in scalar context (returning the number of items in @b).

The third assignment is trickier to see because some commas are hidden in
the expression.  The slice @a[0 .. $#a] is equivalent to ($a[0], $a[1],
$a[2]), so it is really:

my $z = ($a[0], ($a[1], ($a[2], ($b[0], ($b[1], ($b[2]));

And it will obviously follow the same pattern as the first assignment,
which is why it returns the same thing (albeit with a slightly different
warning).

* this is not strictly true.  In the past it has been proven that some
expressions on some versions of Perl have evaluated the LHS in a context
other than void, but I don't remember how to trigger that behavior, but the
practical upshot is the same: only the last item in a sequence returns its
value.

On Wed, Oct 5, 2016 at 4:34 PM khalil zakaria Zemmoura <
zemmoura.kha...@gmail.com> wrote:

> I think I had a problem with my approach. I never thought about assignment
> operator as returning something at all !
> When we include that parameter in the "equation" everything is clear.
>
> Since the list assignment is in scalar context, it returns the number of
> items directly and that number is assigned to the scalar variable
>
> I hope I got this time 😁
> Thanks for your answers
>
> Le 5 oct. 2016 06:46, "Chas. Owens"  a écrit :
>
> You almost have it.  What you are missing is that the list assignment
> operator is in scalar context and is returning the count, not the list
> being assigned to.  Infix operators obscure things sometimes.  It is easier
> to understand if we rewrite it as if list assignment was a function call
> instead of an infix operator:
>
> my $count = list_assignment( \@lhs, \@rhs );
>
> In this case, you wouldn't expect the values in @lhs to be affected in any
> way by the assignment of list_assignment's return value to $count.  It is
> the same thing for the list assignment operator.  In void context:
>
> ($x, $y) = qw/a b c/;
>
> it returns nothing, in scalar context:
>
> my $ret = ($x, $y, $z) = qw/a b c/;
>
> if returns the number of items in the RHS list (3), in list context:
>
> my @a = ($x, $y) = qw/a b c/;
>
> it returns the items that were assigned to the LHS (just "a" and "b").
>
>
>
>
>
>
>
>
> On Tue, Oct 4, 2016 at 6:48 PM zakaria  wrote:
>
> To summarize
>
> There is a list assignment and the difference between assigning to an
> empty list and an non empty list is the container(s) inside the liste
> that capture the different values (scalars or lists or hash

Re: Counting elements returned from expression

2016-10-04 Thread Chas. Owens
You almost have it.  What you are missing is that the list assignment
operator is in scalar context and is returning the count, not the list
being assigned to.  Infix operators obscure things sometimes.  It is easier
to understand if we rewrite it as if list assignment was a function call
instead of an infix operator:

my $count = list_assignment( \@lhs, \@rhs );

In this case, you wouldn't expect the values in @lhs to be affected in any
way by the assignment of list_assignment's return value to $count.  It is
the same thing for the list assignment operator.  In void context:

($x, $y) = qw/a b c/;

it returns nothing, in scalar context:

my $ret = ($x, $y, $z) = qw/a b c/;

if returns the number of items in the RHS list (3), in list context:

my @a = ($x, $y) = qw/a b c/;

it returns the items that were assigned to the LHS (just "a" and "b").








On Tue, Oct 4, 2016 at 6:48 PM zakaria  wrote:

> To summarize
>
> There is a list assignment and the difference between assigning to an
> empty list and an non empty list is the container(s) inside the liste
> that capture the different values (scalars or lists or hashes)
>
> for example () ans ($a, @b, %c... )
>
> when we assign to an ampty list the values assigned are not captured
> but the elements assigned are counted
>
> when we evaluate a list in scalar context, we are accessing its length.
>
> So i could translate this expression as following
>
> $s = () = get_hats_clone();
>
> assigning the return value of get_hats_clone() to an empty list that
> dont copy (capture) them but count them, and that assignement
> '() = sub()' have a return value.
> that return value when in list context is the elements of the list it
> self. When in scalar context, it's the number of elements of that list
> (it is empty but stored the number of all the elements returned by
> get_hats_clone()
>
> since we are using scalar context with $s = (), then we get the number
> of all the elements.
>
> Is that correct.
>
> Regards
>
> On Tue, 2016-10-04 at 09:14 +, Chas. Owens wrote:
> >
> >
> > On Mon, Oct 3, 2016 at 10:45 PM Lawrence Statton 
> > wrote:
> > snip
> > > the =()= "operator" just does that without creating a temporary
> > > variable
> > snip
> >
> > Almost, but not quite.  You can see the difference when =()= is in
> > list context:
> >
> > my @a = (my @temp) = get_clown_hat;
> > my @b = () = get_clown_hat;
> >
> > @a will have the items returned by get_clown_hat and @b won't.  The
> > assignment operators have a return value.  For scalar assignment, it
> > is just the scalar being assigned, regardless of the context.  For
> > list assignment in list context it is the list that was assigned:
> >
> > $ perl -E '@a = ($a, $b) = 1 .. 100; say "a is ", join ", ", @a;'
> > a is 1, 2
> > $ perl -E '@a = () = 1 .. 100; say "a is ", join ", ", @a;'
> > a is
> >
> > and the number of items on the right hand side of the assignment when
> > it is in scalar context:
> >
> > $ perl -E '$a = () = 1 .. 100; say "a is $a"'
> > a is 100
> > $ perl -E '$a = ($b) = 1 .. 100; say "a is $a"'
> > a is 100
> >
> > You can see how different approaches compare using the Benchmark
> > module:
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use Benchmark;
> > use warnings;
> >
> > my $max_hats = 100;
> >
> > sub get_clown_hat {
> >   return (0 .. $max_hats);
> > }
> >
> > # here, scalar context is causing the .. operator
> > # to be the flip flop operator instead of the
> > # range operator, so we get 1E0 instead of
> > # 100 (the last item) or 101 (the count)
> > # when not compared to anything, the range operator
> > # compares against $. (the line number of the last
> > # line read from the currently selected file handle)
> > my $flip_flop_result = get_clown_hat;
> >
> > print "wrong result: $flip_flop_result\n";
> >
> > my %subs = (
> >   array => sub {
> >   my @a = get_clown_hat;
> >   return scalar @a;
> >   },
> >   empty => sub {
> >   my $count = () = get_clown_hat;
> >   return $count;
> >   },
> > );
> >
> > for my $sub (keys %subs) {
> > print "$sub: ", $subs{$sub}(), "\n";
> > }
> >
> > for my $n (1_000, 10_000, 100_000) {
> >   $max_hats = $n;
> >
> >   print "\n$n items\n\n";
> >   Benchmark::cmpthese -2, \%subs;
> > }
> >
> > Avoiding copying the data is about twice as fast for counting items:
> >
> > Use of uninitialized value $. in range (or flip) at bench.pl line 11.
> > wrong result: 1E0
> > array: 101
> > empty: 101
> >
> > 1000 items
> >
> >  Rate array empty
> > array 12620/s--  -52%
> > empty 26457/s  110%--
> >
> > 1 items
> >
> > Rate array empty
> > array 1310/s--  -50%
> > empty 2634/s  101%--
> >
> > 10 items
> >
> >Rate array empty
> > array 124/s--  -49%
> > empty 245/s   97%--
>


Re: Re-exec in perl

2016-10-04 Thread Chas. Owens
The first alarm is in a different process (same PID different process) due
to the exec.  I am not sure what is happening, but in general it is never a
good idea to do anything complex in a signal handler.

On Tue, Oct 4, 2016 at 4:21 PM Shawn H Corey  wrote:

> On Tue, 04 Oct 2016 19:32:44 +
> "Chas. Owens"  wrote:
>
> > It looks like the problem exists at the C level as well.  This code
> > doesn't work past the first alarm:
>
> Doesn't it say the alarm has to be reset by the code in the
> documentation? After all, you don't want a second alarm to go off
> before the first handler is finished. If it did, there's a chance of an
> infinite number of alarms to stack up, fill memory, and kill the
> process.
>
>
> --
> Don't stop where the ink does.
>
> Shawn H Corey
> mailto:shawnhco...@nili.ca
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Re-exec in perl

2016-10-04 Thread Chas. Owens
It looks like the problem exists at the C level as well.  This code doesn't
work past the first alarm:

#include 
#include 
#include 
#include 
#include 

int n;
char s[10];
char* prog;

int alarm_called = 0;

void handle_alrm(int signo) {
if (signo == SIGALRM) {
execv(prog, (char*[]) { prog, s, NULL });
}
}


int main(int argc, char** argv) {
prog = argv[0];

if (argc == 1) {
n = 1;
} else {
n = atoi((const char*)argv[1]);
}

printf("run %d\n", n);
n++;

snprintf(s, 10, "%d", n);

signal(SIGALRM, handle_alrm);

alarm(1);
sleep(3);
alarm(0);

return 0;
}

But this code does work:

#include 
#include 
#include 
#include 
#include 

int n;
char s[10];
char* prog;

int alarm_called = 0;

void handle_alrm(int signo) {
if (signo == SIGALRM) {
alarm_called = 1;
}
}


int main(int argc, char** argv) {
prog = argv[0];

if (argc == 1) {
n = 1;
} else {
n = atoi((const char*)argv[1]);
}

printf("run %d\n", n);
n++;

snprintf(s, 10, "%d", n);

signal(SIGALRM, handle_alrm);

alarm(1);
sleep(3);
if (alarm_called) {
execv(prog, (char*[]) { prog, s, NULL });
}
alarm(0);

return 0;
}


On Tue, Oct 4, 2016 at 10:40 AM Chas. Owens  wrote:

> First, never use -w flag to enable warnings; use the warnings pragma
> instead.
>
> Second, you should not exec the script directly, you don't know if it is
> executable or not, and it could even wind up running under a different
> version of perl.  Instead, you should exec same the interpreter that is
> running the current program.  Happily perl stores the path to the current
> interpreter in $^X for you.
>
> It appears as if the process becomes insensitive to the SIG_ALRM signal if
> you run exec within the signal handler.  In general you should avoid doing
> anything in a signal handler other than setting a status variable.  This
> code seems to work the way you were expecting:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $n = shift || 1;
>
> my $exec = 0;
>
> $SIG{ALRM} = sub { die "timeout\n" };
>
> print "\$\$ is $$ this is run $n\n";
>
> alarm 1;
> eval {
> sleep 5;
> alarm 0;
> 1;
> } or do {
> alarm 0; # handle cases where it wasn't a timeout that died
> if ($@ eq "timeout\n") {
> exec $^X, $0, $n + 1;
> }
> };
>
> print "should not get here\n";
>
> What are you trying to solve with this code?  Is this just academic
> playing around?  If not, there may be a much better solution than
> re-execing the script.
>
>
> On Tue, Oct 4, 2016 at 12:13 AM Unknown User 
> wrote:
>
> I am trying to re-exec my script after a delay. I expect that the code
> below would go on re-execing indefinitely, however that does not
> happen. It exits after one reexec. What should be done to make the
> code re-exec forever?
>
> $ cat exec.pl
> #!/usr/bin/perl -w
> use strict;
> alarm(5);
> $SIG{"ALRM"} = sub {
> print "Reexecing\n";
> exec($0,@ARGV);
> };
>
> print join(".",@ARGV);
> sleep 12;
> $ ./exec.pl 1 2 3 4
> 1.2.3.4Reexecing
> 1.2.3.4$
> $
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Re-exec in perl

2016-10-04 Thread Chas. Owens
First, never use -w flag to enable warnings; use the warnings pragma
instead.

Second, you should not exec the script directly, you don't know if it is
executable or not, and it could even wind up running under a different
version of perl.  Instead, you should exec same the interpreter that is
running the current program.  Happily perl stores the path to the current
interpreter in $^X for you.

It appears as if the process becomes insensitive to the SIG_ALRM signal if
you run exec within the signal handler.  In general you should avoid doing
anything in a signal handler other than setting a status variable.  This
code seems to work the way you were expecting:

#!/usr/bin/perl
use strict;
use warnings;

my $n = shift || 1;

my $exec = 0;

$SIG{ALRM} = sub { die "timeout\n" };

print "\$\$ is $$ this is run $n\n";

alarm 1;
eval {
sleep 5;
alarm 0;
1;
} or do {
alarm 0; # handle cases where it wasn't a timeout that died
if ($@ eq "timeout\n") {
exec $^X, $0, $n + 1;
}
};

print "should not get here\n";

What are you trying to solve with this code?  Is this just academic playing
around?  If not, there may be a much better solution than re-execing the
script.


On Tue, Oct 4, 2016 at 12:13 AM Unknown User 
wrote:

I am trying to re-exec my script after a delay. I expect that the code
below would go on re-execing indefinitely, however that does not
happen. It exits after one reexec. What should be done to make the
code re-exec forever?

$ cat exec.pl
#!/usr/bin/perl -w
use strict;
alarm(5);
$SIG{"ALRM"} = sub {
print "Reexecing\n";
exec($0,@ARGV);
};

print join(".",@ARGV);
sleep 12;
$ ./exec.pl 1 2 3 4
1.2.3.4Reexecing
1.2.3.4$
$

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Re: Counting elements returned from expression

2016-10-04 Thread Chas. Owens
On Mon, Oct 3, 2016 at 10:45 PM Lawrence Statton  wrote:
snip

> the =()= "operator" just does that without creating a temporary variable

snip

Almost, but not quite.  You can see the difference when =()= is in list
context:

my @a = (my @temp) = get_clown_hat;
my @b = () = get_clown_hat;

@a will have the items returned by get_clown_hat and @b won't.  The
assignment operators have a return value.  For scalar assignment, it is
just the scalar being assigned, regardless of the context.  For list
assignment in list context it is the list that was assigned:

$ perl -E '@a = ($a, $b) = 1 .. 100; say "a is ", join ", ", @a;'
a is 1, 2
$ perl -E '@a = () = 1 .. 100; say "a is ", join ", ", @a;'
a is

and the number of items on the right hand side of the assignment when it is
in scalar context:

$ perl -E '$a = () = 1 .. 100; say "a is $a"'
a is 100
$ perl -E '$a = ($b) = 1 .. 100; say "a is $a"'
a is 100

You can see how different approaches compare using the Benchmark module:

#!/usr/bin/perl

use strict;
use Benchmark;
use warnings;

my $max_hats = 100;

sub get_clown_hat {
return (0 .. $max_hats);
}

# here, scalar context is causing the .. operator
# to be the flip flop operator instead of the
# range operator, so we get 1E0 instead of
# 100 (the last item) or 101 (the count)
# when not compared to anything, the range operator
# compares against $. (the line number of the last
# line read from the currently selected file handle)
my $flip_flop_result = get_clown_hat;

print "wrong result: $flip_flop_result\n";

my %subs = (
array => sub {
my @a = get_clown_hat;
return scalar @a;
},
empty => sub {
my $count = () = get_clown_hat;
return $count;
},
);

for my $sub (keys %subs) {
print "$sub: ", $subs{$sub}(), "\n";
}

for my $n (1_000, 10_000, 100_000) {
$max_hats = $n;

print "\n$n items\n\n";
Benchmark::cmpthese -2, \%subs;
}

Avoiding copying the data is about twice as fast for counting items:

Use of uninitialized value $. in range (or flip) at bench.pl line 11.
wrong result: 1E0
array: 101
empty: 101

1000 items

 Rate array empty
array 12620/s--  -52%
empty 26457/s  110%--

1 items

Rate array empty
array 1310/s--  -50%
empty 2634/s  101%--

10 items

   Rate array empty
array 124/s--  -49%
empty 245/s   97%--


Re: Counting elements returned from expression

2016-10-03 Thread Chas. Owens
So, list assignment is

my ($foo, $bar, $baz) = ("a", "b", "c");

$foo will be "a", $bar will be "b", etc. There can be more items on the
right hand side and they won't be copied. This operation has a return
value. In list context it is the list of values that got assigned. In
scalar context it is the number of items on the right hand side of the list
assignment.

So, $count will be 3 in

my $count = my ($foo, $bar, $baz) = ("a", "b", "c");

Since it is the number of items on the right hand side of the list
assignment and the number of items on the left hand side doesn't have to
match the right hand side, you can say

my $count = () = ("a", "b", "c");

And $count will be 3.

Hope that helps.

On Mon, Oct 3, 2016, 19:19 khalil zakaria Zemmoura <
zemmoura.kha...@gmail.com> wrote:

> Hi,
> I am reading modern Perl and despite the explanation of the author I
> couldn't understand:
>
> my $count = () = get_clown_hats()
>
> It's obvious to me that the function get_clown_hat() is evaluated in list
> context but what the author said is that
> the assignment to the empty list throws away all of the values of the
> list, but the assignment takes place in scalar context, which evaluates to
> the number of items on the right!
>
> If someone could explain that, it would be great.
>
> Thanks
> Regards
>


Re: DBI and field type 'bit'

2016-10-01 Thread Chas. Owens
The URL for the bug report is
https://rt.cpan.org/Ticket/Display.html?id=118207&results=52bad5c05e442e5750731e7011056012

On Sat, Oct 1, 2016 at 11:44 AM Chas. Owens  wrote:

> Poking around in the source, it does not appear to be well tested WRT bind
> variables (see the test file below).  I will file a bug at
> https://rt.cpan.org/Public/Dist/Display.html?Name=DBD-mysql but I
> wouldn't hold my breath on seeing anything other than a documentation
> change.  There is probably too much code in the darkpan that counts on the
> current behavior (ie making it sensible would break existing code that
> expects it to be broken).
>
> use strict <https://metacpan.org/source/strict>;
> use warnings <https://metacpan.org/source/warnings>;
>
> use Test::More <https://metacpan.org/source/Test::More>;
> use DBI <https://metacpan.org/source/DBI>;
> use vars <https://metacpan.org/source/vars> qw($test_dsn $test_user
> $test_password);
> use lib <https://metacpan.org/source/lib> '.', 't';
> require 'lib.pl';
>
> sub VerifyBit ($) {
> }
>
> my $dbh;
> my $charset= 'DEFAULT CHARSET=utf8';
>
> eval {$dbh = DBI->connect($test_dsn, $test_user, $test_password,
> { RaiseError => 1, AutoCommit => 1}) or ServerError() ;};
>
> if ($@) {
> plan skip_all => "no database connection";
> }
> else {
> plan tests => 15;
> }
>
> if (!MinimumVersion($dbh, '4.1')) {
> $charset= '';
> }
>
> ok $dbh->do("DROP TABLE IF EXISTS dbd_mysql_b1"), "Drop table if exists
> dbd_mysql_b1";
>
> ok( $dbh->do('CREATE TABLE dbd_mysql_b1 (b BIT(8))') );
>
> ok ($dbh->do("insert into dbd_mysql_b1 set b = b''"));
> ok ($dbh->do("insert into dbd_mysql_b1 set b = b'1010'"));
> ok ($dbh->do("insert into dbd_mysql_b1 set b = b'0101'"));
>
> ok (my $sth = $dbh->prepare("select BIN(b+0) FROM dbd_mysql_b1"));
>
> ok ($sth->execute);
>
> ok (my $result = $sth->fetchall_arrayref);
>
> ok defined($result), "result returned defined";
>
> is $result->[0][0], , "should be ";
> is $result->[1][0], 1010, "should be 1010";
> is $result->[2][0], 101, "should be 101";
>
> ok ($sth->finish);
>
> ok $dbh->do("DROP TABLE dbd_mysql_b1"), "Drop table dbd_mysql_b1";
>
> ok $dbh->disconnect;
>
>
> On Sat, Oct 1, 2016 at 11:34 AM Chas. Owens  wrote:
>
> Whoops, meant to include links for the docs to those two functions:
>
> http://perldoc.perl.org/functions/pack.html
> http://perldoc.perl.org/functions/vec.html
>
> On Sat, Oct 1, 2016 at 11:31 AM Chas. Owens  wrote:
>
> DBD::mysql is treating 1 and 3 as their ASCII values on insert due to
> quoting.  You need to create values that are bit fields themselves.  This
> being Perl, there are lots of ways of doing that:
>
> $dbh->do("create table bittest (lilbits bit(8))");
>
> my $insert = $dbh->prepare("insert into bittest values (?)");
> my $select = $dbh->prepare("select * from bittest where lilbits = ?");
>
> $insert->execute(pack "n", 5);
> $insert->execute(pack "b", "101");
>
> vec(my $bitstring, 0, 8) = 5;
>
> $insert->execute($bitstring);
>
> $select->execute(5);
>
> while (my $row = $select->fetch) {
> printf "%08b\n", ord $row->[0];
> }
>
> The first two use the pack function to pack the input into a binary
> representation (n converts integers, b converts strings of 0s and 1s).  The
> third uses the vec function as an lvalue to build a bitstring.
> Interestingly, selecting with bitstrings doesn't seem to work and you have
> to use the number.  I am not sure if this is a bug or not.  The
> inconsistent behavior for select and insert is certainly surprising enough
> that I would expect it to be mentioned in the docs, but I didn't see it.
>
> On Sat, Oct 1, 2016 at 10:35 AM hw  wrote:
>
>
> Hi,
>
> what´s the correct way with DBI to update a field in a mysql database
> when the field type is 'bit'?
>
> I´m getting, for example, converted to integer, 53 instead of 3 into
> the field.
>
> It goes like this:
>
>
> my $sth = $dbh->prepare("INSERT INTO t (`status`) VALUES (?)
>   ON DUPLICATE KEY UPDATE `status` = ?");
>
> $sth->execute(($var eq 'x' ? 1 : 0), ($var eq 'x' ? 3 : 0));
>
>
> That should work just fine but doesn´t in that the wrong bits of the
> field are being set.
>
> Is this a bug or a feature?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: DBI and field type 'bit'

2016-10-01 Thread Chas. Owens
Poking around in the source, it does not appear to be well tested WRT bind
variables (see the test file below).  I will file a bug at
https://rt.cpan.org/Public/Dist/Display.html?Name=DBD-mysql but I wouldn't
hold my breath on seeing anything other than a documentation change.  There
is probably too much code in the darkpan that counts on the current
behavior (ie making it sensible would break existing code that expects it
to be broken).

use strict <https://metacpan.org/source/strict>;
use warnings <https://metacpan.org/source/warnings>;

use Test::More <https://metacpan.org/source/Test::More>;
use DBI <https://metacpan.org/source/DBI>;
use vars <https://metacpan.org/source/vars> qw($test_dsn $test_user
$test_password);
use lib <https://metacpan.org/source/lib> '.', 't';
require 'lib.pl';

sub VerifyBit ($) {
}

my $dbh;
my $charset= 'DEFAULT CHARSET=utf8';

eval {$dbh = DBI->connect($test_dsn, $test_user, $test_password,
{ RaiseError => 1, AutoCommit => 1}) or ServerError() ;};

if ($@) {
plan skip_all => "no database connection";
}
else {
plan tests => 15;
}

if (!MinimumVersion($dbh, '4.1')) {
$charset= '';
}

ok $dbh->do("DROP TABLE IF EXISTS dbd_mysql_b1"), "Drop table if exists
dbd_mysql_b1";

ok( $dbh->do('CREATE TABLE dbd_mysql_b1 (b BIT(8))') );

ok ($dbh->do("insert into dbd_mysql_b1 set b = b''"));
ok ($dbh->do("insert into dbd_mysql_b1 set b = b'1010'"));
ok ($dbh->do("insert into dbd_mysql_b1 set b = b'0101'"));

ok (my $sth = $dbh->prepare("select BIN(b+0) FROM dbd_mysql_b1"));

ok ($sth->execute);

ok (my $result = $sth->fetchall_arrayref);

ok defined($result), "result returned defined";

is $result->[0][0], 1111, "should be ";
is $result->[1][0], 1010, "should be 1010";
is $result->[2][0], 101, "should be 101";

ok ($sth->finish);

ok $dbh->do("DROP TABLE dbd_mysql_b1"), "Drop table dbd_mysql_b1";

ok $dbh->disconnect;

On Sat, Oct 1, 2016 at 11:34 AM Chas. Owens  wrote:

> Whoops, meant to include links for the docs to those two functions:
>
> http://perldoc.perl.org/functions/pack.html
> http://perldoc.perl.org/functions/vec.html
>
> On Sat, Oct 1, 2016 at 11:31 AM Chas. Owens  wrote:
>
> DBD::mysql is treating 1 and 3 as their ASCII values on insert due to
> quoting.  You need to create values that are bit fields themselves.  This
> being Perl, there are lots of ways of doing that:
>
> $dbh->do("create table bittest (lilbits bit(8))");
>
> my $insert = $dbh->prepare("insert into bittest values (?)");
> my $select = $dbh->prepare("select * from bittest where lilbits = ?");
>
> $insert->execute(pack "n", 5);
> $insert->execute(pack "b", "101");
>
> vec(my $bitstring, 0, 8) = 5;
>
> $insert->execute($bitstring);
>
> $select->execute(5);
>
> while (my $row = $select->fetch) {
> printf "%08b\n", ord $row->[0];
> }
>
> The first two use the pack function to pack the input into a binary
> representation (n converts integers, b converts strings of 0s and 1s).  The
> third uses the vec function as an lvalue to build a bitstring.
> Interestingly, selecting with bitstrings doesn't seem to work and you have
> to use the number.  I am not sure if this is a bug or not.  The
> inconsistent behavior for select and insert is certainly surprising enough
> that I would expect it to be mentioned in the docs, but I didn't see it.
>
> On Sat, Oct 1, 2016 at 10:35 AM hw  wrote:
>
>
> Hi,
>
> what´s the correct way with DBI to update a field in a mysql database
> when the field type is 'bit'?
>
> I´m getting, for example, converted to integer, 53 instead of 3 into
> the field.
>
> It goes like this:
>
>
> my $sth = $dbh->prepare("INSERT INTO t (`status`) VALUES (?)
>   ON DUPLICATE KEY UPDATE `status` = ?");
>
> $sth->execute(($var eq 'x' ? 1 : 0), ($var eq 'x' ? 3 : 0));
>
>
> That should work just fine but doesn´t in that the wrong bits of the
> field are being set.
>
> Is this a bug or a feature?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: DBI and field type 'bit'

2016-10-01 Thread Chas. Owens
Whoops, meant to include links for the docs to those two functions:

http://perldoc.perl.org/functions/pack.html
http://perldoc.perl.org/functions/vec.html

On Sat, Oct 1, 2016 at 11:31 AM Chas. Owens  wrote:

> DBD::mysql is treating 1 and 3 as their ASCII values on insert due to
> quoting.  You need to create values that are bit fields themselves.  This
> being Perl, there are lots of ways of doing that:
>
> $dbh->do("create table bittest (lilbits bit(8))");
>
> my $insert = $dbh->prepare("insert into bittest values (?)");
> my $select = $dbh->prepare("select * from bittest where lilbits = ?");
>
> $insert->execute(pack "n", 5);
> $insert->execute(pack "b", "101");
>
> vec(my $bitstring, 0, 8) = 5;
>
> $insert->execute($bitstring);
>
> $select->execute(5);
>
> while (my $row = $select->fetch) {
> printf "%08b\n", ord $row->[0];
> }
>
> The first two use the pack function to pack the input into a binary
> representation (n converts integers, b converts strings of 0s and 1s).  The
> third uses the vec function as an lvalue to build a bitstring.
> Interestingly, selecting with bitstrings doesn't seem to work and you have
> to use the number.  I am not sure if this is a bug or not.  The
> inconsistent behavior for select and insert is certainly surprising enough
> that I would expect it to be mentioned in the docs, but I didn't see it.
>
> On Sat, Oct 1, 2016 at 10:35 AM hw  wrote:
>
>
> Hi,
>
> what´s the correct way with DBI to update a field in a mysql database
> when the field type is 'bit'?
>
> I´m getting, for example, converted to integer, 53 instead of 3 into
> the field.
>
> It goes like this:
>
>
> my $sth = $dbh->prepare("INSERT INTO t (`status`) VALUES (?)
>   ON DUPLICATE KEY UPDATE `status` = ?");
>
> $sth->execute(($var eq 'x' ? 1 : 0), ($var eq 'x' ? 3 : 0));
>
>
> That should work just fine but doesn´t in that the wrong bits of the
> field are being set.
>
> Is this a bug or a feature?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: DBI and field type 'bit'

2016-10-01 Thread Chas. Owens
DBD::mysql is treating 1 and 3 as their ASCII values on insert due to
quoting.  You need to create values that are bit fields themselves.  This
being Perl, there are lots of ways of doing that:

$dbh->do("create table bittest (lilbits bit(8))");

my $insert = $dbh->prepare("insert into bittest values (?)");
my $select = $dbh->prepare("select * from bittest where lilbits = ?");

$insert->execute(pack "n", 5);
$insert->execute(pack "b", "101");

vec(my $bitstring, 0, 8) = 5;

$insert->execute($bitstring);

$select->execute(5);

while (my $row = $select->fetch) {
printf "%08b\n", ord $row->[0];
}

The first two use the pack function to pack the input into a binary
representation (n converts integers, b converts strings of 0s and 1s).  The
third uses the vec function as an lvalue to build a bitstring.
Interestingly, selecting with bitstrings doesn't seem to work and you have
to use the number.  I am not sure if this is a bug or not.  The
inconsistent behavior for select and insert is certainly surprising enough
that I would expect it to be mentioned in the docs, but I didn't see it.

On Sat, Oct 1, 2016 at 10:35 AM hw  wrote:

>
> Hi,
>
> what´s the correct way with DBI to update a field in a mysql database
> when the field type is 'bit'?
>
> I´m getting, for example, converted to integer, 53 instead of 3 into
> the field.
>
> It goes like this:
>
>
> my $sth = $dbh->prepare("INSERT INTO t (`status`) VALUES (?)
>   ON DUPLICATE KEY UPDATE `status` = ?");
>
> $sth->execute(($var eq 'x' ? 1 : 0), ($var eq 'x' ? 3 : 0));
>
>
> That should work just fine but doesn´t in that the wrong bits of the
> field are being set.
>
> Is this a bug or a feature?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Data type of attributes

2016-09-29 Thread Chas. Owens
This is what the meta object is for:

#!/usr/bin/perl

{ package Foo;
use Moose;
use warnings;

has num => ( is => "rw", isa => "Int" );
has str => ( is => "rw", isa => "Str" );
}

use strict;
use feature "say";
use warnings;

my $foo = Foo->new;

for my $attr ("num", "str") {
say "$attr is of type: ",
$foo->meta->get_attribute($attr)->type_constraint->name;
}

See http://search.cpan.org/dist/Moose/lib/Moose/Meta/Class.pm

On Thu, Sep 29, 2016 at 3:34 PM Klaus Jantzen 
wrote:

> Hello,
>
> I have defined Moose-classes with various attributes.
>
> During the execution of a program that uses such a class I would like to
> determine the data type of a specific attribute at the time I am using it.
>
> How can I do that?
> --
>
> K.D.J.
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Remove Newlines from String

2016-09-09 Thread Chas. Owens
On Tue, Sep 6, 2016 at 3:24 PM Shawn H Corey  wrote:


> > #Change the value to the maximum you want
> > my %HEXCODES = map{$_ => sprintf("%03X", $_)} (0..128);
>
> my %HexCodes = map { ord($_) => sprintf '%02X', $_ } ( 0 .. 128 );
>

Just your friendly reminder that Unicode exists and unless you are opening
the file with the ":raw" IO layer, you can easily get unprintable
characters above 0x7F:

perl -e 'for (0 .. 0x10) { printf("U+%04x is not printable\n", $_)
unless chr =~ /[[:print:]]/  }'

Even limiting ourselves to values that fit in one byte, the first 32 values
above 127 (U+0080 - U+009F) are all control characters (the C1 set).


Re: solved: Net::FTP ALLO --- and how to advise functions in perl

2016-08-20 Thread Chas. Owens
I don't think the specific technique is discussed in perldoc (all of the
parts are there, you just have to put them together), but I do think it is
discussed in the Perl Cookbook and probably Programming Perl.

Stuff about the how Perl handles symbol tables is covered in

perldoc perlmod

http://perldoc.perl.org/perlmod.html#Symbol-Tables

You can see an example of mucking about with the current module's symbol
table in perlref:

perldoc perlref

http://perldoc.perl.org/perlref.html#Function-Templates

But it doesn't mention how to do it to other modules.

You might also search for information on typeglobs, as that is what we are
using to manipulate the symbol table. The basics are that
*Fully::Qualified::sub_name{CODE}
holds a code ref to the function sub_name in Fully::Qualified and assigning
a code ref to *Fully::Qualified::sub_name will replace that function with
the one pointed to by the code ref.

A common pattern is to save off the old code ref, assign an anonymous sub
to the typeglob, and put the code you want in that anonymous sub calling
the old code ref through a closure:

{

no warnings "redefine";

my $old = *Fully::Qualified::sub_name{CODE}

*Fully::Qualified::sub_name = sub {

#do stuff before

$old->(@_);

#do stuff after

};

}

On Sat, Aug 20, 2016, 05:44 hw  wrote:

> Chas. Owens schrieb:
> > If you want to get rid of ALLO completely, it looks like you just need
> to monkeypatch Net::FTP::_ALLO to return 1:
> >
> > use Net::FTP;
> > BEGIN {
> >  no warnings "redefine";
> >  *Net::FTP::_ALLO = sub { 1 };
> > }
> >
> > This replaces the _ALLO method of Net::FTP with a new method that just
> returns 1.  I set it up like that because the actual code is:
> >
> > sub _ALLO { shift->command("ALLO", @_)->response() == CMD_OK }
> >
> > So it looks like it expects the return value to be either true or
> false.  None of the calls seemed to check the value, but better to be safe
> than sorry.
>
> Perfect, thank you very much!  That´s exactly what I´d have done if I knew
> how to.
>
> BTW, it turned out that the rmdir method goes crazy when you try to remove
> a
> directory recursively that doesn´t exist, so I added a check and only
> remove
> one when it does exist.
>
> Is there a good documentation about how to do this kind of
> advising/redefining?
>
>


Re: if element exists in an array

2016-08-19 Thread Chas. Owens
On Fri, Aug 19, 2016 at 2:22 PM Chas. Owens  wrote:

> Truth.  If you are checking in lots of things exist a hashset might be a
> better way to go:
>
> my %hashset = map { ($_ => undef) } (3,1,4,2,9,0);
>
> my $found = exists $hashset{4} || 0;
> my $not_found = exists $hashset{10} || 0;
>
> By setting the value of the hash to be undef, you take up less space than
> setting it any other value
>

Here is the result of a benchmark.

cached_hashset: 1
hashset: 1
any: 1
grep: 1

100 items

 Rate  hashset  grep  any
cached_hashset
hashset   13953/s   --  -93% -95%
 -100%
grep 212031/s1420%-- -25%
-99%
any  283002/s1928%   33%   --
-99%
cached_hashset 33211602/s  237921%15564%   11635%
  --

1000 items

 Rate  hashset grep   any
cached_hashset
hashset1496/s   -- -93%  -95%
 -100%
grep  21900/s1364%   --  -26%
 -100%
any   29658/s1882%  35%--
 -100%
cached_hashset 27036512/s 1807036%  123354%91061%
  --

1 items

 Rate  hashset  grep  any
cached_hashset
hashset 108/s   --  -95% -96%
 -100%
grep   2197/s1941%-- -21%
 -100%
any2796/s2497%   27%   --
 -100%
cached_hashset 36489558/s33894645%  1660653% 1304955%
  --

>From this, you can see that any is the best choice if you are going to
search the list once, but a hashset is the best choice (by an insane
margin) if you are going to search the list many times.  Here is the code
that generated the benchmark:

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

use List::Util qw/any/;

my @a = (1);
my %cache = (1 => undef);

my %subs = (
grep => sub {
return scalar grep { $_ == 1 } @a;
},
any => sub {
return any { $_ == 1 } @a;
},
hashset => sub {
my %hashset = map { ($_ => undef) } @a;
return exists $hashset{1};
},
cached_hashset => sub {
return exists $cache{1};
}
);

for my $sub (keys %subs) {
print "$sub: ", $subs{$sub}(), "\n";
}

for my $n (100, 1_000, 10_000) {
@a = reverse 1 .. $n;

print "\n$n items\n\n";
Benchmark::cmpthese -2, \%subs;
}


Re: if element exists in an array

2016-08-19 Thread Chas. Owens
Truth.  If you are checking in lots of things exist a hashset might be a
better way to go:

my %hashset = map { ($_ => undef) } (3,1,4,2,9,0);

my $found = exists $hashset{4} || 0;
my $not_found = exists $hashset{10} || 0;

By setting the value of the hash to be undef, you take up less space than
setting it any other value.

On Fri, Aug 19, 2016 at 2:15 PM  wrote:

> But does it need to be an array. Rethink into hash and life could be a
> little bit easier...
>
> Wags ;)
> WagsWorld
> Hebrews 4:15
> Ph: 408-914-1341
>
> On Aug 18, 2016, 19:41 -0700, kp...@freenet.de, wrote:
>
> Thanks for all the replies.
> Yes I found List::Util is a useful toolset.
>
>
> On 2016/8/19 10:00, Chas. Owens wrote:
>
> The any function from List::Util will also do what you want.
>
> perldoc List::Util
>
> http://perldoc.perl.org/List/Util.html#any
>
> my $found = any { $_ == 4 } (3, 1, 4, 2, 9, 0); # true
> my $not_found = any { $_ == 10 } (3, 1, 4, 2, 9, 0); # false
>
> Which you want depends on the application. The grep function will
> return a number between 0 and the size of the list and reads the entire
> list. The any function returns the canonical true (a tri-value that
> holds "1", 1, and 1.0) or false (a tri-value that holds "", 0, 0.0)
> values and stops at the first matching value. The canonical false value
> often throws people for a loop as they expect it to be "0" in string
> context, but it is "". You may want to say
>
> my $found = (any { $_ == 10 } (3, 1, 4, 2, 9, 0)) || 0;
>
> to force it to be 0 instead of the canonical false value.
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Net::FTP ALLO --- and how to advise functions in perl

2016-08-19 Thread Chas. Owens
If you want to get rid of ALLO completely, it looks like you just need to
monkeypatch Net::FTP::_ALLO to return 1:

use Net::FTP;
BEGIN {
no warnings "redefine";
*Net::FTP::_ALLO = sub { 1 };
}

This replaces the _ALLO method of Net::FTP with a new method that just
returns 1.  I set it up like that because the actual code is:

sub _ALLO { shift->command("ALLO", @_)->response() == CMD_OK }

So it looks like it expects the return value to be either true or false.
None of the calls seemed to check the value, but better to be safe than
sorry.



On Fri, Aug 19, 2016 at 1:13 PM hw  wrote:

> Chas. Owens schrieb:
> > Based on a cursory reading of the perldoc, it looks like the ALLO
> command is only sent if you call the Net::FTP::alloc method.  If you aren't
> calling it, can you provide a toy test case for us where the code sends
> ALLO.  I will try to debug why it is sending a command you aren't asking
> for.
>
> I´ve been looking at the documentation on cpan and found no way to disable
> the
> using of ALLO, and no mention of the alloc method.
>
> I´m not calling the Net::FTP::alloc method[2], only put:
>
>
> sub update_client_form($cgi, $ftp, $dir, $form_local, $form_remote) {
>unless($ftp->put($form_local, $form_remote)) {
>  say $cgi->h1('WARNUNG: das Formular konnte nicht aktualisiert
> werden');
>  return 0;
>}
>unless($ftp->rmdir($dir . 'metadata', 1)) {
>  say $cgi->h1('WARNUNG: Metadaten konnten nicht gelöscht werden');
>  return 0;
>}
>
>unless($ftp->rmdir($dir . '.cache', 1)) {
>  say $cgi->h1('WARNUNG: der Zwischenspeicher konnte nicht gelöscht
> werden');
>  return 0;
>}
>
>return 1;
> }
>
>
> my $ftp = Net::FTP->new($client, Port=> $PORT, Timeout => 20, Debug => 1);
>
>
> The put fails:
>
>
> Net::FTP>>> Net::FTP(2.79)
> Net::FTP>>>   Exporter(5.71)
> Net::FTP>>>   Net::Cmd(2.30)
> Net::FTP>>>   IO::Socket::INET(1.35)
> Net::FTP>>> IO::Socket(1.37)
> Net::FTP>>>   IO::Handle(1.35)
> Net::FTP=GLOB(0x2e70a18)<<< 220 FTPServer ready (cwd is /)
> Net::FTP=GLOB(0x2e70a18)>>> USER ftp
> Net::FTP=GLOB(0x2e70a18)<<< 331 - Login as ftp OK. Send password
> Net::FTP=GLOB(0x2e70a18)>>> PASS 
> Net::FTP=GLOB(0x2e70a18)<<< 230 - Password accepted
> Net::FTP=GLOB(0x2e70a18)>>> TYPE I
> Net::FTP=GLOB(0x2e70a18)<<< 200 Switching to mode TYPE I
> Net::FTP=GLOB(0x2e70a18)>>> PORT 192,168,220,192,210,156
> Net::FTP=GLOB(0x2e70a18)<<< 200 PORT command successful
> Net::FTP=GLOB(0x2e70a18)>>> NLST /storage/sdcard0/odk/instances/
> Net::FTP=GLOB(0x2e70a18)<<< 150 Directory listing for
> storage/sdcard0/odk/instances/
> Net::FTP=GLOB(0x2e70a18)<<< 226 Directory send OK.
> Net::FTP=GLOB(0x2e70a18)>>> PORT 192,168,220,192,203,166
> Net::FTP=GLOB(0x2e70a18)<<< 200 PORT command successful
> Net::FTP=GLOB(0x2e70a18)>>> ALLO 17017
> Net::FTP=GLOB(0x2e70a18)<<< 500 ALLO not understood
>
>
> > If you are calling Net::FTP::alloc (eg $ftp->alloc( -s $file_to_send )),
> then stop calling it and you should not get anymore errors.  If the program
> connects to multiple FTP servers and some want ALLO and some don't then
> either wrap that code in an if statement, or if it is too much code and you
> don't want to touch it all, you can always monkey patch the method.  You
> could put something like this in your script and all calls to
> Net::FTP::alloc in that script will run your version instead of the
> original (warning untested code):
> >
> > use Net::FTP
> > BEGIN {
> >  no warnings "redefine";
> >  my %bad_hosts = (
> >  bad_host_that_does_not_understand_allo => 1,
> >  );
> >  my $old_alloc = *Net::FTP::alloc{CODE};
> >  *Net::FTP::alloc = sub {
> >  return if $bad_hosts{ $_[0]->host };
> >  $old_alloc->(@_);
> >  }
> > }
> >
> > That will cause it to do nothing when you call Net::FTP::alloc with on
> an Net::FTP object that is connected to a host in the %bad_hosts hash.
>
> Thank you very much, I could try that out.  Perhaps the ALLO command is
> send by default when you call the put method, and advising[1] the method
> to do nothing might help.
>
> The hosts are cell phones, and I tried two different FTP servers for
> Android, both of which don´t comply to the RFC in that they mistreat the
> ALLO command.
>
> My program

Re: Net::FTP ALLO

2016-08-19 Thread Chas. Owens
Based on a cursory reading of the perldoc, it looks like the ALLO command
is only sent if you call the Net::FTP::alloc method.  If you aren't calling
it, can you provide a toy test case for us where the code sends ALLO.  I
will try to debug why it is sending a command you aren't asking for.

If you are calling Net::FTP::alloc (eg $ftp->alloc( -s $file_to_send )),
then stop calling it and you should not get anymore errors.  If the program
connects to multiple FTP servers and some want ALLO and some don't then
either wrap that code in an if statement, or if it is too much code and you
don't want to touch it all, you can always monkey patch the method.  You
could put something like this in your script and all calls to
Net::FTP::alloc in that script will run your version instead of the
original (warning untested code):

use Net::FTP
BEGIN {
no warnings "redefine";
my %bad_hosts = (
bad_host_that_does_not_understand_allo => 1,
);
my $old_alloc = *Net::FTP::alloc{CODE};
*Net::FTP::alloc = sub {
return if $bad_hosts{ $_[0]->host };
$old_alloc->(@_);
}
}

That will cause it to do nothing when you call Net::FTP::alloc with on an
Net::FTP object that is connected to a host in the %bad_hosts hash.


On Fri, Aug 19, 2016 at 9:20 AM hw  wrote:

>
> Hi,
>
> is there some way to prevent Net::FTP from using the ALLO command or
> to make it ignore failures when this command is used?
>
> I have to deal with ftp servers that do not understand the ALLO command.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: if element exists in an array

2016-08-18 Thread Chas. Owens
On Thu, Aug 18, 2016 at 9:39 PM  wrote:

> Hello,
>
> What's the better way to decide if an element exists in an array?
> Something like what ruby does,
>
> irb(main):001:0>  x=[3,1,4,2,9,0]
> => [3, 1, 4, 2, 9, 0]
> irb(main):002:0> x.include? 4
> => true
> irb(main):003:0> x.include? 10
> => false
> irb(main):004:0> quit
>
>
> I tried searching but found nothing such a method in perl.


The grep builtin does what you want

perldoc -f grep

http://perldoc.perl.org/functions/grep.html

my $found = grep { $_ == 4 } (3, 1, 4, 2, 9, 0); # 1
my $not_found = grep { $_ == 10 } (3, 1, 4, 2, 9, 0); # 0

The return value for grep in scalar context is the number of times it
matches.  In list context, it returns all of the elements that match:

my @found = grep { $_ % 2 } (3, 1, 4, 2, 9, 0); # @found holds (3, 1, 9)

The any function from List::Util will also do what you want.

perldoc List::Util

http://perldoc.perl.org/List/Util.html#any

my $found = any { $_ == 4 } (3, 1, 4, 2, 9, 0); # true
my $not_found = any { $_ == 10 } (3, 1, 4, 2, 9, 0); # false

Which you want depends on the application.  The grep function will return a
number between 0 and the size of the list and reads the entire list.  The
any function returns the canonical true (a tri-value that holds "1", 1, and
1.0) or false (a tri-value that holds "", 0, 0.0) values and stops at the
first matching value.  The canonical false value often throws people for a
loop as they expect it to be "0" in string context, but it is "".  You may
want to say

my $found = (any { $_ == 10 } (3, 1, 4, 2, 9, 0)) || 0;

to force it to be 0 instead of the canonical false value.


Re: XML::Simple Umlaute

2016-08-09 Thread Chas. Owens
Take a look at the -C argument for perl and the PERL_UNICODE environment
variable in http://perldoc.perl.org/perlrun.html

Examine the difference between

perl -E 'say "\x{df}"'

and

PERL_UNICODE=O perl -E 'say "\x{df}"'

That said, if you are working with the web, why in the world are you
sending UTF-8?  HTML has entities for a reason.  I would suggest using
HTML::Entities instead of trying to send non-ASCII characters through who
knows how many layers of things that can screw up UTF-8:

perl -MHTML::Entities -E 'say encode_entities "\x{df}"'



On Tue, Aug 9, 2016 at 7:34 AM hw  wrote:

> Chas. Owens schrieb:
> >
> > On Thu, Jul 28, 2016 at 10:55 AM Paul Johnson  p...@pjcj.net>> wrote:
> >
> > On Thu, Jul 28, 2016 at 10:23:19AM -0400, Chas. Owens wrote:
> >
> > snip
> >
> >  > Also, this answer on StackOverflow by tchrist (Tom Christiansen,
> who I
> >  > would say knows the most about the intersection of Perl and
> Unicode)
> >  > is a good resource: http://stackoverflow.com/a/6163129/78259
> >
> > Quite.  And utf8::all tries to encapsulate as much of that
> boilerplate
> > as it can.
> >
> >
> > I have always read that answer as a bit of an indictment of the idea of
> "you should be able to load this module and everything will be fine".
> Unicode is complex and trying to treat it like just another list of
> characters is doomed to teeth gnashing and crying.  Of course, even
> treating it the way it should be leads to teeth gnashing and crying, but at
> least that will be over the fact the humans suck (we can't even agree on
> where þ should be sorted) as opposed to Perl sucking.
>
> When I have something like
>
>
> print $cgi->p('Gebäudefläche:');
>
>
> in my source, which is correctly displayed everywhere else, I also
> need it correctly displayed in the web browser --- even particularly
> there because that is what the users are looking at.
>
> And that´s all there is to it.  It´s really that simple.
>
>


Re: XML::Simple Umlaute

2016-07-28 Thread Chas. Owens
On Thu, Jul 28, 2016 at 10:55 AM Paul Johnson  wrote:

> On Thu, Jul 28, 2016 at 10:23:19AM -0400, Chas. Owens wrote:

 snip

> > Also, this answer on StackOverflow by tchrist (Tom Christiansen, who I
> > would say knows the most about the intersection of Perl and Unicode)
> > is a good resource: http://stackoverflow.com/a/6163129/78259
>
> Quite.  And utf8::all tries to encapsulate as much of that boilerplate
> as it can.
>

I have always read that answer as a bit of an indictment of the idea of
"you should be able to load this module and everything will be fine".
Unicode is complex and trying to treat it like just another list of
characters is doomed to teeth gnashing and crying.  Of course, even
treating it the way it should be leads to teeth gnashing and crying, but at
least that will be over the fact the humans suck (we can't even agree on
where þ should be sorted) as opposed to Perl sucking.


Re: XML::Simple Umlaute

2016-07-28 Thread Chas. Owens
On Thu, Jul 28, 2016 at 10:05 AM, hw  wrote:
snip
> So which character encoding on STDOUT does perl use by default?  That should
> be utf-8 without any further ado, shouldn´t it?  When I add
>
>
> binmode STDOUT, ":encoding(utf-8)";
>
>
> the characters are displayed correctly in the terminal.  Why would perl use
> something else than utf-8 by default?

Take the following with a grain of salt.  My knowledge is mostly
hearsay and supposition with a dash of cargo cultism on this matter.

Perl predates even Unicode (they both came out in '87).  Unicode did
not get much traction until the mid-nineties when people started
realizing that UTF-8 (created in '92) was a good thing.   So, for most
of its early history, Perl used Latin1.  It still does to a large
extent for backwards compatibility reasons.  To make Perl 5 a proper
UTF-8 environment there are a number of knobs to pull and buttons to
poke.

You may find this video from YAPC NA 2016 enlightening:
https://www.youtube.com/watch?v=TmTeXcEixEg

Others that may be helpful (I haven't watched them, but I trust the speaker):

https://www.youtube.com/watch?v=iZgqhVu72zc
https://www.youtube.com/watch?v=X2FQHUHjo8M

Also, this answer on StackOverflow by tchrist (Tom Christiansen, who I
would say knows the most about the intersection of Perl and Unicode)
is a good resource: http://stackoverflow.com/a/6163129/78259

Hope this helps.


-- 
Chas. Owens
http://github.com/cowens
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: XML::Simple Umlaute

2016-07-28 Thread Chas. Owens
Data::Dumper is dumping the internal format.  To ensure compatibility, it
is using the \x{df} escape to represent LATIN SMALL LETTER SHARP S. To see
it rendered as a character, just print it:

#!/usr/bin/perl

use strict;
use feature 'say';

use XML::Simple;

#warnings should come last to handle any registered warnings in previous
modules
use warnings;

binmode STDOUT, ":encoding(UTF-8)";

my $xml = XML::Simple->new;
my $data = $xml->XMLin("test.xml");

say $data->{Regaletikett_ausgeben};


On Thu, Jul 28, 2016 at 9:05 AM hw  wrote:

>
> Hi,
>
> I would like to read XML files which look like this:
>
>
> 
> 
>http-equiv="content-type" content="text/html; charset=UTF-8">
>  uuid:ee1bd852-37ee-4965-a097-50130cf6dac7
>
>Infostand
>5449000134264
>
>groß
>
>  
>  5449000134264
>  
>5449000134264
>10.0
>20
>
>  
>
> 
>
>
> There is an Umlaut, ß, supposed to be at
>
>
> groß
>
>
>
> which is apparently impossible to read.  The following program ...
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use feature 'say';
>
> use XML::Simple;
> use Data::Dumper;
>
>
> my $xml = new XML::Simple;
> my $data = $xml->XMLin("test.xml");
>
> open my $fh, ">", 'pout';
> print $fh Dumper($data);
> close $fh;
>
> print Dumper($data);
>
>
> exit 0;
>
>
> ... gives me this output:
>
>
> $VAR1 = {
>'Bezeichnung1' => {},
>'id' => 'build_Inventur_1469705446',
>'Stationsnummer' => 'Infostand',
>'meta' => {
>  'content' => 'text/html; charset=UTF-8',
>  'http-equiv' => 'content-type',
>  'instanceID' =>
> 'uuid:ee1bd852-37ee-4965-a097-50130cf6dac7'
>},
>'Mitarbeiter_inv' => '5449000134264',
>'Regaletikett_ausgeben' => "gro\x{df}",
>'Erfassung' => {
>   'Artikelstapel' => {
>  'Menge' => '20',
>  'Preis' => '10.0',
>  'EAN_Artikel' =>
> '5449000134264',
>  'Etikettentyp' => {}
>},
>   'Artikel_erfassen' => {},
>   'Lagerstaette' => '5449000134264'
> }
>  };
>
>
> I´m not getting any better results when adding an encoding tag to the
> XML file and when writing the Dumper output to a file.
>
> Is it impossible to use Umlaute in XML Files?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: DBI.c: loadable library and perl binaries are mismatched

2016-06-30 Thread Chas. Owens
That typically occurs when the XS portion of a Perl module was compiled
against a different version of perl.  Since you say that you re-installed
everything, then it is possible you are loading a module from a different
location than you think you are.  Try running this command:

perl -le 'for (@INC) { print $_ if -f "$_/DBI.pm" }'

It should list all of the places perl finds a DBI module installed.  If you
have more than one, then that is almost certainly your problem.

On Tue, May 17, 2016 at 3:16 PM Ron Wingfield 
wrote:

> I am getting no response from this post on the FreeBSD Forums:
> https://forums.freebsd.org/threads/56291/
>
> If I try to execute a simple perl script that utilizes the DBI, the
> following error diagnostic:
>
> |# perl -T create_roster_db.pl
> DBI.c: loadable library and perl binaries are mismatched (got handshake
> key 0x7ac0080, needed 0x7b80080)|
>
> . . .with a fresh install form the FreeBSD Ports, perl5.22.2 and
> p5-DBI-1.636, why this mismatch?
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Excessive wait time after fork

2011-04-04 Thread Chas. Owens
On Sat, Apr 2, 2011 at 12:06, TAO, NENGBING [AG/1005]
 wrote:
> Hi,
>
>        I noticed that after fork, the wait time is excessive (my other
> test waited for hours). Is this a known problem, how to get around it?
snip
> child 3 AFTER  sub call :Sat Apr  2 10:45:01 2011 numKeys:401
> child 4 BEFORE sub call : Sat Apr  2 10:44:54 2011
> child 4 AFTER  sub call :Sat Apr  2 10:45:04 2011 numKeys:501
> child 5 BEFORE sub call : Sat Apr  2 10:44:54 2011
> child 5 AFTER  sub call :Sat Apr  2 10:45:06 2011 numKeys:601
> child 1 BEFORE sub call : Sat Apr  2 10:44:54 2011
> child 1 AFTER  sub call :Sat Apr  2 10:45:41 2011 numKeys:2001
> child 2 BEFORE sub call : Sat Apr  2 10:44:54 2011
> child 2 AFTER  sub call :Sat Apr  2 10:45:56 2011 numKeys:3001
> child 0 BEFORE sub call : Sat Apr  2 10:44:54 2011
> child 0 AFTER  sub call :Sat Apr  2 10:48:45 2011 numKeys:10001   #
snip

My bet is you don't have enough RAM to hold all of that data; your OS
is being forced to use swap.  Your poor machine is so swamped it is
taking that long to get back to normal.

Each key in your hash is probably taking up around fifteen bytes and
each value is probably around four bytes (if not eight).  You have
165,000,006 entries.  That means you need around 3 gigabytes of RAM
for the data alone.  That doesn't count the hash that holds the data,
perl itself, or any other programs you may be running.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: transposing %d values to %x output

2011-03-30 Thread Chas. Owens
On Wed, Mar 30, 2011 at 00:28, Brian Fraser  wrote:
> On Wed, Mar 30, 2011 at 12:46 AM, Chas. Owens  wrote:
>>
>> If you are dealing exclusively with ASCII, then you should be using
>>  the [bytes][0] pragma;
>
> It's nitpicky, but I'd advice against ever recommending use bytes in the
> beginners list. Or any list really. See:
> http://www.nntp.perl.org/group/perl.perl5.porters/2011/03/msg170010.html
> Brian.

Hmm, I had missed that change.  Thanks.  An easier to read version
comes from the Perl 5.13.11 version of the bytes pod:

This pragma reflects early attempts to incorporate Unicode into perl and
has since been superseded. It breaks encapsulation (i.e. it exposes the
innards of how the perl executable currently happens to store a string),
and use of this module for anything other than debugging purposes is
strongly discouraged. If you feel that the functions here within might be
useful for your application, this possibly indicates a mismatch between
your mental model of Perl Unicode and the current reality. In that case,
you may wish to read some of the perl Unicode documentation:
L, L, L and L.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: transposing %d values to %x output

2011-03-29 Thread Chas. Owens
On Tue, Mar 29, 2011 at 22:03, Jim Gibson  wrote:
snip
> I have heard this advice before, and it just sounds silly to me.
> I deal exclusively with ASCII characters, so \d will only match
> the characters '0' through '9'.
snip

If you are dealing exclusively with ASCII, then you should be using
the [bytes][0] pragma; however, just because you are dealing with only
ASCII today doesn't mean you will always be dealing only with ASCII.
At some point in the near future you will probably have to start
dealing with Unicode.  When that happens (and it really is a matter of
when, not if), do you want to go through all of your code and fix the
problems you could have easily avoided?

snip
> Can you provide any example of a situation where something bad
> will happen from using \d?
snip

Simple.  In the case we are talking about things will got terribly
astray if you happened to match something that wasn't [0-9]:

#!/usr/bin/perl

use strict;
use warnings;

binmode STDOUT, ":utf8";

my $s = "\x{ff15}.\x{ff15}.\x{ff15}.\x{ff15}";

print "$s\n";
$s =~ s/(\d+)[.](\d+)[.](\d+)[.](\d+)/sprintf "%x.%x.%x.%x", $1, $2, $3, $4/e;
print "$s\n";

5.5.5.5
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
0.0.0.0

The problem is that things that match \d just don't behave like
numbers.  Only ten of the hundreds of digits characters are actually
numbers.  They may look like numbers, as the Fullwidth Digit Five
characters I used here do, but Perl 5 still treats them like
non-number characters.

snip
> The statement "To match the ASCII digit characters you must use [0-9]" is
> wrong. I believe you meant to say "To match the ASCII digit characters and
> only those characters, you must use [0-9]".
snip

By your logic, it is perfectly fine to use . to match numbers.  After
all, . will match [0-9].  If you have bothered to specify \d, you most
likely mean [0-9].

[0]: http://perldoc.perl.org/bytes.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: transposing %d values to %x output

2011-03-29 Thread Chas. Owens
On Tue, Mar 29, 2011 at 19:50, Noah Garrett Wallach
 wrote:
snip
> s/(\d+)\.(\d+)\.(\d+)\.(\d+)/$1:$2:$3:$4::0/
>
> so is there a slick, easily readable way  to get the value $1, $2, $3, $4 to
> be rewriten as  %x instead of a %d?
snip

The [eval regex modifier (e)][0] will let you use an arbitrary
expression for the replacement.  You can then use [sprintf][1] to
print the captures in hexadecimal.  It is important to note that \d
doesn't match what you think it does.  Starting with Perl 5.8, \d
matches and digit character.  This includes characters such as
"\x{1815}" (Mongolian digit five).  To match the ASCII digit
characters you must use [0-9]:

s/([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)/sprintf
"%02x:%02x:%02x:%02x::0", $1, $2, $3, $4/e;

It is also important to note that, if this is supposed to match an IP
Address, it will match invalid strings like "256.256.256.256".  If you
only want to match numbers between 0 and 255, it can be done using a
regex like this:

my $octet = "(?:[1-9]?0-9|1[0-9]{2}|2[1-4][0-9]|25[0-5])";
s/\b($octet)[.]($octet)[.]($octet)[.]($octet)\b/sprintf
"%02x:%02x:%02x:%02x::0", $1, $2, $3, $4/e;

[0]: http://perldoc.perl.org/perlop.html#s%2fPATTERN%2fREPLACEMENT%2fmsixpogce
[1]: http://perldoc.perl.org/functions/sprintf.html
-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: problem with naming of variables

2011-03-29 Thread Chas. Owens
On Mon, Mar 28, 2011 at 11:10, Katya Gorodinsky
 wrote:
> Maybe this way:
>
> @bet1 = (0,0,0,0);
> @bet2 = (0,0,0,1);
> @bet3 = (0,0,1,0);
>
>  $random_bet_position = int(rand(3) + 1);
>
>  $name = 'bet' . $random_bet_position;
>  @selected_bet = @{$name};

Symbolic references are incredibly dangerous and mostly unnecessary in
Modern Perl.  This is why the [strict pragma][0] bans their use.  The
proper solution is to use the correct data structure (in this case an
array of arrays).

[0]: http://perldoc.perl.org/strict.html


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: problem with naming of variables

2011-03-28 Thread Chas. Owens
2011/3/28 jj mahoney :
>
> Hi, How do I name this variable correctly, @{bet$random_bet_position);
> With regards to the script below, @selected_bet is suppose to select either 
> @bet1, @bet2 or @bet3 depending on the chosen number held by 
> $random_bet_position, so how do I do this? Thanks
>
> @bet1 = (0,0,0,0);
> @bet2 = (0,0,0,1);
> @bet3 = (0,0,1,0);
>
> $random_bet_position = int(rand(3) + 1);
>
> @selected_bet = @{bet$random_bet_position);


What you want is an Array of Arrays.  Other replies have given you
syntax that covers it, but you would be well served by reading the
documentation that covers them: perldoc perldsc or the web version
http://perldoc.perl.org/perldsc.html.  You will also want to become
familiar with references and how they work: perldoc perlref and
http://perldoc.perl.org/perlref.html.  If you find that document too
formidable, you may want to start with: perldoc perlreftut or
http://perldoc.perl.org/perlreftut.html.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl dd

2011-03-28 Thread Chas. Owens
On Mon, Mar 28, 2011 at 05:22, a b  wrote:
> Thanks Rob
>
> I too was thinking that way. Just in case if any one might have face this
> issue and hence this email
>
> Well if no module i will live with it for time being
snip

The Unix dd command is a swiss army knife of copying data, could you
narrow it down to what you want to use dd for?  A simple
implementation would be

#!/usr/bin/perl

use strict;
use warnings;

die "usage: $0 infile outfile\n" unless @ARGV == 2;

open my $in, "<:raw", $ARGV[0]
or die "could not open $ARGV[0]: $!\n";
open my $out, ">:raw", $ARGV[1]
or die "could not open $ARGV[1]: $!\n";

local $/ = \4096; #read 4k at a time

print $out $_ while <$in>;



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: assigning hash to a scalar

2011-03-27 Thread Chas. Owens
On Sat, Mar 26, 2011 at 12:05, Brian Fraser  wrote:
> Chas++
>
> I don't get to see Algorithms in college, so I doubly appreciate
> explanations like this, in the one language I sort of grok* :) You've made
> my morning, thank you!
snip

Be warned, my implementation is incredibly naive and inefficient.
This was the just the bare minimum needed to understand the meaning of
a hash's scalar value.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: assigning hash to a scalar

2011-03-26 Thread Chas. Owens
 is a bad situation to be
in.  It cause the hash to be slow that bucket.  This is one of the
uses of the ratio of used to total buckets that hashes return in
scalar context.  If the hash says that only a few buckets are being
used, but they are lots of keys in the hash, then you know you have a
problem.

To learn more about hashes, ask questions here about what I have said,
or [read about them][4].

[0]: http://en.wikipedia.org/wiki/Md5
[1]: http://en.wikipedia.org/wiki/Sha1
[2]: http://en.wikipedia.org/wiki/Jenkins_hash_function#one-at-a-time
[3]: http://en.wikipedia.org/wiki/Modulo_operation
[4]: http://en.wikipedia.org/wiki/Hash_table

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: ternary operator

2011-03-25 Thread Chas. Owens
On Thu, Mar 24, 2011 at 16:53, Chris Stinemetz
 wrote:
> I have a ternary operator that I would like to be rounded to the nearest 
> tenth decimal
> place before the array is pushed.

The proper term is conditional operator, even in C.

Rounding is a tricky subject (see
http://en.wikipedia.org/wiki/Rounding for the gory details), that is
why Perl 5 does not provide a simple rounding function for you.  Do
you have a rounding method in mind, or you just happy with whatever
you get?

snip
>        $dist =
>                ( length( $dist ) > 1 ) ? $dist/6.6/8/2*10/10 : 0 ; # I 
> would like to round the number to the nearest tenth right here. 
snip

As for how to combine a function call with a conditional, it is simple:

sub round {
my ($n, $places) = @_;
return sprintf "%.${places}f", $n; #I don't care how the number is rounded
}

$dist = ( length( $dist ) > 1 ) ? round($dist/6.6/8/2*10/10, 1) : 0;



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Garbled lines

2011-03-24 Thread Chas. Owens
On Thu, Mar 24, 2011 at 03:14, HACKER Nora  wrote:
snip
> I am not sure whether this is really a Perl problem but maybe anybody
> can point me into the right direction: I am generating insert statements
> and writing them into a file for later execution. Most of the lines are
> written correctly:
smnip
> but from time to time some of the following garbled lines appear in the
> same file among the other correct lines:
snip
> Obviously, two insert statements get mixed and characters from both
> statements are written alternately. But how does this happen? Is this a
> Perl problem or could this be a buffer problem on the AIX server?
snip

Without seeing the code, or at least being told how you are writing to
the file (for instance, are you writing to a named pipe that is being
read by another program), it is nearly impossible to give you any
hints about what is going on.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach loop

2011-03-23 Thread Chas. Owens
On Mon, Mar 21, 2011 at 01:46, Mike McClain  wrote:
snip
>> > my @report = map
>> > "$_->{cell}\t$_->{sect}\t$_->{carr}\t$_->{chan}\t$_->{dist}\n" , @sorted;
>> > print @report ;
>>
>> This map will consume a lot of memory, better do it using a foreach loop.
>
> In what way will the use of map here use any more memory than a foreach loop?
snip

The problem is that map returns a list.  That list will exist in
memory as you copy it to @report.  Just before the assignment is
finished, you will be using twice the amount of memory you expect.
Perl doesn't tend to return memory to the system, so, even though no
variable is using it, the memory used to hold the list will still be
held by perl.  Happily, perl will reuse the memory, so, as long as it
isn't huge, it normally isn't a big deal.



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Need Help. .Issue with format statement

2011-03-23 Thread Chas. Owens
On Wed, Mar 23, 2011 at 08:47, Sudhir  wrote:
> My work environment recently shifted from perl v5.6 to perl v5.10
>
> I found one issue with perl format statement in latest version v5.10
>
> sample code:
>
> #!/bin/env perl
> use strict;
>
> &genRep();
>
> sub genRep
> {
> format DURATION_TOP =
> @<<<<<<<<<<<<<<<<<<<<<<<<<<<
> "This is TOP"
> ---
> .
> format DURATION =
> @<<<<<<<<<<<<<<<
> "Main Body"
> .
> $~ = 'DURATION';
> write;
> }
>
> Output in perl v5.6:
> This is TOP
> ---
> Main Body
>
> output in perl v5.10
> Main Body
>
>
> What should I do in v5.10 to get the old output?
snip

I don't have 5.6 laying around, but it looks like 5.12 (and I assume
5.10) will do the right thing if the file is open:

#!/usr/bin/perl

use strict;

#create an in memory file to test if the problem is
#the lack of a filehandle
open DURATION, ">", \my $output or die $!;

&genRep();

print $output;


sub genRep
{
format DURATION_TOP =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<
"This is TOP"
---
.
format DURATION =
@<<<<<<<<<<<<<<<
"Main Body"
.
write DURATION;
}

You could also fix it with fewer steps by dup'ing STDOUT to DURATION
if you weren't already opening DURATION:

#!/usr/bin/perl

use strict;

open DURATION, ">&", \*STDOUT or die $!;

&genRep();

sub genRep
{
format DURATION_TOP =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<
"This is TOP"
---
.
format DURATION =
@<<<<<<<<<<<<<<<
"Main Body"
.
write DURATION;
}


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Better Regrex

2011-03-23 Thread Chas. Owens
On Wed, Mar 23, 2011 at 09:05, Mike Blezien  wrote:
> Hello,
>
> I'm working on a simple regrex issue which I got to work but I think there's 
> a better way to do this. This is what I have right now. I need to simply 
> remove the string section in red.
>
> my($marker);
> my $message = "Why are we here?  To bless, inspire and uplift one another. 
> #TRB #inspiration #loa";
>   if($message =~ /\#(.*)/i) { $marker = $1; }
>   $message =~ s!$mark!!gi;
>   $message =~ s!\#!!gi;
>
> #Resulting String wanted:
>  Why are we here?  To bless, inspire and uplift one another.
>
> The method I'm using above works and we get the results wanted but I was 
> looking at it again and I think there's a better way to do this. Any 
> suggested would be appreciated.
snip

Removal can be done with one substitution:

$message =~ s/\s*#\S+\s*//g;

That will remove any whitespace characters followed by a # followed by
one or more non-whitespace characters and the whitespace that follows
it.

If you want to save the markers, you will need to match first, then remove them:

#!/usr/bin/perl

use strict;
use warnings;

my $message = "Why are we here?  To bless, inspire and uplift one
another. #TRB #inspiration #loa";

my @markers = $message =~ /#(\S+)/g;
$message =~ s/\s*#\S+\s*//g;

print "[$message]\nmarkers: ", join(", ", @markers), "\n";





-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Error Netcdf

2011-03-18 Thread Chas. Owens
On Fri, Mar 18, 2011 at 10:42,   wrote:
> Please help me, why this message appear.
> *
> /work/radar/radar_mkCAPPI/volume/Husein_20110221/201102211442
> cp: omitting directory 
> `/work/radar/radar_mkCAPPI/volume/Husein_20110221/201102211442'
> ncopen: filename "test.nc": NetCDF: Unknown file format
snip

There is no where near enough information to diagnose your problem,
but I would say that the file test.nc does not contain what you think
it does.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: input file

2011-03-17 Thread Chas. Owens
On Thu, Mar 17, 2011 at 11:56, Chris Stinemetz
 wrote:
> I'm trying to use file path for my file that I want to read but I am getting 
> the following error when trying to use strict.
>
> Can't use string ("C://temp//PCMD") as a symbol ref while "strict refs" in 
> use at ./DOband.pl line 10.
>
> Any help is greatly appreciated.
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $filepath = "C://temp//PCMD";
> my $outfile  = "output.txt";
>
>
> open ("$filepath") || die "ERROR: opening $filepath\n";
> open (OUTFILE, "> $outfile") || die "ERROR: opening $outfile\n";
>
>
>
> Chris Stinemetz
>
>

The proper syntax is one of

#very old school and bad
our $FILEPATH = $filepath;
open FILEPATH;

#dangerous because we don't specify the mode,
#which means if $filepath starts with > we could accidentally
#overwrite another file
open INFILE, $filepath or die "Could not open $filepath: $!";

#better, but still using the old two argument version of open
open INFILE, "< $filepath" or die "Could not open $filepath: $!";

#pretty good, but INFILE is visible to the entire package
#and some other code may already be using it, this is
#not much a problem in small programs, but in larger ones
#it leads to obscure and hard to diagnose bugs
open INFILE, "<", $filepath or die "Could not open $filepath: $!";

#this is the best way, it is safe and $infile is scope to the enclosing block
open my $infile, "<", $filepath or die "Could not open $filepath: $!";

You can read more in

http://perldoc.perl.org/functions/open.html

or

perldoc -f open



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sort results in ascending order

2011-03-17 Thread Chas. Owens
On Thu, Mar 17, 2011 at 11:04, Chris Stinemetz
 wrote:
snip
> For some reason I am not getting the sorted list in my output file. Instead I 
> am getting the following:
>
> bc8) HASH(0x100d0d78) HASH(0x100d15e8) HASH(0x100d0f28) HASH(0x100d0c58) 
> HASH(0x100d1168) HASH(0x100d1678)
snip


You are getting these results because the array holds hash references.

snip
>                push @array, {
>                        cell => $cell,
>                        sect => $sect,
>                        carr => $carr,
>                        RTD  => $RTD,
>                };
snip

This line of code pushes a hash reference onto the array

snip
>                print OUTFILE "@sorted:\n";
snip

And this line prints out the whole array.  To get at the individual
elements of the hash, you must ask for them individually:

for my $hashref (@sorted) {
print "$hashref->{cell} $hashref->{sect} $hashref->{carr}
$hashref->{RTD}\n";
}

Or all together with a hash slice:

for my $hashref (@sorted) {
print "@{$hashref}{qw/cell sect carr RTD/}\n";
}

You may find the documentation on references helpful:

http://perldoc.perl.org/perlreftut.html
http://perldoc.perl.org/perlref.html
http://perldoc.perl.org/perldsc.html

or

perldoc perlreftut
perldoc perlref
perldoc perldsc


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Out of Memory!

2010-10-13 Thread Chas. Owens
On Wed, Oct 13, 2010 at 04:41, Panda-X  wrote:
snip
> From my observation, the Out of Memory error could happen anytime, where
> the "MEM" size was never over 10K.
>
> Any clues ?
snip

Then the problem is unlikely to be that message.  The two most common
things that will cause you to run out of memory are slurping a massive
file into memory and cyclic references.  You can avoid the first by
always processing files line by line.  The second is more complicated.
 Perl 5 uses reference counting for garbage collection.  When a
scalar, hash, or array has no references it is eligible for garbage
collection.  Here is an example:

   {
   my $foo = 42; #creating and naming the variable causes the
refcnt to go to 1
   }
   #the scope ends destroying the name $foo, this causes the refcnt
   #to go down 1 to 0, and the contents are garbage collected

   my $ref;

   {
   my $foo = 42; #refcnt = 1
   $ref = \$foo; #taking a reference bumps the refcnt by 1 to 2
   } #the name $foo goes out of scope so refcnt goes down to 1

   $ref = undef;
   #the reference to the scalar goes away taking the
   #the refcnt down to 0 and garbage collection occurs

Reference counting works really well, but there is possible to shoot
yourself in the foot:

{
my ($x, $y); # x's and y's refcnt is now 1
$x = \$y; #y's refcnt is 2
$y = \$x; #x's refcnt is 2
}
#the names $x and $y go out of scope bring the refcnt for each to 1
#but nothing can access those scalars anymore, so the memory for
#each will stay allocated until the end of the program

This is one form of cyclic reference.  Finding cycles can be hard.
The first step is to look at all of your data structures with
[Devel::Cycle][0].  If you find a cycle you can weaken a reference in
the cycle with [weaken][1] from Scalar::Util.  Weakened references
don't increase the reference count, for example:

use Scalar::Util qw/weaken/;

{
my ($x, $y); # $x and $y have a refcnt of 1
$x = \$y; # $y has a refcnt of 2
$y = \$x; # $x has a refcnt of 2
weaken $y; #$x has a refcnt of 1, but the reference still works
}
#the names $x and $y go out of scope taking the refcnts for the scalars
#associated with $x and $y to refcnts of 0 and 1 respectively.  This makes
#the scalar associated with $x eligible for garbage collection.  The
#garbage collection destroys the reference to the scalar associated with
#$y, so its refcnt goes down to 0, which makes it eligible for garbage
#collection as well.



 [0]: http://search.cpan.org/dist/Devel-Cycle/lib/Devel/Cycle.pm
 [1]: http://perldoc.perl.org/Scalar/Util.html#weaken-REF




-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: No Output in Terminal

2010-10-01 Thread Chas. Owens
On Fri, Oct 1, 2010 at 18:56, Brandon McCaig  wrote:
> On Fri, Oct 1, 2010 at 1:07 AM, Chas. Owens  wrote:
>
>> Okay, here is what I think happened: you were print a carriage return.
>
> I thought that Mac OS X used UNIX newlines though (though I'm not a
> Mac user). :-/ So shouldn't "\n" be interpreted as an LF (0x0A) on
> Macs, the same as it would in other unices?
>
> --
> Brandon McCaig 
> V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
> Castopulence Software <http://www.castopulence.org/> 
> 
>

OS X does use UNIX newlines and the \n is 0x0A on that platform.  I
don't know how he got a carriage return in there, but it is the only
unexpected control character in his code and it fits the behavior.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




  1   2   3   4   5   6   7   8   9   10   >