Re: First line of input file not accessible.

2024-07-17 Thread David Precious
On Wed, 17 Jul 2024 17:41:22 +1000
Peter West via beginners  wrote:
[...]
> $ cat print_file
> #!/usr/bin/perl -n
> while (<>) {
> print "$. $_";
> }
> exit;
[...]
> What happened to line 1?

It's eaten by the `while (<>) { ... }` loop that is wrapped around your
program because you used `-n` in the shebang line - see `perldoc
perlrun` for details on the `-n` arg.

What's essentially happening is:

# this outer loop is the effect of the -n arg to perl
while (<>) {
# and this is your actual program code
while (<>) {
print "$. $_";
}
exit;
}

If you remove the `-n` from the shebang line, it'll work as you'd
expect it to.  (For portability you could also consider changing the
shebang line to use /usr/bin/env to use the first Perl found in $PATH,
e.g.:

#!/usr/bin/env perl

... but that's up to you :)

Cheers

Dave P   (BIGPRESH)



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




Re: Please help: perl run out of memory

2022-04-27 Thread David Emanuel da Costa Santiago




Às 11:33 de 17/04/22, wilson escreveu:

hello the experts,

can you help check my script for how to optimize it?
currently it was going as "run out of memory".

$ perl count.pl
Out of memory!
Killed


My script:
use strict;

my %hash;
my %stat;



To be honest you don't need the %stat, however you'll need to go through 
the %hash several times for the average calculation. It will also make 
the script more unreadable.


You can also read the file several times, and on each time extract only 
the values of only one item.


The best option is for you to use a profiler to figure it out where the 
most memory is being consumed...



Good luck.

Regards,
David Santiago

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




Re: Please help: perl run out of memory

2022-04-22 Thread David Precious
On Thu, 21 Apr 2022 07:12:07 -0700
al...@coakmail.com wrote:

> OP maybe need the streaming IO for reading files.

Which is what they were already doing - they used:

while () {
...
}

Which, under the hood, uses readline, to read a line at a time.

(where "HD" is their global filehandle - a lexical filehandle would
have been better, but makes no difference here)


You can use B::Deparse to see that the above deparses to a use of
readline:


  [davidp@columbia:~]$ cat tmp/readline
  #!/usr/bin/env perl
  
  while () {
  print "Line: $_\n";
  }
  
  [davidp@columbia:~]$ perl -MO=Deparse tmp/readline
  while (defined($_ = readline STDIN)) {
  print "Line: $_\n";
  }
  tmp/readline syntax OK


So, they're already reading line-wise, it seems they're just
running in to memory usage issues from holding a hash of 80+million
values, which is not super suprising on a reasonably low-memory box.

Personally, if it were me, I'd go one of two ways:

* just throw some more RAM at it - these days, RAM is far cheaper than
  programmer time trying to squeeze it into the least amount of bytes,
  especially true if it's a quick "Get It Done" solution

* hand it off to a tool made for the job - import the data into SQLite
  or some other DB engine and let it do what it's designed for, as it's
  likely to be far more efficient than a hand-rolled Perl solution.
  (They already proved that Apache Spark can handle it on the same
  hardware)


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




Re: Please help: perl run out of memory

2022-04-21 Thread David Precious
On Thu, 21 Apr 2022 17:26:15 +0530
"M.N Thanishka sree Manikandan"  wrote:

> Hi wilson
> Try  this module file::slurp

Given that the OP is running into memory issues processing an 80+
million line file, I don't think suggesting a CPAN module designed to
read the entire contents of a file into memory is going to be very
helpful :)


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




Re: Please help: perl run out of memory

2022-04-17 Thread David Mertens
I see nothing glaringly inefficient in the Perl. This would be fine on your
system if you were dealing with 1 million items, but you could easily be
pushing up against your system's limits with the generic data structures
that Perl uses, especially since Perl is probably using 64-bit floats and
ints, and storing the hash keys twice (because you have to hashes).

You could try to use the Perl Data Language, PDL, to create large typed
arrays with minimal overhead. However, I think a more Perlish approach
would be to use a single hash to store the data, as you do (or maybe using
pack/unpack to store the data using 32-bit floats and integers). Then
instead of using sort, run through the whole collection and build your own
top-20 list (or 50 or whatever) by hand. This way the final process of
picking out the top 20 doesn't allocate new storage for all 80 million
items.

Does that make sense? I could bang out some code illustrating what I mean
if that would help.

David

On Sun, Apr 17, 2022, 5:33 AM wilson  wrote:

> hello the experts,
>
> can you help check my script for how to optimize it?
> currently it was going as "run out of memory".
>
> $ perl count.pl
> Out of memory!
> Killed
>
>
> My script:
> use strict;
>
> my %hash;
> my %stat;
>
> # dataset: userId, itemId, rate, time
> # AV056ETQ5RXLN,031887,1.0,1397692800
>
> open HD,"rate.csv" or die $!;
> while() {
>  my ($item,$rate) = (split /\,/)[1,2];
>  $hash{$item}{total} += $rate;
>  $hash{$item}{count} +=1;
> }
> close HD;
>
> for my $key (keys %hash) {
>  $stat{$key} = $hash{$key}{total} / $hash{$key}{count};
> }
>
> my $i = 0;
> for (sort { $stat{$b} <=> $stat{$a}} keys %stat) {
>  print "$_: $stat{$_}\n";
>  last if $i == 99;
>  $i ++;
> }
>
> The purpose is to aggregate and average the itemId's scores, and print
> the result after sorting.
>
> The dataset has 80+ million items:
>
> $ wc -l rate.csv
> 82677131 rate.csv
>
> And my memory is somewhat limited:
>
> $ free -m
>totalusedfree  shared  buff/cache
> available
> Mem:   1992 152  76   01763
>1700
> Swap:  1023 802 221
>
>
>
> What confused me is that Apache Spark can make this job done with this
> limited memory. It got the statistics done within 2 minutes. But I want
> to give perl a try since it's not that convenient to run a spark job
> always.
>
> The spark implementation:
>
> scala> val schema="uid STRING,item STRING,rate FLOAT,time INT"
> val schema: String = uid STRING,item STRING,rate FLOAT,time INT
>
> scala> val df =
> spark.read.format("csv").schema(schema).load("skydrive/rate.csv")
> val df: org.apache.spark.sql.DataFrame = [uid: string, item: string ...
> 2 more fields]
>
> scala>
>
> df.groupBy("item").agg(avg("rate").alias("avg_rate")).orderBy(desc("avg_rate")).show()
> +--++
>
> |  item|avg_rate|
> +--++
> |0001061100| 5.0|
> |0001543849| 5.0|
> |0001061127| 5.0|
> |0001019880| 5.0|
> |0001062395| 5.0|
> |143502| 5.0|
> |14357X| 5.0|
> |0001527665| 5.0|
> |000107461X| 5.0|
> |191639| 5.0|
> |0001127748| 5.0|
> |791156| 5.0|
> |0001203088| 5.0|
> |0001053744| 5.0|
> |0001360183| 5.0|
> |0001042335| 5.0|
> |0001374400| 5.0|
> |0001046810| 5.0|
> |0001380877| 5.0|
> |0001050230| 5.0|
> +--++
> only showing top 20 rows
>
>
> I think my perl script should be possible to be optimized to run this
> job as well. So ask for your helps.
>
> Thanks in advance.
>
> wilson
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Can't call method "Convolve" on unblessed reference at gifaninmation.pl line 47.

2022-02-04 Thread David Mertens
Thanks!

The problem here is actually fairly straight forward: $image->[1] is not
defined. To verify this, try running the code below, which is a trimmed
version of what you provided. You'll notice that I added a few print
statements, for $x in the for-loop and for $p after we get it from
$image->[1]. You'll notice that (1) no numbers print and (2) we get the
warning "Use of uninitialized value $p in concatenation..." arising from
printing $p. So, what ever you are trying to do is not doing what you think
it is doing. :-/

This looks like it is a bunch of example scripts cobbled together. Do you
have an example of just how to convolve an image?

David

--%<
use strict;
use warnings;
use Image::Magick;

# The script reads three images, crops them, and writes a single image as a
GIF animation sequence
my($image,$p,$q);

$image = new Image::Magick;
$image->Read('Bugs_Bunny.svg.png');
$image->Read('ng-clipart-bugs-bunny-witch-hazel-drawing-looney-tunes-cartoon-bugs-and-lola-bunny.png');
$image->Read('k.miff[1, 5, 3]');
$image->Contrast();
for(my $x = 0; $image->[$x]; $x++){
print "$x\n";
$image->[$x]->Frame('100x200') if $image->[$x]->Get('magick') eq 'GIF';
}

$p = $image->[1];

print "$p\n";

$p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);



On Fri, Feb 4, 2022 at 11:05 AM William Torrez Corea 
wrote:

> #!/usr/local/bin/perl
>> use Image::Magick;
>> use PDL::ImageND;
>>
>> # The script reads three images, crops them, and writes a single image as
>> a GIF animation sequence
>> my($image,$p,$q);
>>
>> $image = new Image::Magick;
>> $image->Read('Bugs_Bunny.svg.png');
>>
>> $image->Read('ng-clipart-bugs-bunny-witch-hazel-drawing-looney-tunes-cartoon-bugs-and-lola-bunny.png');
>> $image->Read('k.miff[1, 5, 3]');
>> $image->Contrast();
>> for($x = 0; $image->[$x]; $x++){
>> $image->[$x]->Frame('100x200') if $image->[$x]->Get('magick') eq
>> 'GIF';
>> undef $image->[$x] if $image->[$x]->Get('columns') < 100;
>> }
>>
>> $p = $image->[1];
>>
>> # Suppose you want to start out with a 100x100 pixel white canvas with a
>> red
>> $image = Image::Magick->new;
>> $image->Set(size=>'100x100');
>> $image->ReadImage('canvas:white');
>> $image->Set('pixel[49,49]'=>'red');
>>
>> # Here we reduce the intensity of the red component at (1,1) by half:
>> @pixels = $image->GetPixel(x=>1,y=>1);
>> $pixels[0]*=0.5;
>> $image->SetPixel(x=>1,y=>1,color=>\@pixels);
>>
>> # Or suppose you want to convert your color image to grayscale
>> $image->Quantize(colorspace=>'gray');
>>
>> # Let's annotate an image with a taipai truetype font
>> $text = 'Works like magick!';
>> $image->Annotate(font=>'kai.ttf',
>> pointsize=>40,fill=>'green',text=>$text);
>>
>> # Perhaps you want to extract all the pixel intensities from an image and
>> write them to STDOUT
>> @pixels = $image->GetPixels(map=>'I',
>> height=>$height,width=>$width,normalize=>true);
>> binmode STDOUT;
>> print pack('B*',join('',@pixels));
>>
>> # Other clever things you can do with a PerlMagick objects include
>> $i = "$#$p+1";   # return the number of images associated with object p
>> push(@$q, @$p); # push the images from object p onto object q
>> @$p = (); #delete the images but not the object p
>> $p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);
>>
>
> On Fri, Feb 4, 2022 at 9:41 AM David Mertens 
> wrote:
>
>> The issue isn't having the method, the issue is calling it on an
>> unblessed reference. Can you show the code that gives this problem?
>>
>> Btw, my bet is that it's not a matter of having PDL's method. You would
>> use PDL for number crunching, not gif animation. :-)
>>
>> David
>>
>> On Fri, Feb 4, 2022, 10:14 AM William Torrez Corea 
>> wrote:
>>
>>> Ready, install the method from https://metacpan.org/pod/PDL::ImageND.
>>>
>>> $p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);
>>>>
>>>
>>>
>>> According to the function convolve this declaration is of the following
>>> way
>>>
>>>> $new = convolve $x, $kernel
>>>

Re: Can't call method "Convolve" on unblessed reference at gifaninmation.pl line 47.

2022-02-04 Thread David Mertens
The issue isn't having the method, the issue is calling it on an unblessed
reference. Can you show the code that gives this problem?

Btw, my bet is that it's not a matter of having PDL's method. You would use
PDL for number crunching, not gif animation. :-)

David

On Fri, Feb 4, 2022, 10:14 AM William Torrez Corea 
wrote:

> Ready, install the method from https://metacpan.org/pod/PDL::ImageND.
>
> $p->Convolve([1, 2, 1, 2, 4, 2, 1, 2, 1]);
>>
>
>
> According to the function convolve this declaration is of the following
> way
>
>> $new = convolve $x, $kernel
>>
>> But when I declare the function in my code the result is wrong.
>
> Can't call method "Convolve" on unblessed reference at gifaninmation.pl
>> line 47.
>>
>
> I don't know in what part I am confused.
>
>
>
>
>
>
> On Fri, Feb 4, 2022 at 8:34 AM Andrew Solomon  wrote:
>
>> Is this what you're looking for?
>>
>> https://metacpan.org/pod/PDL::ImageND
>>
>>
>> On Fri, Feb 4, 2022 at 2:30 PM William Torrez Corea <
>> willitc9...@gmail.com> wrote:
>>
>>> I'm using cpanm trying to find a method that contains the function
>>> convolve but I only found the method Imager.
>>>
>>> https://metacpan.org/pod/Imager
>>>
>>> When I try to declare the method and use it in the code, the result is
>>> always wrong.
>>>
>>> I am using the following libraries:
>>>
>>>>
>>>>
>>>>
>>>> *#!/usr/local/bin/perl use Image::Magick;use Imager;*
>>>
>>>
>>>
>>>
>>> --
>>>
>>> With kindest regards, William.
>>>
>>> ⢀⣴⠾⠻⢶⣦⠀
>>> ⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
>>> ⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
>>> ⠈⠳⣄
>>>
>>>
>>
>> --
>> Andrew Solomon
>> Director, Geekuni <https://geekuni.com/>
>> P: +44 7931 946 062
>> E: and...@geekuni.com
>>
>
>
> --
>
> With kindest regards, William.
>
> ⢀⣴⠾⠻⢶⣦⠀
> ⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
> ⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
> ⠈⠳⣄
>
>


Re: perl script question

2019-10-10 Thread David Precious


The error message "FASEQ can not open" doesn't look like something
which could be output by the code you've provided.

*Either* you should get the "can not read open $!" message (where the
$! will be interpolated to the reason it failed), or the message is
coming from whatever the "usearch" tool you're executing is.

Have your script print out the ./usearch invocation instead of running
it, then try running it yourself - if you get the same error then you
at least know that it's not coming from your script.

My guess - did you mean "-ublast FASEQ" to be replaced by the name of
the file you're reading - e.g. "-ublast $files" ? 

Likewise, the "-userout OUTFILE" part of the usearch invocation looks
like you may have intended that to be the output filename, e.g.
"-userout $filename.txt" or similar?

(Incidentally, the var name $files when it holds the singular filename
for each iteration irks me - that'd read much better as e.g.:

for my $filename (glob("$dir/*.fa")) {
...
}


On Thu, 10 Oct 2019 10:21:45 +0800 (CST) 刘东 
wrote:

> hellow:
> I have written a script, but it does not work, can you tell me what
> wrong with me?
> 
> 
> #! /usr/bin/perl
> 
> use strict;
> use warnings;
> use Getopt::Long;
> 
> my ($dir, $files, $file_name, $file_format, $file_dir, $file_main);
> 
> GetOptions ('dr=s'  =>\$dir);
> 
> open INF,"<",'sine.fa' or die "can't read open sine";
> 
> foreach $files (glob("$dir/*.fa")) {
> open FASEQ, "<", $files or die "can not read open $!";
>   ($file_dir, $file_main) = split (/\/\//,$files);
>   ($file_name,$file_format) =split(/\./,$file_main);
> open OUTFILE, '>', $file_name.".txt";
> `./usearch -ublast FASEQ -db INF -evalue 1e-5 -userout OUTFILE
> -strand both -userfields
> query+qlo+qhi+ql+qs+qstrand+target+tlo+thi+tl`; }
> 
> close FASEQ;
> close OUTFILE;
> close INF;
> 
> 
> 
> when I run the script, and the results is "FASEQ can not open".
> additional, the results from glob as follows:
> ./fadata//carp-carp01_TKD181002053-1_1.fa
> ./fadata//carp-carp01_TKD181002053-1_2.fa
> ..
> 
> 
> 
> 
> 
> 
> 
> --
> 
> 湖南师范大学生命科学院:刘 东
>  
> 格言:积土而为山,积水而为海;
> 知识在于积累,成功在于勤奋。

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




Re: Question re remove_tree and symlinks

2019-08-24 Thread David Precious
On Fri, 23 Aug 2019 19:13:15 +0100
Mike Martin  wrote:

> Am I right in believing that remove_tree from File::Path does not
> follow symlinks, ie: does not remove the linked file only the link if
> there are links in the direstories removed

An obvious way to find out would be to just try it - set up two test
directories with files, add a symlink in the first to a file in the
second, then run remove_tree on the first and see what happens :)

Otherwise, File::Path uses unlink() to remove the files, the doco for
which is at https://perldoc.perl.org/functions/unlink.html

That isn't massively detailed, but the manpage for unlink(2) gives much
more detail: https://linux.die.net/man/2/unlink

-- start quote --
unlink() deletes a name from the file system. If that name was the
last link to a file and no processes have the file open the file is
deleted and the space it was using is made available for reuse.

If the name was the last link to a file but any processes still have
the file open the file will remain in existence until the last file
descriptor referring to it is closed.

If the name referred to a symbolic link the link is removed. 
-- end quote --


So, if there's a symlink, you can expect the symlink to be removed, but
the file it linked to to be untouched.  Even if it's a hard link, the
file will still exist unless the other link to it gets removed.

Cheers

Dave P (BIGPRESH)

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




Re: Google Sheets API Data Structure

2019-08-16 Thread David Precious
On Thu, 15 Aug 2019 16:18:57 +0100
James Kerwin  wrote:
> I'm currently investigating a data structure that it gives me to see
> what values I can get from it. Through random experimentation I've
> managed to find that it had "title" and "id".
> 
> I wrote this to get the data structure containing the worksheets:
> 
> my @ws = $spreadsheet->worksheets;
[...]
> If I do this:
[...]
> I get:
> 
> { atom=XML::Atom::Entry=HASH(0x1b81928)
> container=Net::Google::Spreadsheets::Spreadsheet=HASH(0x1bbdb80) }
[...]

Right, from that class name it sounds like you're working with
Net::Google::Spreadsheets -
https://metacpan.org/pod/Net::Google::Spreadsheets

Given that you're talking about $spreadsheet->worksheets I assume that
the bit of code you omitted before your example finds the appropriate
spreadsheet using the spreadsheets() / spreadsheet(%cond) methods, so
$spreadsheet will be a Net::Google::Spreadsheets::Spreadsheet object.

Rather than trying to poke around in the guts of the objects you got,
look at the documentation.  The worksheets() method gives you a list of 
Net::Google::Spreadsheets::Worksheet objects, so have a look at the
documentation for that class:

https://metacpan.org/pod/Net::Google::Spreadsheets::Worksheet

For e.g. you'll see that $worksheet->rows will give you a list of rows,
as Net::Google::Spreadsheets::Row objects, so you could then look at
the documentation for that ::Row class:

https://metacpan.org/pod/Net::Google::Spreadsheets::Row

All in all, you're dealing with objects here, so you're better off
looking at their documentation to see what methods they provide rather
than digging around in their guts - both because it should be easier for
you, but also because your code should be cleaner and more robust, and
less likely to break on future updates to the modules you're using.

Most CPAN authors will try to keep the exposed methods they've
documented consistent as far as possible or do deprecations as cleanly
as possible, but if you've been digging around "under the hood" treating
the object as a hashref, that internal implementation may well change
without notice and that's on you.

Cheers

Dave P (BIGPRESH)

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




Re: Device::SerialPort on debian Buster

2019-07-11 Thread David Precious
On Wed, 10 Jul 2019 08:51:08 -0500
"Martin McCormick"  wrote:
>   The code below talks to what it thinks is a RS-232 serial
> port and controlls a radio scanner so that one can program the
> scanner's settings and enter frequency and operational data.
> Don't worry about all that.  It used to work on debian stretch
> which is last year's version of debian.  After an upgrade to
> buster which is this year's version, also known as debian 10, all
> RS-232 ports appear to be dead.  [...]
> The very same code ported to the buster system also fails with
> Can't call method "baudrate" on an undefined value
> at /home/martin/etc/mm line 121.

OK, so it failed to get the Device::SerialPort object; there's no
error-checking in your code where you instantiate it, so it explodes on
the next line when you try to call a method on it:

> our $port = Device::SerialPort->new("$dev");
> $port->baudrate(115200);

Adding some error checking could help - e.g.:

  my $port = Device::SerialPort->new($dev)
  or die "Failed to open serial port $dev - $!";

I'd guess that $! may contain an error explaining what happened - and
the constructors section of Device::SerialPort's doco supports that.

Try modifying your code as mentioned above, and see what error you
get.  (And, while you're modifying, Shlomi's helpful review of your
code contained loads of useful advice too - but you might want to focus
on just resolving the actual problem first I guess).

I'd also, without having seen anything else, take a gut feeling guess
in the dark that Buster has changed the permissions applied to serial
port device nodes, possibly via AppArmor and that the user you run your
code as no longer has access to open the port - but that *is* just a
guess - you'll know if it's right if your modified code reports a
permissions error as it dies.

If so, you could look at the device node permissions to see the user
and group it's owned by, e.g.:

[davidp@supernova:~]$ ls -l /dev/ttyS0
crw-rw 1 root dialout 4, 64 Jul  8 09:28 /dev/ttyS0

... change to suit which device you're trying to use, and make sure your
user is a member of the group specified.


Cheers

Dave P (BIGPRESH)

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




Re: proper use of TEMPLATE

2019-03-19 Thread David Precious
On Thu, 14 Mar 2019 12:14:59 -0500
Rick T  wrote:
>  Full Course (1
> credit)
> 
> My code can easily replace the correct var with ‘selected’ and the
> other vars on the list with a blank. But what should that blank be?
> 
> Should I make it an empty string, a space, undef, or something else?

An empty string would work just fine.

On the other hand, your template var name there suggests it's a
boolean, indicating whether or not the full course is selected, as
opposed to containing the word "selected" or an empty string.

I'd be inclined to actually make it so, and have the "what do we output
if it *should* be selected" be in the template, e.g.:



Also, as we've not seen your full code, it's not clear how you're
generating the list of options, but you can iterate over a list or
names or hashrefs of information about each entry.

You could, for instance, have a list of hashrefs of information about
each course - the value to use in the value="" attribute, the course
name to display, etc... something like:

  my %courses = (
{ id => 1, name => "Full Course", selected => 1 },
{ id => 2, name => "Half Course", },
  );

(Of course, it's quite likely that in a production system, that
information will have been assembled from a database query or a config
file rather than hard-coded, but you get the idea.)

You could pass that on to your Template->process call, then in the
template itself, output the options with something like:

  [% FOREACH course IN courses %]
[% course.name %]

  [% END %]


That way, your template doesn't need to know/care how many course
options there are or what they are, it only needs to know/care how to
*present* them.

Cheers

Dave P

--
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-11 Thread David Mertens
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
>


Re: Syntax "||" before sub

2018-11-24 Thread David Precious
On Thu, 22 Nov 2018 12:33:25 -0800
"John W. Krahn"  wrote:

> On 2018-11-22 8:08 a.m., David Precious wrote:
> > 
> > 
> > You'll often see these operators used to provide default values.
> > 
> > e.g.
> > 
> >sub hello {
> >my $name = shift;
> >$name ||= 'Anonymous Person';  
> 
> Which is usually written as:
> 
> sub hello {
> my $name = shift || 'Anonymous Person';

Sure - but that doesn't provide a simple, useful example of ||= which
the OP was asking about.
 
> > I do notice that there isn't actually a very useful section on ||=
> > and //= - I may try to raise a pull requests to add more
> > documentation on them.  
> 
> $var ||= 'VALUE';
> 
> Is just shorthand for:
> 
> $var = $var || 'VALUE';
> 
> The syntax is borrowed from the C programming language and it is 
> slightly more efficient when compiled to machine code.

Indeed - *I* know that, but it strikes me that it could be better
documented, to make it easier for people new to Perl to find the
documentation.  I went to perldoc perlop expecting to be able to find a
section to point the OP at as a "here's the documentation for it", and
couldn't find anything particularly useful.

That strikes me as sub-optimal :)

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




Re: Syntax "||" before sub

2018-11-22 Thread David Precious
On Thu, 22 Nov 2018 13:48:18 +
James Kerwin  wrote:

> Hi All,
> 
> I'm looking through some Perl files for software we use and I noticed
> this in one of the config files:
> 
> $c->{guess_doc_type} ||= sub {
> 
> All other similar config files show a similar structure, but without
> the "||" before the equals sign:
> 
> $c->{validate_document} = sub {
> 
> My question is, what is the "||" doing in the first example?

"||=" is the conditional assignment operator - it assigns the value on
the right-hand side to the thing on the left hand side, *but* only if
the thing on the left hand side doesn't evaluate to a true value.

For instance,

  my $foo;
  $foo ||= "Bar";
  # $foo is now "Bar" - because it wasn't true before
  $foo ||= "Baz";
  # $foo is still "Bar" - it wasn't changed

In your example, the value on the right hand side is a coderef = so, if
$c->{guess_doc_type} didn't already contain something truthy, then it
will be set to a coderef defined by the sub { ... } block.

See also "//=", the defined-or operator, which works in pretty much the
same way, but checking for definedness - the difference being that if
the left hand side contained something that wasn't a truthy value, ||=
would overwrite it, whereas //= wouldn't.

You'll often see these operators used to provide default values.

e.g.

  sub hello {
  my $name = shift;
  $name ||= 'Anonymous Person';
  return "Hi there, $name!";
  }


> I've attempted to Google this, but found no solid answer.

Yeah, Googling for operators when you don't know their name is rarely
useful :)  Perl's operators are all documented quite well at:

https://perldoc.perl.org/perlop.html

I do notice that there isn't actually a very useful section on ||=
and //= - I may try to raise a pull requests to add more documentation
on them.

Cheers

Dave P

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




Re: Common regex for timedate

2018-11-12 Thread David Y Wagner via beginners
You could change the first portion of RegEx to

\d{2}[.\-](\d{2}|\D{3})[.\-]

This could cover all the valid Month codes, but this will look for the hyphen 
or period and two digit month or three non digits and then the the hyphen or 
period following. There is much more you could do, but this covers the initial 
start.

Did not test, but believe it should find both items...

Wags ;)
WagsWorld
Hebrews 4:15
Ph(primary) : 408-914-1341
Ph(secondary): 408-761-7391
On Nov 12, 2018, 09:37 -0800, Asad , wrote:
> Hi all ,
>
>            I have two stings from logfile how can we have a common regex so 
> that its parse datetime details for further parsing ;
>
> Calling upgrade.sql on 05-JUL-18 10.19.42.559000 PM -12
> Calling apply.sql on 17.10.18 12:28:12,447849 +02:
>
> I created on regex : \d\d\.\d\d\.\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]
> this only matches : Calling apply.sql on 17.10.18 12:28:12,447849 +02:
>
> I need a common regex which matches both the lines ?
>
>
> Thanks,
> --
> Asad Hasan
> +91 9582111698


Re: Read a huge text file in perl

2018-05-24 Thread David Precious
On Thu, 24 May 2018 20:27:23 +0530
SATYABRATA KARAN  wrote:

> Hello,
> Does anyone guide me how to handle a huge file (>50GB) in perl?

Your code should be looping line by line, and shouldn't use much
memory, depending of course on how long the lines are, and what you're
doing with them.

It will fall down if the file doesn't contain lines, or if $/ doesn't
match the idea of lines in the file.

You haven't shown what you're actually doing with each line you process
- if you're keeping it in memory, then yes, you'll probably soon run
out of space.

Do you still get problems if you simplify your code to e.g.:

my $lines;
open my $FILE, "<", "Log_file.txt";

while (<$FILE>) {
 $lines++;
}

close $FILE;

print "Saw $lines lines\n";


If so - look at what you're doing with each line (the "some task" bit
of your psuedocode) and see if it's copying each line somewhere or
something.

If not - most likely you need to look at the value of $/ and make sure
it makes sense for the line endings in that file.

Cheers

Dave P

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




Re: captcha

2018-03-05 Thread David Precious
On Mon, 5 Mar 2018 11:43:48 +0700
Eko Budiharto  wrote:

> dear all,
> 
> I am trying to put a captcha on my site. I already have public key
> and secret key from google. I already put the public key on my html
> and secret key on the server respond site. But when I ran it, I got
> an error, For security reasons, you must pass the remote ip to
> reCAPTCHA at respond.pl line 66.
> 
> the CPAN library used is
> 
> Captcha::reCAPTCHA;
> 
> 
> the line 66 (respond.pl) is
> 
> my $result = $c->check_answer(
>      "xxx123", $ENV{'REMOTE_ADDR'},
>      \$challenge, $response
> );

One would assume that $ENV{'REMOTE_ADDR'} is empty, but you haven't
mentioned how you're actually running the code in question, so it's
hard to tell you why / how to get it.

So - how are you running this code? 

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




Re: Checking if a website is up doesn't work correctly

2018-02-24 Thread David Precious
On Sat, 17 Feb 2018 18:21:26 +0100
Manfred Lotz  wrote:

> Thanks.  The attached program does better as https://notabug.org
> works. Only http://scripts.sil.org doesn't work. It seems there are
> special checks active on that site. 

Yeah, some sites block user-agents recognised as robots, scripts etc.

You can, of course, tell LWP to send any other user-agent header you
like, to pretend to be a normal browser, but that coul be consiered to
be a bit shady and deceptive.  Maybe talk to the owners of the site
in question first?

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




Re: Can't use 'defined(@array)'

2018-02-23 Thread David Precious
On Fri, 23 Feb 2018 15:46:14 +0100
"jose cabrera"  wrote:

> Greetings!
> 
> I am using perl v5.22.1, and I am debugging an old script.  The code
> is,
> 
> if (defined($$glob)) {
> $localconfig{$var} = $$glob;
> }
> elsif (defined (@$glob)) {
> $localconfig{$var} = \@$glob;
> }
> elsif (defined (%$glob)) {
> $localconfig{$var} = \%$glob;
> }
> 
> when I run it, the interpreter errors out with:
> 
> Can't use 'defined(@array)' (Maybe you should just omit the
> defined()?) at Bugzilla/Install/Localconfig.pm line 239,  line
> 755.
> 
> Any thoughts?  Thanks.

At a quick glance, it sounds like that script ought to be changed to
use ref() to see what kind of reference it got, e.g.:

  if (ref $glob eq 'SCALAR') {
  $localconfig{$var} = $$glob;
  } else {
  $localconfig{$var} = $glob;
  }

That approach avoids the somewhat pointless de-ref and re-ref of
hashrefs/arrayrefs that the old code did - *unless* it does that for
good reason, i.e. it will be changing it and doesn't want a reference
but a shallow copy.

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




Re: TLS and Perl

2018-01-25 Thread David Precious

On Thu, 25 Jan 2018 10:19:26 -0800 SurfShop
 wrote:
>   I keep getting emails from Authorize.net about their upcoming
> disablement of TLS 1.0 and TLS 1.1 and I need to know if that has
> anything to do with Perl or not.  I don't have any code in SurfShop
> that references either SSL or TLS, so maybe that's handled by Perl
> itself or a module I'm using like SSLeay.

Well, how do you interact with Authorize.net?

For instance, if you use Business::AuthorizeNet::CIM to deal with them,
then that uses LWP::UserAgent under the hood for the communication with
authorize.net; it doesn't set any SSL/TLS-specific options when calling
LWP::UserAgent, unless you're causing it to yourself.

LWP::UserAgent will use LWP::Protocol::https to talk to remote
servers over SSL, using either IO::Socket::SSL or Net::SSLeay under the
hood.

In the absence of any specific instructions otherwise (which B::A::CIM
doesn't provide), IO::Socket::SSL will use a sane, secure set of
ciphers.

If LWP::UserAgent is using IO::Socket::SSL, then setting
$IO::Socket::SSL::DEBUG to a suitable value should let you see what
it's doing, and what ciphers it negotiates IIRC.

If you've made use that the openssl library and the above-mentioned
modules are up to date, you're likely to be fine.

I do believe they disabled TLS 1.0 on their testbed in advance though,
so to be confident, point your code at their testbed and check that it
works - if so, all is well!

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




Re: Getting the results of a remote script

2017-11-09 Thread David Precious
On Thu, 9 Nov 2017 12:50:05 -0800
SSC_perl  wrote:

> > On Nov 8, 2017, at 5:16 PM, thelip sia  wrote:
> > 
> > you can use backtick to store the output.
> > $list = `ssh usern...@domain.com
> > '/usr/bin/perl /path/to/dir-list.pl'`;  
> 
>   Thanks, Heince.  I had mistakenly thought those were
> synonymous with each other.  And, of course, this morning, doing a
> new search produced the backtick solution.  However, every example I
> found was for a unix command, not a perl script.  
> 
>   Using the backticks returns nothing, so perhaps my remote
> script may not be set up properly to "export" it's result?  Sorry -
> I've never dealt with WAN solutions like this before so I don't even
> know how to put it.

Using backticks will capture the output of the script, i.e. what it
wrote to STDOUT.  If it didn't output anything, then you won't get
anything.

[...]
>   As I mentioned, my remote script produces an array of
> directory names that I need to use in my local script.  The array is
> populated correctly, but I can't see it in my local script.

Your script won't see any variables etc that were set in the other
script - all you'll have is a blob of text of whatever it output on
STDOUT.


>   Can the result of a remote script be used in a local script
> by calling it via SSH or some other means?  If so, how do you get the
> result back?  This is not a CGI script - that was only an example
> path I used.

As above, you'll get whatever the script output to STDOUT.

Its output could be just a list of filenames which you can then iterate
over, or it could be some JSON or your serialisation format of
preference which you can then deserialise - whatever you like.


For robustness, you probably want to check $? after using backticks to
see the return status, to know if the remote script was executed
successfully or not.

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




Re: Device::SerialPort Am I Missing Something?

2017-11-04 Thread David Precious
On Fri, 03 Nov 2017 15:12:15 -0500
"Martin McCormick"  wrote:
>   What is needed, however, is to be able to use the are_match
> feature which fills a buffer with data until a pattern is
> detected which stops the read and gives you a series of bytes
> that may not necessarily end with a newline or carriage return.
> 
>   I have never yet gotten anything like the following to
> work:
> 
> #!/usr/bin/perl -w
> use strict;
> #use warnings::unused;
> use File::Basename;
> use File::Copy;
> use File::Spec;
> use Time::Local;
> use Device::SerialPort;
> 
> sub comm {#serialport
> 
> my $dev = "/dev/ttyS0";
> my $c = "";
> my $port = Device::SerialPort->new("$dev");
> $port->baudrate(9600); $port->databits(8); $port->parity("none");
> $port->stopbits(1); $port->handshake("none");
>$port->write_settings;
> 
>  until ("" ne $c) {
> my $c= $port->lookfor;
> print ("$c\n") if $c;
> }
> return;
> }#serial port
[...]
>   I have tried it with and without an are_match pattern and
> it just roars along, looping endlessly at the lookfor statement
> and never picking up anything.

Instead of assigning the result of $port->lookfor to $c, you've created
a new lexically-scoped $c (with "my $c") which exists only within that
loop body execution - after the execution, it goes away, and the $c the
loop is looking for, at a higher scope, will still be empty.

I imagine things might change if you remove the "my" from that
assignment, so that you're assigning the result to the $c that the loop
condition is looking at.

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




Re: Problems Receiving this List

2017-11-04 Thread David Precious
On Fri, 03 Nov 2017 15:29:29 -0500
"Martin McCormick"  wrote:

> I just posted a message to this list about the Device::SerialPort
> module.  I finally saw my message reflected back to me after
> quite a bit of head-scratching and some correspondence with the
> list-owner address.  All message traffic from this list stopped
> arriving some time during October 16.  I now appear to be
> receiving messages again after unsubscribing and re-subscribing
> to this list.

If I had to guess, I'd say that your subscription was automatically
disabled after several messages bounced, if you've had any mail
reception issues at any point.  Only a guess though, as I can't see any
details from the mailing list back end.


> I tried a couple of earlier attempts at sending my
> question and never saw it or any answers.  If this is the third
> time that everybody has seen my question, my sincere apology.

Doesn't look like the others arrived - before today's two messages, the
last one on list I see from you ws back in 2015.


> If there is an archive, I can read any answers there

There's a couple of options:
https://www.nntp.perl.org/group/perl.beginners/
https://www.mail-archive.com/beginners@perl.org/


(CC'd a copy to you direct just in case you stop receiving list mail
again :) )

Cheers

Dave P

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




Re: Heredocs with \r\n on linux

2017-10-16 Thread David Santiago
I'm implementing a network protocol that is line oriented (\r\n).

I thought of :

my $string = <<"END";
Hello\r
World\r
END

but i don't know how a socket printing will work in windows (i don't
have one to test it)

I'm writing to the socket like this: print $socket $string


Best regards,
David Santiago

2017-10-16 1:12 GMT+02:00 Kent Fredric :
> On 16 October 2017 at 11:24, David Santiago  wrote:
>>
>> Hi.
>>
>> How can i have \r\n on my heredoc string when i run my script in linux?
>>
>> my $text=<<"END";
>> This is one line
>> This is another one
>> Final line
>> END
>>
>> When i print this var in my linux box, i get this:
>> "This is one line\nThis is another one\nFinal line"
>>
>> However i want:
>> "This is one line\r\nThis is another one\r\nFinal line"
>>
>>
>> What's the best way of accomplishing this?
>>
>> I know that i can $text=~ s/\n/\r\n/; but i think it must be a better
>> way...
>>
>
> It may help if you explain why you want this, people tend to want the
> opposite behaviour.
>
> However, depending on why, it may be simpler to just encode the \r
> directly in the heredoc, as, after all, they're just strings.
>
> #!perl
> use strict;
> use warnings;
>
> use Data::Dump qw(pp);
>
> pp <<"EOF";
> Hello\r
> World\r
> EOF
>
> 
>
> "Hello\r\nWorld\r\n"
>
>
> I'm not sure what this does on windows though.
>
>
> --
> Kent
>
> KENTNL - https://metacpan.org/author/KENTNL

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




Heredocs with \r\n on linux

2017-10-15 Thread David Santiago

Hi.

How can i have \r\n on my heredoc string when i run my script in linux?

my $text=<<"END";
This is one line
This is another one
Final line
END

When i print this var in my linux box, i get this:
"This is one line\nThis is another one\nFinal line"

However i want:
"This is one line\r\nThis is another one\r\nFinal line"


What's the best way of accomplishing this?

I know that i can $text=~ s/\n/\r\n/; but i think it must be a better
way...


Best regards,
David Santiago

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




Re: "Information station" using Perl and Raspberry Pi

2017-07-26 Thread David Precious
On Tue, 25 Jul 2017 12:20:44 -0700
SSC_perl  wrote:

> My first though was to use HTML, but I don't want to use a browser to
> display the text onscreen.  Would this require a GUI, such as Tk?

FWIW, it might be sensible to not fully discount the HTML option -
seting an RPi up to start a browser on boot in full-screen "kiosk mode"
is easy, and you could have it load either a page served by the RPi
itself, or hosted elsewhere, whichever you prefer.

I'd say knocking up information pages in HTML would be quicker and
easier than using Tk or similar to write a GUI app.

If you wanted to get real clever, you could look at something like
NeTV, which could take an incoming HDMI feed and overlay text onto it,
so she could be watching telly and still have reminders etc appear (it
wasn't clear from your original question whether you intended for the
TV to still be used for watching things, or solely dedicated to
displaying information).  I've not used them, though, so that's not a
personal recommendation, just another option to consider.

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




Re: "Information station" using Perl and Raspberry Pi

2017-07-25 Thread David Mertens
Hello Frank,

I think I can speak with some minor authority on this. I am a BeagleBone
Black guy, not a RPi guy, but in this situation I think my knowledge of one
is enough to inform the other.

The RPi (and BeagleBone Black) run full Linux OSes running on ARM. Perl
runs just fine on these; web servers run without trouble by extension; and
more interestingly, the X windowing system and GUI layers (like Gnome,
Mate, KDE, etc) run just fine, too. These are fully functional computers,
powered by ARM processors. As such, they would do just fine creating a
display if you can plug the monitor into the computer. BeagleBone has
pre-compiled Debian and Ubuntu images; I'm sure RPi does, too.

RPi is supposed to be better than BeagleBone for multimedia because it has
more and varied ports, but the BBB has HDMI output, too. I don't do
mutlimedia with my BBs so I don't know or care. If you can plug your
mother's TV into a DVI input, or get a converter, then you could simply use
the TV as a monitor. Then you could write Tk or Prima applications to
achieve what you want. Language level support (Python, Javascript, or
otherwise) need not have any bearing on your decision here.

BTW, the big differences that I know of between the BB and RPi are:

   - BB is fully open-source, including board layout; RPi is not because of
   Broadcom (I believe) licensing agreements.
   - RPi's Broadcom chipset is more multimedia friendly, and it has two USB
   ports to the BB's one.
   - BB is built on TI's Sitara chip. This chip includes two
   microcontrollers *within the main CPU silicon*, making it all-around
   much better for industrial and real-time applications. It's like having two
   Arduinos baked into the chip. This should have no bearing on your decision,
   but was the deciding factor for me picking BB.

Good luck! Feel free to reach out either on this list or directly if you
have questions!

David

On Tue, Jul 25, 2017 at 7:53 PM, SSC_perl  wrote:

> On Jul 25, 2017, at 2:08 PM, Andrew Solomon  wrote:
>
> In case you want to see what's already out there, the search engine term
> for you is "assistive technology".
>
>
> Thanks, Andrew!  I appreciate it.  However, I think that's more than what
> I need.  I'm really only looking to display formatted text on a TV screen.
>
> ---
>
> I finally found a project that looks similar to what I want to do:
>
> https://www.cnet.com/how-to/turn-an-old-monitor-into-a-
> wall-display-with-a-raspberry-pi/
>
> It's a lot more elaborate than what I need and it uses a web service to
> display the information.  I'd prefer to use Perl, if at all possible, and
> use my own server for the files.
>
> Frank
>



-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: debug perl package

2017-07-17 Thread David Mertens
If you really want a GUI debugger you might consider Padre. I've used it as
a debugger once or twice, but recall running into issues. Note that Padre
has been a stale project for a while, so it may not even install.

A safe bet, if non-gui, is the perl debugger, as already mentioned.

David

On Mon, Jul 17, 2017 at 4:39 PM, Andrew Solomon  wrote:

> There's also a very nice tutorial here http://techblog.net-a-
> porter.com/2014/03/learning-the-perl-debugger-introduction/
>
> On Mon, Jul 17, 2017 at 8:54 PM, Chas. Owens  wrote:
>
>> 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>
>>>
>>
>
>
> --
> Andrew Solomon
>
> Mentor@Geekuni http://geekuni.com/
> http://www.linkedin.com/in/asolomon
>



-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Filehandle within foreach loop

2017-07-16 Thread David Mertens
Yes, and I think that gives us *two* reasons to always explicitly close
filehandles. :-)

David

On Sun, Jul 16, 2017 at 8:00 AM, Shawn H Corey 
wrote:

> On Sun, 16 Jul 2017 07:36:39 -0400
> David Mertens  wrote:
>
> > Also note that lexical filehandles close when they go out of scope
>
> True but you should always explicitly close your files. This gives you
> a chance to report any errors it had, have rather than silently
> ignoring them.
>
>
> --
> Don't stop where the ink does.
>
> Shawn H Corey
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Filehandle within foreach loop

2017-07-16 Thread David Mertens
Even more readable:

FILE: foreach my $file ( @files ) }
  ...
  last FILE if (some_condition);
  ...
}

Also note that lexical filehandles close when they go out of scope, except
for the most recently "stat"ed file. Perl holds a reference to "the most
recently stat-ed filehandle" in "the solitary underscore" as discussed in the
documentation about -X <https://perldoc.perl.org/functions/-X.html>. The
upshot is that a filehandle used in an expression like "-M $fh" will live
until the next such expression, and the filehandle used in the last such
expression won't close until the end of the program.

David

On Wed, Jul 12, 2017 at 5:28 PM, Jim Gibson  wrote:

> If you wish to terminate execution of a foreach loop without iterating
> over all of the elements (@files, in this case) use the “last” statement:
>
> foreach my $file ( @files ) {
>  # process file
>  open( my $fh, ‘<‘, $file ) or die(…);
>  while( my $line = <$fh> ) {
># process line
>  }
>  close ($fh) or die( … );
>
>  last if (some_condition);
> }
>
> If you wish to terminate the foreach loop from inside the while loop, you
> can give the foreach loop a label and use the label in the “last”
> statement. Without an explicit label, “last” will terminate the innermost
> loop (the while loop here):
>
> ALL: foreach my $file ( @files ) {
>  # process file
>  open( my $fh, ‘<‘, $file ) or die(…);
>  while( my $line = <$fh> ) {
># process line
>last ALL if (some_condition);
>  }
>  close ($fh) or die( … );
> }
>
> However, in that case you have skipped the close statement, and the close
> will happen automatically when the file handle $fh goes out of scope, but
> you cannot do any explicit error checking on the close.
>
>
> > On Jul 12, 2017, at 12:20 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.
>
>
>
> Jim Gibson
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


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

2017-07-07 Thread David Mertens
On Thu, Jul 6, 2017 at 11:05 PM,  wrote:

> Perl is highly unusual in that the operator, not the operand, dictates the
>> context.
>>
>
> Good point - and one that I hadn't got around to noticing.
>
> Therefore, the '!' operator has to be set up to either:
> a) operate always in numeric context;
> or
> b) operate always in string context;
> or
> c) operate always in both contexts (as per the current behaviour).
>
> Having an ambivalent '!' operator (where it  alternates between a) and b),
> according to the operand's flags) is therefore not an option.
>
> If we wanted an operator for "logical string negation" and an operator for
> "logical numeric negation" we would need 2 different operators.
>
> Have I got that  somewhere near right ?
>
> Cheers,
> Rob
>

Yes, I think so. Of course, that was one of Larry's design decisions, and I
think it was a good one. If you really care to have operand-dependent
behavior, you can create a class that overloads the operator and carry your
data around in instances of that class.

Also, boolean works beyond simple string and numeric context. In
particular, boolean context coerces undefined values to false without
issuing warnings, and it's not clear how an undefined value would operate
under "logical string negation" and "logical numeric negation".

David

-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


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

2017-07-06 Thread David Mertens
On Thu, Jul 6, 2017 at 9:12 PM,  wrote:

> I find it a little surprising that use of the '!' operator is all that's
> needed to add the stringification stuff:
>
> ...
>
> If the '!' operator didn't do that, then I believe the OP would be seeing
> precisely what he expects.
>
> So ... why should the '!' operator *not* respect the string/numeric
> context of the operand ?
>

Perl is highly unusual in that the *operator*, not the operand, dictates
the context. We know tha

  $foo eq $bar

forces $foo and $bar into string context and then compares them as strings,
and

  $foo == $bar

forces numeric context. I personally find this to be a hallmark of Perl, a
way in which it Does What I Mean, where what I mean is indicated by the
operator that I chose to use.

Assignment like

  $result = !$i

does not provide a context, but the negation operator does---boolean---and
so we get a result that is boolean true or false, both of which are
represented by special kinds of dual-vars, as was already explained.

David

-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Module to extract patterns

2017-06-13 Thread David Mertens
See also Text::Balanced and its extract_quotelike:

http://search.cpan.org/~shay/Text-Balanced-2.03/lib/Text/Balanced.pm#extract_quotelike

David

On Tue, Jun 13, 2017 at 7:23 AM, Paul Johnson  wrote:

> On Tue, Jun 13, 2017 at 02:03:10PM +0300, Lars Noodén wrote:
> > There are a lot of ways to write patterns.
>
> > Is there a module or method that can identify and extract them from a
> > larger bit of code regardless of style?
>
> That's not an easy problem.  Fortunately, there is a module which will
> probably do what you want: PPI.  See https://metacpan.org/pod/PPI
>
> --
> Paul Johnson - p...@pjcj.net
> http://www.pjcj.net
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: How to match words with a consecutive character

2017-05-06 Thread David Emanuel da Costa Santiago

Thanks!

I didn't knew that feature.

It's very cool!

Thank you very much.

Best regards,
David Santiago


On Sat, 6 May 2017 23:03:48 +0100
David Precious  wrote:

> On Sat, 6 May 2017 23:13:04 +0200
> David Emanuel da Costa Santiago  wrote:
> > Is there any regular expression, that can match any consecutive
> > character?  
> 
> You want to use a back-reference - e.g. /(.)\1+/ matches any character
> followed by one or more of that same character.
> 
> Demo:
> 
> [davidp@cloudburst:~/tmp]$ cat re-repeat.pl 
> #!/usr/bin/env perl
> 
> use strict;
> 
> for my $string (qw(qwerty qwertyq qqwerty qwerrrty)) {
> say "$string " .  ($string =~ /(.)\1+/ ? "contains" : "has no")
> . " repeated characters";
> }
> 
> [davidp@cloudburst:~/tmp]$ perl re-repeat.pl
> qwerty has no repeated characters
> qwertyq has no repeated characters
> qqwerty contains repeated characters
> qwerrrty contains repeated characters
> 
> For further info, a read of perlretut should help:
> https://perldoc.perl.org/perlretut.html#Backreferences
> 
> Cheers
> 
> Dave P (bigpresh)
> 

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




Re: How to match words with a consecutive character

2017-05-06 Thread David Precious
On Sat, 6 May 2017 23:13:04 +0200
David Emanuel da Costa Santiago  wrote:
> Is there any regular expression, that can match any consecutive
> character?

You want to use a back-reference - e.g. /(.)\1+/ matches any character
followed by one or more of that same character.

Demo:

[davidp@cloudburst:~/tmp]$ cat re-repeat.pl 
#!/usr/bin/env perl

use strict;

for my $string (qw(qwerty qwertyq qqwerty qwerrrty)) {
say "$string " .  ($string =~ /(.)\1+/ ? "contains" : "has no")
. " repeated characters";
}

[davidp@cloudburst:~/tmp]$ perl re-repeat.pl
qwerty has no repeated characters
qwertyq has no repeated characters
qqwerty contains repeated characters
qwerrrty contains repeated characters

For further info, a read of perlretut should help:
https://perldoc.perl.org/perlretut.html#Backreferences

Cheers

Dave P (bigpresh)

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




How to match words with a consecutive character

2017-05-06 Thread David Emanuel da Costa Santiago

Hello.

Is there any regular expression, that can match any consecutive
character?

Example:

qwerty -> no match
qwertyq -> no match
qqwerty -> match because of the qq
qwerrrty -> match because of the rrr

and so on...

My obvious attempts that resulted in failure were:

1- /.{2,}/
2- /[qwerty]{2,}

I tried to search a solution on the internet, but i only found solutions
where you need to specify a single character:

q{2,}
w{2,}
...



Thanks and best regards,
David Santiago

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




Re: How to delete multiple indices from array

2017-04-12 Thread David Emanuel da Costa Santiago


Thank you all for your replies.

I tested the 5 solutions with the benchmark module, and the splice one
is the fastest:

Benchmark: timing 99 iterations of each, grep, hash, keep, splice...
 each:  5 wallclock secs ( 5.07 usr +  0.00 sys =  5.07 CPU) @
197238.46/s (n=99) 

 grep:  5 wallclock secs ( 4.96 usr +  0.00 sys =  4.96 CPU) @
201612.70/s (n=99) 

 hash:  6 wallclock secs ( 5.37 usr +  0.00 sys =  5.37 CPU) @
186219.55/s (n=99) 

 keep:  2 wallclock secs ( 1.68 usr +  0.00 sys =  1.68 CPU) @
595237.50/s (n=99)

splice:  1 wallclock secs ( 1.40 usr +  0.00 sys
=  1.40 CPU) @ 714285.00/s (n=99)


The hash one, which was my first solution is the slowest one. :-(

code and results: https://pastebin.com/FYdJzVzu

Thanks all.

Regards,
David Santiago


On Wed, 12 Apr 2017 16:50:49 -0400
Uri Guttman  wrote:

> On 04/12/2017 04:38 PM, Shlomi Fish wrote:
> > Hi Uri!
> >
> > Some notes.
> >
> > On Wed, 12 Apr 2017 15:19:33 -0400
> > Uri Guttman  wrote:
> >  
> >> On 04/12/2017 03:00 PM, David Emanuel da Costa Santiago wrote:  
> >>> Hello!
> >>>
> >>> What's the best way to delete multiple indices from an array?
> >>>
> >>> i'm doing:
> >>>
> >>> ---
> >>> my @array=qw/zero one two three four five six seven eight nine
> >>> ten/;
> >>>
> >>> my @indicesToDelete = (2,4,6,7);  
> >> if you have the indexes to keep, this would be a simple slice:
> >>
> >> my @keep_indexes = ( 0, 1, 3, 5, 8,9 10 );
> >> @array = @array{ @keep_indexes } ;
> >>  
> > it should be «@array = @array[ @keep_indexes ] ;» instead. With
> > curly braces it's a hash slice.
> >  
> >> so one idea is to make that list of kept indexes from the list of
> >> indexes to delete:
> >>
> >> my %keep_hash ;
> >> @keep_hash{ 0 .. 10 } = () ; # no need for any values so this save
> >> space delete @keep_hash{ @keep_indexes } ;
> >> @array = @array{ keys %keep_hash } ;  
> > 1. again - square brackets instead of curly braces.  
> yes, i missed that.
> >
> > 2. You should sort the keys numerically using sort { $a <=> $b }.  
> i don't sort the indexes. that was some other code.
> >
> > 3. Better use « keys@array» or in older perls «0 .. $#array» to
> > avoid hard coding the values and magic constants.  
> again, that wasn't my code.
> 
> > 
> >
> > some other ways I can think of doing it:
> >
> > 1.
> >
> > my %blacklist;
> > @blacklist{@remove} = ();
> > @array = @array[grep {!exists $blacklist{$_} } keys@array];
> >
> > 2.
> >
> > my @new;
> > while (my ($i, $v) = each@array)
> > {
> > if (@remove and $i == $remove[0])
> > {
> > shift@remove;
> > }
> > else
> > {
> > push @new, $v;  
> > }
> > }
> > @array = @new  
> that is a much slower version of your grep solution.
> 
> uri
> 

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




Re: How to delete multiple indices from array

2017-04-12 Thread David Emanuel da Costa Santiago

Thanks for your reply. I didn't think about that! :-)

While i was reading your email, i remembered about splice, so i guess
i'm going to end up with:

--
my @array=qw/zero one two three four five six seven eight nine ten/;

my @indicesToDelete = (2,4,6,7);

my $deviation = 0;
splice(@array, $_-$deviation++,1) for(@indicesToDelete);
--

Thank you all!


Regards,
David Santiago


On Wed, 12 Apr 2017 15:19:33 -0400
Uri Guttman  wrote:

> On 04/12/2017 03:00 PM, David Emanuel da Costa Santiago wrote:
> > Hello!
> >
> > What's the best way to delete multiple indices from an array?
> >
> > i'm doing:
> >
> > ---
> > my @array=qw/zero one two three four five six seven eight nine ten/;
> >
> > my @indicesToDelete = (2,4,6,7);  
> 
> if you have the indexes to keep, this would be a simple slice:
> 
> my @keep_indexes = ( 0, 1, 3, 5, 8,9 10 );
> @array = @array{ @keep_indexes } ;
> 
> so one idea is to make that list of kept indexes from the list of 
> indexes to delete:
> 
> my %keep_hash ;
> @keep_hash{ 0 .. 10 } = () ; # no need for any values so this save
> space delete @keep_hash{ @keep_indexes } ;
> @array = @array{ keys %keep_hash } ;
> >
> > my %hash;
> >
> > @hash{(0..scalar(@array)-1)} = @array;
> >
> > delete $hash{$_} for @indicesToDelete;  
> delete can be used on a slice
> 
> delete @hash{ @indicesToDelete } ;
> 
> uri
> 
> Perl Guru for hire. Available for contract/full time/part time Perl
> hacking.
> 

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




How to delete multiple indices from array

2017-04-12 Thread David Emanuel da Costa Santiago

Hello!

What's the best way to delete multiple indices from an array?

i'm doing:

---
my @array=qw/zero one two three four five six seven eight nine ten/;

my @indicesToDelete = (2,4,6,7);

my %hash;

@hash{(0..scalar(@array)-1)} = @array;

delete $hash{$_} for @indicesToDelete;

@array = values(%hash);

--

but this doesn't seem to me much efficient. Is there a better way to do
this?

Btw, for this case the order doesn't matter.

Thanks!

Best regards,
David Santiago

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




Re: Problem using Email::Mailer on laptop

2017-03-20 Thread David Precious
On Mon, 20 Mar 2017 11:23:39 -0700
SSC_perl  wrote:

>   Is anyone here successfully using Email::Mailer?  I installed
> it using cpanm a couple of days ago on my Mac and I can’t get it to
> work.  It just prints the following output and nothing more:
> 
> postdrop: warning: mail_queue_enter: create file
> maildrop/352468.4276: No such file or directory
> 
>   It appears to be trying to send it directly from my laptop,
> but I have the SMTP transport variable set up correctly (which works
> just fine using Email::Stuffer, BTW).  I really like what I’ve read
> about this new module - I just need to get past this hurdle.

I think you found a bug in Email::Mailer.

Looking at its code, it sends the mail by calling
Email::Sender::Simple, as :

# send the email with Email::Sender::Simple
sendmail( $email_mime, $mail->{transport} );

but looking at Email::Sender::Simple->send_email(), :

sub send_email {
my ($class, $email, $arg) = @_;
 
my $transport = $class->default_transport;
 
if ($arg->{transport}) {
...

So, Email::Sender::Simple->send_email() is expecting the second
parameter passed to it to be a hashref containing a 'transport' key,
but it's getting passed an Email::Sender::Transport::SMTP object
instead, and ignoring it.

I've taken the liberty of raising this on Github for you, since I'd
already done the digging and found the code in question, so it was just
as easy to more or less copy & paste this reply into a ticket:
https://github.com/gryphonshafer/Email-Mailer/issues/1


Cheers

Dave P  (BIGPRESH)

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




Re: Strange HASH(0x2ced735) values

2017-02-21 Thread David Precious
On Tue, 21 Feb 2017 09:11:10 -0800
SSC_perl  wrote:

> > On Feb 21, 2017, at 8:34 AM, Uri Guttman 
> > wrote:
> > 
> > you can't trace it from the value. but you can write code where
> > that value is stuffed into the db and look for a reference vs 1 or
> > a blank. then you can dump the call stack (with caller()) or do
> > other debugging. something is putting a hash reference in there
> > that shouldn't be doing it.
> 
>   Thanks Uri, but that’s the problem - so far I haven’t been
> able to tell where it’s being generated.  That’s why I was hoping to
> decode that value.  Looks like I’ll have to continue searching. :\

As Uri explained, there's nothing to "decode" - it's just a string
representation, where the value is the memory address that hash had at
the time.

Look at all the places you execute queries; (at least) one of them is
accidentally passing a hashref instead of a scalar value.

--
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-06 Thread David Emanuel da Costa Santiago


Thanks :-)

A little bug:

> my %HexCodes = map { ord($_) => sprintf '%02X', $_ } ( 0 .. 128 );
my %HexCodes = map { chr($_) => sprintf '%02X', $_ } ( 0 .. 128 );




-- 
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-06 Thread David Emanuel da Costa Santiago

Hi!

You can use "chomp" to remove the $/ from the end of the line.


If you want to replace all non printable characters you can do
something like:

### START

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

my $s="This is my string! \r\n the end";

say "String before: $s";

#Change the character class you want
$s =~ s/([^[:print:]])/$HEXCODES{ord($1)}/g;

say "String after: $s";

END


Regards,
David Santiago


On Tue, 6 Sep 2016 11:11:35 -0500
Matt  wrote:

> I am receiving log entries as a string and then writing them to a file
> with the date tacked on beginning.  Problem is that sometimes the
> string I receive contains \n and it makes parsing the file with grep
> more difficult.  Looking for a simple way to replace all \n in the
> string with text  or something of that sort.
> 
> It might be even better yet to replace all characters that are not
> directly printable with there HEX equivalent since I only need
> readable text log info on one line.  Is there an easy way to do that?
> 


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




Re: file type

2016-08-24 Thread David Mertens
I don't know if I add much to the conversation by simply referring you to
Stack Overflow, but I've found that it's a decent repository of information
for this sort of question. This question has been asked and answered before
<http://stackoverflow.com/questions/22827465/what-perl-module-turns-a-file-extension-txt-jpeg-into-a-mime-type-text-pla/22832209#22832209>,
and the answer suggests MIME::Types, but also suggests some content
introspection, for which File::Type and File::MimeInfo::Magic get a mention.

I have never used any of these, so I cannot vouch for them myself.

Good luck!
David

On Tue, Aug 23, 2016 at 12:41 PM, hw  wrote:

>
> Hi,
>
> what´s the best way to find out of what type a file is?
>
> I´ll probably be having a list of available files and another
> list of the types I´ll be interested in, and it needs to be
> determined which files in the list of available files need to
> be processed further and which ones are to be ignored.  This
> is to store references to files in a database with some
> additional information, like the type of the file that is
> being referenced.
>
> File::MimeInfo looks most promising, but maybe there´s a
> better way?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Perl regular expression for implementing infix expression parser?

2016-08-14 Thread David Mertens
Hello siegried,

This is a fun question! In fact, v5.10 introduced "recursive subpatterns"
into the regex lexicon, so using v5.10 or later, it is possible to
implement (rather arcane) recursive descent regexes without resorting to
eval-based tricks like what you have.

But before diving in, a few questions are in order to make sure we're
working on the right problem. Are you looking for something to cram into a
one-liner? (Do you have a character limit for your one-liner?) Are you open
to solving this with a combination of for-loops and regexes, or do you want
a purely regex-based approach? Are you trying to write something that
merely validates that an expression as valid mathematics, or do you want to
build an abstract syntax tree? Are you interested in using modules from
CPAN or rolling your own? Finally, if your answer to all of these is, "I'm
just curious!", then you can expect to get a very wide range of answers,
not just regex-based.

Thanks! Looking forward to the fun!
David

On Sat, Aug 13, 2016 at 3:45 PM, Richard Heintze via beginners <
beginners@perl.org> wrote:

> I this perl one liner for evaluating infix expressions in VI and emacs/VI
> emulator mode:
>
>
>
> .! perl -MPOSIX   -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np })
> )* \) }x;$funpat = qr/$np/;} s/($funpat)=($funpat|(")[^\"]*
> (")|[0-9\.]+)/"$1=$3".eval($1)."$4"/ge'
>
> That $np is from the camel book and it is a regular expression that parses
> nested sets of parentheses and then my replace command evaluates the
> arithmetic expression.
>
> Since perl accommodates recursive regular expressions, it ought to be
> possible to implement a recursive decent parser.
>
> Can someone help me enhance the above code so that instead of just blindly
> looking for balanced parenthesis, the regular expression will recognize
> (match) this:
>
> 5+8*(2+8/(3+2))*(2+22/3)=()
> Thanks
> siegfried
>
>
>


-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


Re: Interpolation Problem

2016-07-17 Thread David Mertens
To second what Uri mentioned already,

On Fri, Jul 15, 2016 at 11:42 PM, AC P  wrote:

> 
>
> Argument "$command" isn't numeric in subtraction (-) at then it gives me
> my directory.
>
> 
>

This error message (I suspect it was a warning, can you check?) probably
reported a line number. Could you post the code in the vicinity of that
line number? (Better yet, post your whole script, or enough of a script to
reproduce the problem.)

Thanks!
David

-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan


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

2016-06-30 Thread David Emanuel da Costa Santiago

Hi Ron.


I had that problem not with the DBI module, but when i was using
Inline::C.

To solve that issue i had to compile my code with the gcc flag
-DPERL_IMPLICIT_SYS

Maybe it helps if you can recompile the bit that has the XS (or C) code with 
that flag?



Best regards,
David Santiago



On Tue, 17 May 2016 14:16:16 -0500
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: having trouble understanding the built-in Perl sort with regards to mixed numbers and strings

2016-06-28 Thread David Precious
On Fri, 17 Jun 2016 14:33:12 -0700
Kenneth Wolcott  wrote:

> Hi;
> 
>   I'm having trouble understanding the built-in Perl sort with regards
> to mixed numbers and strings
> 
>   I'm looking at http://perldoc.perl.org/functions/sort.html
> 
>   I have an array that I want to have sorted numerically and
> descending.
[...]
>   What I did as a workaround was to implement my own extremely
> brute-force sort routine, which works, but is very ugly.
[...]
>   I'd rather that my code be correct, intuitive and elegant (and
> efficient).

Personally, I'd skip Perl's built-in sort for this and look at using
Sort::Naturally - it handles just this case cleanly and simply, and
will leave your code readable.

e.g.:

my @strings = (
'999 zzz',
'111 zzz',
'111 aaa',
'999 aaa',
);

print join "\n", Sort::Naturally::nsort @strings;

... would get you the output I think you want, i.e.:

  111 aaa
  111 zzz
  999 aaa
  999 zzz


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




Re: Script runs slower with better hardware

2016-06-02 Thread David Santiago
Hi James.

I'm going to take a look into the perf utility. The `openssl speed`
command shows that the desktop cpu has a bigger throughput than the
laptop, so the write_partial in the desktop shouldn't spend that much
time (in comparison to laptop).

The output of the openssl command is attached.

Regards.
David Santiago

2016-06-02 16:59 GMT+02:00 James Alton :
> The laptop has better specs in terms of number of threads and memory
> bandwidth.  I'd also have a play around with the "perf" command if all other
> software versions are the same and you want to see if the lower level CPU
> usage is different.
> https://perf.wiki.kernel.org/index.php/Tutorial
> http://sandsoftwaresound.net/perf/perf-tut-count-hw-events/
>
> James Alton
>
> On Thu, Jun 2, 2016 at 12:25 AM, David Emanuel da Costa Santiago
>  wrote:
>>
>>
>> Hello James,
>>
>> The CPUs are
>>
>> Laptop CPU: i7 Q 720
>> Desktop CPU: i5 6500T
>>
>> The rest of software i'm using:
>>
>> Perl version: 5.22
>> Net::SSLeay version: 1.72
>> Openssl version: 1.0.2.h
>> OS: Archlinux up to date.
>>
>>
>> The script is single threaded and i'm using a single
>> IO::Select->select() to know when i should write or read.
>>
>> CPU comparison:
>> http://cpuboss.com/cpus/Intel-Core-i7-720QM-vs-Intel-Core-i5-6500T
>>
>>
>>
>> Regards,
>> David Santiago
>>
>> On Wed, 1 Jun 2016 19:20:00 -0600
>> James Alton  wrote:
>>
>> > Can you please give specs on both CPUs? (The exact manufacturer and
>> > model.)
>> >
>> > Is there a reason why you think one CPU is better than another? You
>> > can have a CPU that's old and fast at single threaded jobs (say an old
>> > overclocked dual core 4.0Ghz CPU) and a newer CPU that's slower at
>> > single threaded jobs and faster at multi-threaded jobs (say a Core i7
>> > 3.2GHz). Is the program you're trying to run written in such a way as
>> > to take advantage of multi-threading? Was the benchmark you mentioned
>> > a single threaded or multi-threaded benchmark?
>> >
>> > Best wishes,
>> > James Alton
>> >
>> >
>> > On Wed, Jun 1, 2016 at 12:25 PM, David Emanuel da Costa Santiago <
>> > deman...@gmail.com> wrote:
>> >
>> > >
>> > > Hi!
>> > >
>> > > I have a script that writes to a socket, but i noticed that the same
>> > > script have diferent speeds on different machines. It's faster on my
>> > > 5 year laptop than on my desktop.
>> > >
>> > > I profiled the script on both machines and some functions are taking
>> > > almost the double of the time! Example:
>> > >
>> > > on my laptop:
>> > >
>> > > Calls: 10631
>> > > Exclusive Time: 28.2s
>> > > Inclusive Time: 28.2s
>> > > Subroutine: Net::SSLeay::write_partial (xsub)
>> > >
>> > > On my desktop:
>> > > Calls: 5057
>> > > Exclusive Time: 45.0s
>> > > Inclusive Time: 45.0s
>> > > Subroutine: Net::SSLeay::write_partial (xsub)
>> > >
>> > >
>> > > Both machines have the same software version (OS, perl,..) and the
>> > > internet connection is the same. The hardware is different.
>> > >
>> > > What puzzles me is that my desktop have better hardware (according
>> > > to the benchmarks on the internet) than my old laptop. However i
>> > > get way worse speeds on my desktop.
>> > >
>> > > On both machines, when the script is running the CPU is around 7%,
>> > > and the RAM usage is between 50MB - 100MB.
>> > >
>> > > The question for one million dollar is "Why?". And how can i improve
>> > > the performance of my desktop to reach the same speed as my laptop
>> > > (considering that i have better hardware on my desktop)? If i
>> > > recompile perl instead of using a binary package, will that make it
>> > > even?
>> > >
>> > >
>> > >
>> > > Regards,
>> > > David Santiago
>> > >
>> > > --
>> > > To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>> > > For additional commands, e-mail: beginners-h...@perl.org
>> > > http://learn.perl.org/
>> > >
>> > >
>> > >
>>
>


openssl_speed_laptop
Description: Binary data


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


Re: Script runs slower with better hardware

2016-06-01 Thread David Emanuel da Costa Santiago

Hello James,

The CPUs are 

Laptop CPU: i7 Q 720
Desktop CPU: i5 6500T

The rest of software i'm using:

Perl version: 5.22
Net::SSLeay version: 1.72
Openssl version: 1.0.2.h
OS: Archlinux up to date.


The script is single threaded and i'm using a single
IO::Select->select() to know when i should write or read.

CPU comparison:
http://cpuboss.com/cpus/Intel-Core-i7-720QM-vs-Intel-Core-i5-6500T



Regards,
David Santiago

On Wed, 1 Jun 2016 19:20:00 -0600
James Alton  wrote:

> Can you please give specs on both CPUs? (The exact manufacturer and
> model.)
> 
> Is there a reason why you think one CPU is better than another? You
> can have a CPU that's old and fast at single threaded jobs (say an old
> overclocked dual core 4.0Ghz CPU) and a newer CPU that's slower at
> single threaded jobs and faster at multi-threaded jobs (say a Core i7
> 3.2GHz). Is the program you're trying to run written in such a way as
> to take advantage of multi-threading? Was the benchmark you mentioned
> a single threaded or multi-threaded benchmark?
> 
> Best wishes,
> James Alton
> 
> 
> On Wed, Jun 1, 2016 at 12:25 PM, David Emanuel da Costa Santiago <
> deman...@gmail.com> wrote:  
> 
> >
> > Hi!
> >
> > I have a script that writes to a socket, but i noticed that the same
> > script have diferent speeds on different machines. It's faster on my
> > 5 year laptop than on my desktop.
> >
> > I profiled the script on both machines and some functions are taking
> > almost the double of the time! Example:
> >
> > on my laptop:
> >
> > Calls: 10631
> > Exclusive Time: 28.2s
> > Inclusive Time: 28.2s
> > Subroutine: Net::SSLeay::write_partial (xsub)
> >
> > On my desktop:
> > Calls: 5057
> > Exclusive Time: 45.0s
> > Inclusive Time: 45.0s
> > Subroutine: Net::SSLeay::write_partial (xsub)
> >
> >
> > Both machines have the same software version (OS, perl,..) and the
> > internet connection is the same. The hardware is different.
> >
> > What puzzles me is that my desktop have better hardware (according
> > to the benchmarks on the internet) than my old laptop. However i
> > get way worse speeds on my desktop.
> >
> > On both machines, when the script is running the CPU is around 7%,
> > and the RAM usage is between 50MB - 100MB.
> >
> > The question for one million dollar is "Why?". And how can i improve
> > the performance of my desktop to reach the same speed as my laptop
> > (considering that i have better hardware on my desktop)? If i
> > recompile perl instead of using a binary package, will that make it
> > even?
> >
> >
> >
> > Regards,
> > David Santiago
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> > For additional commands, e-mail: 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: Script runs slower with better hardware

2016-06-01 Thread David Emanuel da Costa Santiago

Unfortunatelly i don't have a third box. :-(

I'm going to follow your advice and send an email to p5p.

Thank you!

Best regards,
David Santiago

On Wed, 1 Jun 2016 16:17:47 -0400
Uri Guttman  wrote:

> On 06/01/2016 04:04 PM, David Emanuel da Costa Santiago wrote:
> > Hi Kent,
> >
> > They are using the same verion of Net::SSLeay (version 1.72). All
> > the software have the same version.
> >
> > This is not random. This happens 100% of the times.
> >
> > All the settings related to this script are the same.
> >
> > I don't think it's my network card, because i can reach the maximum
> > speed using a different application.
> >  
> 
> i can't say much to help as i would want to be more hands on. but i 
> think this is way too deep for the beginner's list. just knowing
> about nytprof would rule this out! :) why don't you post to p5p where
> you may get a better answer.
> 
> the fact that you are seeing different call counts is very curious.
> that says to me that your code isn't doing the same thing on both
> boxes. i doubt ssleay is doing different paths so that is the first
> thing i would check. maybe the laptop is making more calls but to a
> more efficient internal call? can you trace what is being called in
> each case? maybe the hardware has different ssl support on the two
> boxes? do you have access to a third box to play with?
> 
> thanx,
> 
> uri
> 


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




Re: Script runs slower with better hardware

2016-06-01 Thread David Emanuel da Costa Santiago

The openssl version is 1.0.2.h.

Thanks for your help. I'm going to follow Uri's advice and send an
email to p5p list.

Thank you!

Regards,
David Santiago


On Thu, 2 Jun 2016 08:18:23 +1200
Kent Fredric  wrote:

> On 2 June 2016 at 08:04, David Emanuel da Costa Santiago
>  wrote:
> > They are using the same verion of Net::SSLeay (version 1.72). All
> > the software have the same version.  
> 
> 
> No, not Net::SSLeay ... OpenSSL, which it links against.
> 
> And if you recently upgraded/downgraded OpenSSL to match versions,
> Net::SSLeay will need to be reinstalled to utilize C level changes.
> 
> You could have one machine suffering due to an OpenSSL bug and not
> realise it.
> 
> Things like buffer sizes being different would be enough to change the
> number of calls to write_partial, but what is calling write_partial is
> probably part of this problem.
> 


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




Re: Script runs slower with better hardware

2016-06-01 Thread David Emanuel da Costa Santiago

Hi Kent,

They are using the same verion of Net::SSLeay (version 1.72). All the
software have the same version. 

This is not random. This happens 100% of the times. 

All the settings related to this script are the same.

I don't think it's my network card, because i can reach the maximum
speed using a different application.


Regards,
David Santiago

On Thu, 2 Jun 2016 07:40:00 +1200
Kent Fredric  wrote:

> On 2 June 2016 at 06:25, David Emanuel da Costa Santiago
>  wrote:
> > The question for one million dollar is "Why?". And how can i improve
> > the performance of my desktop to reach the same speed as my laptop
> > (considering that i have better hardware on my desktop)? If i
> > recompile perl instead of using a binary package, will that make it
> > even?  
> 
> 
> Are they using the same SSL Implementation Version?
> 
> Is SSL Configured identically?
> 
> Is the recipient of the SSL connection the same recipient?
> 
> Have you ruled out the problem being just transient ( ie: can you make
> it happen reliably and repeatedly sufficient to rule out it just being
> subject to randomness? I hate asking this question, but I bite this
> one *constantly* because I'm really not good at identifying random )
> 
> Given the code of the function you are calling is an xsub, and that
> xsub hooks directly into SSL, there's a lot of scope here for non-perl
> to be the culprit.
> 
> https://metacpan.org/source/MIKEM/Net-SSLeay-1.74/SSLeay.xs#L1693-1721
> 
> I'd also contend the possiblity your network card could be to blame,
> but demonstrating that would be hard (you'd need to try performing a
> reproduction of the case somehow good luck)
> 
> If you persist you'll probably find the problem eventually, ... just
> ... it might take a few hours. Can you afford to spend a few hours on
> a 20 second difference?
> 
> 


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




Re: Script runs slower with better hardware

2016-06-01 Thread David Emanuel da Costa Santiago

Hi Shlomi,

This snippet is from nytprof, and it's where it's spending most of the
time:

> Calls: 10631
> Exclusive Time: 28.2s
> Inclusive Time: 28.2s
> Subroutine: Net::SSLeay::write_partial (xsub)
> 
> On my desktop:
> Calls: 5057
> Exclusive Time: 45.0s
> Inclusive Time: 45.0s
> Subroutine: Net::SSLeay::write_partial (xsub)
 
One interesting thing is that besides being faster in older hardware
the ammount of calls is also higher in the old hardware.

I can send both html output for ntyprof if you are interested.

The problem is that on the oldest hardware runs faster. 
The SSLeay version is the same on both machines (version 1.72 - the
latest one haven't reach the stable repos yet)


Regards,
David Santiago


On Wed, 1 Jun 2016 22:23:51 +0300
Shlomi Fish  wrote:

> On Wed, 1 Jun 2016 20:25:39 +0200
> David Emanuel da Costa Santiago  wrote:
> 
> > Hi!
> > 
> > I have a script that writes to a socket, but i noticed that the same
> > script have diferent speeds on different machines. It's faster on my
> > 5 year laptop than on my desktop.
> > 
> > I profiled the script on both machines and some functions are taking
> > almost the double of the time! Example:
> > 
> > on my laptop:
> > 
> > Calls: 10631
> > Exclusive Time: 28.2s
> > Inclusive Time: 28.2s
> > Subroutine: Net::SSLeay::write_partial (xsub)
> > 
> > On my desktop:
> > Calls: 5057
> > Exclusive Time: 45.0s
> > Inclusive Time: 45.0s
> > Subroutine: Net::SSLeay::write_partial (xsub)
> > 
> > 
> > Both machines have the same software version (OS, perl,..) and the
> > internet connection is the same. The hardware is different.
> > 
> > What puzzles me is that my desktop have better hardware (according
> > to the benchmarks on the internet) than my old laptop. However i
> > get way worse speeds on my desktop.
> > 
> > On both machines, when the script is running the CPU is around 7%,
> > and the RAM usage is between 50MB - 100MB.
> > 
> > The question for one million dollar is "Why?". And how can i improve
> > the performance of my desktop to reach the same speed as my laptop
> > (considering that i have better hardware on my desktop)? If i
> > recompile perl instead of using a binary package, will that make it
> > even? 
> 
> It may make it a little better, but it should work better on the
> newer machine as it is. You can try using
> https://metacpan.org/pod/Devel::NYTProf to see what consumes the most
> time on the slower-to-run-it machine and see if  you've missed
> something.
> 
> Regards,
> 
>   Shlomi Fish
> 
> > 
> > 
> > Regards,
> > David Santiago
> >   
> 
> 
> 


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




Re: Script runs slower with better hardware

2016-06-01 Thread David Emanuel da Costa Santiago

No. My laptop doesn't have TPM (at least there's no option in the BIOS
to enable/disable and there's nothing in /sys/class/tpm/ ).

My desktop have, but not enabled:

desktop$ cat /sys/class/tpm/tpm0/device/enabled 
0
desktop$

Do you think it's because of that? With other applications i can reach
same speed with SSL on both machines.

Regards,
David Santiago


On Wed, 01 Jun 2016 19:36:06 +0100
Steve Pointer  wrote:

> Does your laptop have a TPM? 
> 
> 
> 
> On 1 June 2016 19:25:39 BST, David Emanuel da Costa Santiago
>  wrote:
> >
> >Hi!
> >
> >I have a script that writes to a socket, but i noticed that the same
> >script have diferent speeds on different machines. It's faster on my
> >5 year laptop than on my desktop.
> >
> >I profiled the script on both machines and some functions are taking
> >almost the double of the time! Example:
> >
> >on my laptop:
> >
> >Calls: 10631
> >Exclusive Time: 28.2s
> >Inclusive Time: 28.2s
> >Subroutine: Net::SSLeay::write_partial (xsub)
> >
> >On my desktop:
> >Calls: 5057
> >Exclusive Time: 45.0s
> >Inclusive Time: 45.0s
> >Subroutine: Net::SSLeay::write_partial (xsub)
> >
> >
> >Both machines have the same software version (OS, perl,..) and the
> >internet connection is the same. The hardware is different.
> >
> >What puzzles me is that my desktop have better hardware (according
> >to the benchmarks on the internet) than my old laptop. However i get
> >way
> >worse speeds on my desktop.
> >
> >On both machines, when the script is running the CPU is around 7%,
> >and the RAM usage is between 50MB - 100MB.
> >
> >The question for one million dollar is "Why?". And how can i improve
> >the performance of my desktop to reach the same speed as my laptop
> >(considering that i have better hardware on my desktop)? If i
> >recompile perl instead of using a binary package, will that make it
> >even?
> >
> >
> >
> >Regards,
> >David Santiago
> >
> >-- 
> >To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> >For additional commands, e-mail: 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/




Script runs slower with better hardware

2016-06-01 Thread David Emanuel da Costa Santiago

Hi!

I have a script that writes to a socket, but i noticed that the same
script have diferent speeds on different machines. It's faster on my
5 year laptop than on my desktop.

I profiled the script on both machines and some functions are taking
almost the double of the time! Example:

on my laptop:

Calls: 10631
Exclusive Time: 28.2s
Inclusive Time: 28.2s
Subroutine: Net::SSLeay::write_partial (xsub)

On my desktop:
Calls: 5057
Exclusive Time: 45.0s
Inclusive Time: 45.0s
Subroutine: Net::SSLeay::write_partial (xsub)


Both machines have the same software version (OS, perl,..) and the
internet connection is the same. The hardware is different.

What puzzles me is that my desktop have better hardware (according
to the benchmarks on the internet) than my old laptop. However i get way
worse speeds on my desktop.

On both machines, when the script is running the CPU is around 7%, and
the RAM usage is between 50MB - 100MB.

The question for one million dollar is "Why?". And how can i improve
the performance of my desktop to reach the same speed as my laptop
(considering that i have better hardware on my desktop)? If i recompile
perl instead of using a binary package, will that make it even?



Regards,
David Santiago

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




Re: Forking as another user

2016-03-11 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256



Hello all.

AFAIK, you can't do that. When you fork everything is copied, including
the UID.

To achieve something similar, you need to launch another process (but
you'll need to be root to launch it as another user)

Regards,
David Santiago


On Fri, 11 Mar 2016 20:22:34 +0200
Lars Noodén  wrote:

> If I have the code below to fork a child process, how would the right
> way be to fork as a different user?  I gather that fork() itself does
> not support that, so some other method must be used.
> 
> Regards,
> Lars
> 
> -
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> use English;# for $UID and such
> 
> my $old_uid = $UID;
> 
> $UID = 1000;
> 
> my $pid = fork();
> 
> unless( $pid ){
> print qq(\tThis is a child process, PID $$\n);
> print qq(\tThe child UID is $UID \n);
> 
> sleep 5;
> 
> exit 0;
> }
> 
> $UID = $old_uid;
> 
> print qq(This is the parent process PID $PID and a child PID is
> $pid\n); print qq(The parent is running as UID $UID \n);
> 
> sleep 5;
> 
> exit 0;
> 

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJW41ZSAAoJEJ/OLjwuDYzKj+sH/i2IT675x1DJeEDUlkPPchtA
p9GXwHPm0YTp/DPuyAlsS0UVe4jGB4dmhOk+F+3xaE9y4muv/s+F0HxQtWiGZLs9
DbhBoIjWcsgcC2z4ithRtb4gCHNHmr5Wbf+J7dZWIM/ajVhB7ZgGhaHvFWIJFfkg
xYB8jr93IC26XlxNPAtOmRB9jOIrUWnz5+Ut/eBlTdaKc8JW4VZ7NNiaHPx2LyVl
dR2R6YH5s3o6yNoyMO0RnBcRcqXL/3e+s37ufmRslPKjSnoz+dg8V+WeV8PT2AQv
FNQjV8fZMxY+gKDAKAjyZCDUl5vsZOsnc94toO3L2WyHl55e13+cZKcB//PljXU=
=eyGf
-END PGP SIGNATURE-


Re: Is perl user specific

2016-01-06 Thread David Santiago
Hello Ankita,

The error says all:
Perl 5.008003 required--this is only version 5.00503

You are running perl version 5.005, but the minimum version to run is
5.008. Most probably the other account on the same server have a
higher perl version installed (please check perlbrew
(http://perlbrew.pl/) to learn how to install multiple versions on the
same machine)

Regards!

2016-01-06 16:34 GMT+01:00 Ankita Rath :
> Hi all,
>
> I am trying to execute some perl scripts. I am getting following error. But
> other user in the same server are not getting the error. So can anybody help
> me understanding this problem. And why other user are not getting the error.
>
> Perl 5.008003 required--this is only version 5.00503, stopped at
> ../Modules/Modules_64/5.8.5/x86_64-linux-thread-multi/YAML/XS.pm line 26.
> BEGIN failed--compilation aborted at
> ../Modules/Modules_64/5.8.5/x86_64-linux-thread-multi/YAML/XS.pm line 26.
>
> Regards,
> Ankita
>
>
>
>
>

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




Re: How to interpolate a hash in a string

2015-11-04 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Thank you all for your replies. I was able to do what i needed. I
changed a bit the conf files syntax:

conf file 1:
option=value
option2=value2

conf file 2:
property=propertyValue
property2=propertyValue2
property3=${option2} #instead of $OPTS{option2}

Read function: 

sub _read_conf_file{
  my $file = shift;

  open my $FH, '<', $file;

  while(my $line = <$FH>){
chomp $line;
$line =~ s/\#.*//;
next if $line =~ /^\s*$/;

my( $key, $val ) = split(/=/,$line,2);
  
next if (!defined $val || $val =~ /^\s*$/);
$key =~ s/^\s+|\s+$//g;
$val =~ s/^\s+|\s+$//g;

if ($val=~/\$\{.+?\}/) {
  $val =~ s/\$\{(.+?)\}/$OPTS{$1}/g; #This is very interesting! :-D
}

$OPTS{$key}=$val;
  }
  close $FH;
}



Thank you all.


On Tue, 3 Nov 2015 19:34:20 -0800
"$Bill"  wrote:

> Re-sending with CC to OP since it didn't make the group.
> 
> On 11/3/2015 14:03, David Emanuel da Costa Santiago wrote:
> > -BEGIN PGP SIGNED MESSAGE-
> > Hash: SHA256
> >
> >
> > Hello all.
> >
> > I'm trying to interpolate a hash value in a string but i got stuck.
> > I already tried with eval, without any success...
> >
> >
> > I have two conf files in the form
> > conf file 1:
> > option=value
> > option2=value2
> >
> > conf file 2:
> > property=propertyValue
> > property2=propertyValue2
> >
> > I'm loading these files into a hash. But the problem is that i want
> > to access, for example on conf file 2, properties from conf file 1:
> >
> > property3=$HASH{option2}
> >
> >
> > So i'm getting the hash for conf file 2:
> >  
> > %PROPERTIES=(property=>"propertyValue",  property2=>
> > "propertyValue2", property3=> "$HASH{option2}" );  
> >
> > but what i want is:  
> > %PROPERTIES=(property=>"propertyValue",  property2=>
> > "propertyValue2", property3=> "value2" );  
> >
> >
> > this is how i'm reading the files:
> >
> > sub _read_conf_file{
> >open my $FH, '<', shift @_;
> >while(<$FH>){
> >  chomp;
> >  s/\#.*//;
> >  /(\S+)\s*=\s*(.+)/;
> >  next if (!defined $1 || !defined $2);
> >  $OPTS{$1}=$2;
> >}
> >close $FH;
> > }
> >
> > Does anybody knows how to do this?  
> 
> I would make s simple addition to your syntax to indicate the value
> is to be taken from the first hash using that value as key.  I
> arbitrarily picked '~' to indicate this condition (you could use
> whatever unique marker you want including $HASH and replace my '~'.
> I avoided trying to eval the $OPTS{} in file 2, but you could play
> with that using 2nd version below which accomplishes the same thing
> with eval.
> 
> use strict;
> use warnings;
> use Data::Dumper;
>$Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;
> 
> my $c1 = < option=value
> option2=value2
> EOD
> 
> my $c2 = <<'EOD'; # file 2
> property=propertyValue
> property2=propertyValue2
> property3=~option2# arbitrary ~ marker
> EOD
> 
> my %OPTS = ();
> 
> _read_conf_file ($c1);
> _read_conf_file ($c2);
> print (Data::Dumper->Dump([\%OPTS], [qw(%OPTS)]));
> exit;
> 
> sub _read_conf_file { # modified to handle from vrbls
> instead of files my $src = shift;
> 
> # open my $FH, '<', shift @_;
> # while (<$FH>) {
> foreach (split /\s*\n\s*/, $src) {
> 
>   next if /^\s*#.*/ or /^\s*$/;   # remove blank and
> comment lines
> 
>   if (not /(\S+)\s*=\s*(.+)/) {
>   print "Invalid config file syntax ($_)\n"; next;
>   }
> 
>   my $k = $1 // ''; my $v = $2 // '';
>   if (not $k) { print "Missing key ($_)\n"; next; }
> 
>   if ($v =~ /^~/) {   # if indirect hash assign
>   $OPTS{$k} = $OPTS{substr $v, 1};
>   } else {
>   $OPTS{$k} = $v;
>   }
> }
> # close $FH;
> 
> }
> 
> __END__
> 
> eval version:
> 
> foreach (split /\s*\n\s*/, $src) {
> 
>   next if /^\s*#.*/ or /^\s*$/;   # remove blank and
> comment lines
> 
>   if (not /(\S+)\s*=\s*(.+)/) {
>   print "Invalid config file syntax ($_)\n"; next;
>   }
> 
>   my $k = $1 // ''; my $v = $2 // '';
>   if (not $k) { print "Missing key ($_)\n"; next; }
> 
>   $v = eval $v if $v =~ /^\$/;
>   $OPTS{$k} = $v;
> }
> 

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJWOkstAAoJEJ/OLjwuDYzKg/AH/AiTYpPupOrV9fqKM8xKDO7Q
i/87w3ynwzB6md5y+zmKZqYvoN1r2NocVvvce3nQH9aSdMIwlRfPFyAPUBEaJpnL
Nddcg4N8YktMuo77Ptk6TlluG+Ok3pKPC7F9wyZ8Qh1+CWJLxj5N/KzpJ2Osei5W
E9ZzTyUgwz/KzU3GVIvUdSV2A51v7QeIhdPYmnqQKZwnfKQ3U9APxZ+a8AuYXHSb
3d+bnVidNpRSPvkpXgLqCHQwphpYspj2MhOg9cYAeOJxcidWiPxWP2cbL8g8EN6Z
z5wb4j1SP2iW6/vZkWC2INudRQWktE5FHo8tUJhrxwlRAKYmyVunv/YghTyOhnY=
=yX+d
-END PGP SIGNATURE-


How to interpolate a hash in a string

2015-11-03 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Hello all.

I'm trying to interpolate a hash value in a string but i got stuck.
I already tried with eval, without any success... 


I have two conf files in the form
conf file 1:
option=value
option2=value2

conf file 2:
property=propertyValue
property2=propertyValue2

I'm loading these files into a hash. But the problem is that i want to
access, for example on conf file 2, properties from conf file 1:

property3=$HASH{option2}


So i'm getting the hash for conf file 2:

%PROPERTIES=(property=>"propertyValue",  property2=> "propertyValue2",
property3=> "$HASH{option2}" );

but what i want is:
%PROPERTIES=(property=>"propertyValue",  property2=> "propertyValue2",
property3=> "value2" );


this is how i'm reading the files:

sub _read_conf_file{
  open my $FH, '<', shift @_;
  while(<$FH>){
chomp;
s/\#.*//;
/(\S+)\s*=\s*(.+)/;
next if (!defined $1 || !defined $2);
$OPTS{$1}=$2;
  }
  close $FH;
}

Does anybody knows how to do this?

Best regards,
David Santiago

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEbBAEBCAAGBQJWOS8dAAoJEJ/OLjwuDYzKFHUH9jIdB/XtN/ub0mJFCNFPdvVt
JVQnFKHpbzoWpiPXbsxuGTYRDg3QFZnyqqBgrkB+yJ82IK3Uc/aDxvr1dSog6jdP
59okJgbmvqMLvH9d8B7bWQLAoKZCQvQKRgeeZuSxuE7v7y8gIKvFUvYcR7vqUBau
zlcG20mE6zhKO6rBsJsb3blFPtYuf/FewzRwvKRkD8NHGLuPmrzXFepkYnynJz+k
/BhejiiJN4GMlbjLPlYIOrLKlCCKVrOx7XeUB13lnsHZ1jvMimtu4T9wxWRGfqaf
pJJrQA7weEJz/jLBAuEbRbsqhskHJGBfARxhQ3Ba4M410cQyVsmuVxjz6JOrlA==
=hl/V
-END PGP SIGNATURE-


Re: Sockets getting stuck

2015-10-13 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256



Hi Brandom.

On Tue, 13 Oct 2015 10:08:27 -0400
Brandon McCaig  wrote:

> On Tue, Oct 13, 2015 at 6:34 AM, David Emanuel da Costa Santiago
>  wrote:
> > Hi!  
> 
> Hello,
> 
> > THanks for your suggestion.  
> 
> I assume this was off-list because I didn't get it. Also consider
> bottom-posting or interleaved posting for easier reading.

Noted.

> 
> > I changed my code to:
> >
> > sub _read_from_socket{
> >   my ($select) = @_;
> >
> >   my ($output, $buffer) = ('', '');
> >   my $socket = undef;
> >   do {
> >   $socket = ($select->can_read(0.1))[0];
> > }  while (!defined $socket);  
> 
> You're essentially manually blocking your code here. Until you can
> read the program will be stuck in that loop, blocked/hung. It's no
> different than just reading without IO::Select (which probably wastes
> fewer CPU cycles too).

Thanks. I'll remove the select, and leave the read (it was how i had
it before).

> 
> In general it would depend on the protocol you're using. _Should_
> there always be data when you go to read or could the remote end have
> nothing to send you? You can try to rearrange your program to handle
> the case when there is no data. Instead of looping until can_read(),
> try printing or logging a diagnostic message and either moving on to
> other work, putting the program to sleep to try again after some
> period of time, or exit 1.
> 

> Perhaps your troubles aren't code and are instead network stability.
> If your network isn't stable enough to receive the packets then there
> isn't much you can do within software to fix it.
> 

Yes i also believe so. 


Thanks!

David Santiago
> 
> 

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJWHU0IAAoJEJ/OLjwuDYzKCdQH/AsSXpit4TB8y/zHtGJgqpvc
gkh8rsKGNBtNCfyHF8P30MbH2RUOc/MjoZ9Y6+LqkXpqHv7fyiAkCfAKZ2RRO9IX
QQPWbE8rjdXNbqaK60oj+TmUpkCASwKIGAl7cQsezG4YRdPoqibhzYnvycfWNbvt
2BrUkad4h5JXTqopt0ZjRV+O2ublCRbPNQWcGh6M63X0PEaXL/BsxkqaWAGscCwa
+0nXzG4e8EKXjsTgFpDyTrj+H4iUL1rp5tCA15f9WK/Ls/qff/YGbX6S2d0NdthM
iFh5mcN/4wQTDJYb28Lu27RVVO6Fsw4n2AAEbhL3LSLXHsrTPZtUFKrjFuqYrZo=
=J76w
-END PGP SIGNATURE-


Re: Sockets getting stuck

2015-10-13 Thread David Emanuel da Costa Santiago

Hi!

THanks for your suggestion. 

I changed my code to:

sub _read_from_socket{
  my ($select) = @_;

  my ($output, $buffer) = ('', '');
  my $socket = undef;
  do {
  $socket = ($select->can_read(0.1))[0];
}  while (!defined $socket);

  while(1){

my $status = $socket->read($buffer, 1);
die "Error: $!" if(!defined $status);
$output .= $buffer;
last if $output =~ /\r\n$|^\z/;
  }

  return $output;
}


Unfortunatelly i'm still getting connections "hanged". This time,
according to wireshark it's when there's a TCP retransmission of a
[FIN,ACK]:

Remote [FIN,ACK] -> Local
Local [FIN,ACK] -> Remote
Remote [ACK] -> Local
Remote [ACK] -> Local
Local [FIN, ACK] -> Remote (TCP Retransmission)
Local [FIN, ACK] -> Remote (TCP Retransmission)
Local [FIN, ACK] -> Remote (TCP Retransmission)
Remote [SYN, ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup Ack #5)
Local [FIN, ACK] -> Remote (TCP Retransmission)
Local [FIN, ACK] -> Remote (TCP Retransmission)
Local [FIN, ACK] -> Remote (TCP Retransmission)


No more exchanges.






On Mon, 12 Oct 2015 20:50:46 -0700
$Bill  wrote:

> This failed twice to post - emailing it.
> 
> On 10/12/2015 09:58, David Emanuel da Costa Santiago wrote:
> > -BEGIN PGP SIGNED MESSAGE-
> > Hash: SHA256
> >
> >
> > Hello all!
> >
> > I'm creating some sockets to a remote server and from time to time,
> > the sockets get "frozen".
> >
> > I'm creating the socket as:
> >
> >  $socket = IO::Socket::INET->new (
> >  PeerAddr => $server,
> >  PeerPort => $port,
> >  Blocking => 1,
> >  Proto => 'tcp',
> >  Timeout => 20, #connection
> > timeout ) or die "Error: Failed to
> >  connect : $!\n";
> >
> >
> > So far so good, but when i try to make a first read, the read hangs
> > (always on the first read - If the first read is successfull all the
> > other reads will succeed). No error whatsoever (the connection
> > doesn't even drop!).
> >
> > I'm reading the socket as:
> >
> > sub _read_from_socket{
> >my ($socket) = @_;
> >
> >my ($output, $buffer) = ('', '');
> >while(1){
> >  my $status = $socket->read($buffer, 1);
> >  die "Error: $!" if(!defined $status);
> >  $output .= $buffer;
> >  last if $output =~ /\r\n$|^\z/;
> >}
> >
> >return $output;
> > }
> >
> >
> > The same happens even with a sysread (both on windows and linux
> > platforms). This hanging doesn't happen everytime, it's like 1 in 6
> > tries.
> >
> > If i check with netstat i see the connection with state ESTABLISHED.
> >
> > Do you guys know what's going on?  
> 
> No, but you should be able to avoid the condition by:
> 1) Use non-blocking IO on your new; or
> 2) Use IO::Select can_read to test for data prior to reading; or
> 3) Use an ioctl to test for data or get size of data in input Q
> (FIONREAD); or 4) Use ioctl to reset blocking (FIONBIO) on socket.
> 
> I usually use 2) to do my socket IO.
> 

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




Re: Sockets getting stuck

2015-10-12 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256



Hello all, 

So... i fired up wireshark and i can see that the socket get in hanged
state when i get a first "TCP spurious retransmission".

This is how it goes:

Local [Syn] -> Remote
Local [Syn] -> Remote
Remote [SYN ACK] -> Local
Local [ACK] -> Remote
Remote [SYN ACK] -> Local
Local [ACK] -> Remote
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #1)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #1)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #2)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #2)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #3)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #3)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #4)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #4)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #5)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)
Local [ACK] -> Remote (TCP Dup ACK #5)
Remote [SYN ACK] -> Local (TCP Spurious Retransmission)

No more packets exchanged.
After this the socket simply gets in zombie state. Not dead, but not
alive.

The connection timeout doesn't work, the receive timeout (SO_RCVTIMEO)
and send timeout (SO_SNDTIMEO) does work.

Is there any way to recover from this?



On Mon, 12 Oct 2015 18:58:42 +0200
David Emanuel da Costa Santiago  wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
> 
> 
> Hello all!
> 
> I'm creating some sockets to a remote server and from time to time,
> the sockets get "frozen".
> 
> I'm creating the socket as:
> 
> $socket = IO::Socket::INET->new (
>PeerAddr => $server,
>PeerPort => $port,
>Blocking => 1,
>Proto => 'tcp',
>Timeout => 20, #connection
> timeout ) or die "Error: Failed to
> connect : $!\n";
> 
> 
> So far so good, but when i try to make a first read, the read hangs
> (always on the first read - If the first read is successfull all the
> other reads will succeed). No error whatsoever (the connection doesn't
> even drop!).
> 
> I'm reading the socket as: 
> 
> sub _read_from_socket{
>   my ($socket) = @_;
> 
>   my ($output, $buffer) = ('', '');
>   while(1){
> my $status = $socket->read($buffer, 1);
> die "Error: $!" if(!defined $status);
> $output .= $buffer;
> last if $output =~ /\r\n$|^\z/;
>   }
> 
>   return $output;
> }
> 
> 
> The same happens even with a sysread (both on windows and linux
> platforms). This hanging doesn't happen everytime, it's like 1 in 6
> tries.
> 
> If i check with netstat i see the connection with state ESTABLISHED.
> 
> Do you guys know what's going on? 
> 
> 
> 
> Regards,
> David Santiago
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v2
> 
> iQEcBAEBCAAGBQJWG+bCAAoJEJ/OLjwuDYzKX3wIAKT0pH1gdcUMxmgGrN6v5vbJ
> MVvNx85HJ9Z83LEe8hewsxVLLxcYVgVCIm/VAdRuDKREZmnL7xdraaHmu3a2wHzG
> /yNedRGs/CWJdZB0jt+o7Y3RbH9dFo9YMSpRugUHrQPbhe4fY5PbWws7RPleR2fv
> AXpKRd3wx4zxdzbrbVieXtLNCgqRvEEFJ9Jpq0SYXQ9Tv+E7BoJ4+u2Zx4jfQMfv
> 54YOx6lEMiMRLCUdDe+bft26J6OoYWZCk88Dk92VurCdvIsSXKsqFeB84B3B1lva
> J7/BJQdFsbzTsSy7egdbiQUKApRR1uhW6IeClEkhbFzrfpF/lKJGZXU022PrJTE=
> =TeGg
> -END PGP SIGNATURE-

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJWHDYfAAoJEJ/OLjwuDYzKHuUH/jeK+sXsCxZvx8WtEDFpbB1u
w2ouv2bxbg8mwH0AHfGjFBaJoGL2R8SoBPPhObfzvQMFV5ZD1XXmoc3qoUiJf3Ot
zYxdfkq7P0QX24sUa7Ky9HMkxIg75bVTrOK4Sw5xVrjoHK/Q7tWzdWquP+cWWeqA
eL+kM0Dnu42FkW2FqQzEcp8nLk0nUioQ9JJ4HpkaM/UN2NbK97jPBseEAr4nbMRi
BP/8R7w0096C2vrXjzzF/sDCgnHTSkagPPbTs3of+dGTXtTPS95C53d+7rG4qWJx
Lk7P6nnuotrK3MeRGd+Di7nWfyZ+/bpbqFvrRE1pyXdKqTj37EcNO2Su2sDJx00=
=PxuA
-END PGP SIGNATURE-


Sockets getting stuck

2015-10-12 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Hello all!

I'm creating some sockets to a remote server and from time to time, the
sockets get "frozen".

I'm creating the socket as:

$socket = IO::Socket::INET->new (
 PeerAddr => $server,
 PeerPort => $port,
 Blocking => 1,
 Proto => 'tcp',
 Timeout => 20, #connection timeout
) or die "Error: Failed to
connect : $!\n";


So far so good, but when i try to make a first read, the read hangs
(always on the first read - If the first read is successfull all the
other reads will succeed). No error whatsoever (the connection doesn't
even drop!).

I'm reading the socket as: 

sub _read_from_socket{
  my ($socket) = @_;

  my ($output, $buffer) = ('', '');
  while(1){
my $status = $socket->read($buffer, 1);
die "Error: $!" if(!defined $status);
$output .= $buffer;
last if $output =~ /\r\n$|^\z/;
  }

  return $output;
}


The same happens even with a sysread (both on windows and linux
platforms). This hanging doesn't happen everytime, it's like 1 in 6
tries.

If i check with netstat i see the connection with state ESTABLISHED.

Do you guys know what's going on? 



Regards,
David Santiago
-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJWG+bCAAoJEJ/OLjwuDYzKX3wIAKT0pH1gdcUMxmgGrN6v5vbJ
MVvNx85HJ9Z83LEe8hewsxVLLxcYVgVCIm/VAdRuDKREZmnL7xdraaHmu3a2wHzG
/yNedRGs/CWJdZB0jt+o7Y3RbH9dFo9YMSpRugUHrQPbhe4fY5PbWws7RPleR2fv
AXpKRd3wx4zxdzbrbVieXtLNCgqRvEEFJ9Jpq0SYXQ9Tv+E7BoJ4+u2Zx4jfQMfv
54YOx6lEMiMRLCUdDe+bft26J6OoYWZCk88Dk92VurCdvIsSXKsqFeB84B3B1lva
J7/BJQdFsbzTsSy7egdbiQUKApRR1uhW6IeClEkhbFzrfpF/lKJGZXU022PrJTE=
=TeGg
-END PGP SIGNATURE-


Re: Perl memory leaks (and valgrind)

2015-10-02 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256



Hi Shlomi

I just noticed that, if i merge yenc_encode_c_for_perl with
_yenc_encode_c into one function then valgrind stops reporting memory
leaks.

I need to do more tests though.

Thanks and best regards,
David Santiago


On Thu, 1 Oct 2015 22:16:58 +0200
David Emanuel da Costa Santiago  wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
> 
> 
> 
> Hi Shlomi,
> 
> Thanks for your reply. I tried your suggestion and it didn't work:
> 
> ==10175== LEAK SUMMARY:
> ==10175==definitely lost: 24,451 bytes in 37 blocks
> ==10175==indirectly lost: 76,082 bytes in 33 blocks
> ==10175==  possibly lost: 14,587,533 bytes in 52,651 blocks
> ==10175==still reachable: 111,892 bytes in 2,953 blocks
> ==10175== suppressed: 0 bytes in 0 blocks
> 
> 
> 
> What do you mean with "mortal" ? If you mean a temporary value then
> it's yes.
> 
> Regards,
> David Santiago
> 
> 
> 
> On Thu, 1 Oct 2015 21:26:59 +0300
> Shlomi Fish  wrote:
> 
> > On Thu, 1 Oct 2015 19:51:12 +0200
> > David Emanuel da Costa Santiago  wrote:
> > 
> > > -BEGIN PGP SIGNED MESSAGE-
> > > Hash: SHA256
> > > 
> > > 
> > > Now with the code attached.
> > > 
> > > Best regards,
> > > David Santiago
> > > 
> > 
> > Hi David!
> > 
> > One thing that strikes me as a problem is the fact that
> > _yenc_encode_c returns a string that was malloc()ed/realloc()ed and
> > I don't think that Inline::C will free() it for you. What you should
> > try to do is:
> > 
> > char* _yenc_encode_c(unsigned char* data, size_t data_size)
> > {
> > .
> > .
> > .
> > }
> > 
> > SV* yenc_encode_c_for_perl(unsigned char* data, size_t data_size)
> > {
> > SV * ret;
> > char * s;
> > s = _yenc_encode_c(data, data_size);
> > 
> > ret = newSVpv(s, 0);
> > free(s);
> > return ret;
> > }
> > »
> > 
> > And then use yenc_encode_c_for_perl() in your Perl program.
> > 
> > Regards,
> > 
> > Shlomi Fish
> > 
> > P.S: does the «SV * ret» need to be made mortal?
> > 
> 
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v2
> 
> iQEcBAEBCAAGBQJWDZS6AAoJEJ/OLjwuDYzK1bEIAK2i5tQfZkEAFoRqCLZ93IuH
> Ya0QVgAs/obdBHWLcGv5lQlVc3RgdFX1/2Nl7sJV3Waiqos9921IjQdbw97z4M99
> OMVMI1UabjWrvhRHUc7IIhp6fJq4bRvKmiDF5j2BaRlrX101iszrmESHVllAE6iD
> ava+WtJbIsfG168p/QB5Y9vdvCypXx4ZIPUkZf3YBzRm8z7w+Xjh6u1GdZuGZdpi
> Vadm2t1AFgcuKjNpvSoRTeH+hMuphiPaxEVqmFLGpapM5KJp2YckLKMGH9cUsL7d
> qe+iEvXVIXoMLR9qGPNLVvgb1P299WQ3yRTcIpMK3r4PsSQtAbv9JtAWa+Msi9U=
> =9Vxg
> -END PGP SIGNATURE-

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJWDuPnAAoJEJ/OLjwuDYzK3PwIAIvlqyje9yKOqG+veIkpgq+H
xqB47KnDD9CEtZhptSW7/GKWzt9FDBhL1ED+Puy1XrB0xzRNP4D0jGdv57l2MFbz
0zUwkIl7gCWPYnn2hVcumv7U2esyvrTc//is35xj9xWmvqa2DDvHOmcBkOjpvcWB
bhzjYkyZZcqa6zj73X5z6m7ln/FE4/SKNtl7DQp5dDQeCVDH+eHjl+ddA3ofr56a
NQMAoqJveNxlgV/BcsQV8Fto0zPbD3UU7+pKFSB6AJyFNTDW9OPacws3azxzuQGV
7CE9JxkKNo85XmF2saQyN70c0wEiqpiJiCETUqxX8qE2TjkYmoT3669D/DDZGI8=
=da4X
-END PGP SIGNATURE-


Re: Perl memory leaks (and valgrind)

2015-10-01 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Now with the code attached.

Best regards,
David Santiago


On Thu, 1 Oct 2015 19:19:47 +0200
David Emanuel da Costa Santiago  wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
> 
> 
> Hello all,
> 
> Is the valgrind tool a reliable tool for checking for memory leaks in
> perl and in our perl scripts?
> 
> I have a perl script that's consuming all the memory available on my
> system. I rechecked the script and since i didn't found anything
> (algorithm wise) that can cause such memory consuption, I decided to
> use valgrind. 
> 
> And valgrind is reporting a memory leak. :-(
> 
> I'm running it as:
> 
> valgrind --log-file=valgrind --trace-children=yes -v
> - --show-leak-kinds=all --track-origins=yes --leak-check=full
> perl /path/to/perl/script.pl
> 
> 
> and i'm getting the output:
> ==25483== LEAK SUMMARY:
> ==25483==definitely lost: 23,347 bytes in 36 blocks
> ==25483==indirectly lost: 76,082 bytes in 33 blocks
> ==25483==  possibly lost: 14,593,429 bytes in 52,715 blocks
> ==25483==still reachable: 111,559 bytes in 2,948 blocks
> ==25483== suppressed: 0 bytes in 0 blocks
> 
> 
> When i look into the log file i see a lot of entries like:
> 
> 
> ==25498== 2 bytes in 1 blocks are possibly lost in loss record 1 of
> 5,588 
> ==25498==at 0x4C29E6F: malloc
> (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
> ==25498==
> by 0x4EE1051: Perl_safesysmalloc
> (in /usr/lib/perl5/core_perl/CORE/libperl.so) 
> ==25498==by
> 0x4EE13E6: Perl_savepvn (in /usr/lib/perl5/core_perl/CORE/libperl.so)
> ==25498==by 0x4F16B38: Perl_sv_magicext
> (in /usr/lib/perl5/core_perl/CORE/libperl.so) 
> ==25498==by
> 0x4F1A38A: Perl_sv_magic (in /usr/lib/perl5/core_perl/CORE/libperl.so)
> ==25498==by 0x4E8F85E: Perl_gv_fetchpvn_flags
> (in /usr/lib/perl5/core_perl/CORE/libperl.so) 
> ==25498==by
> 0x4E89E2B: perl_parse (in /usr/lib/perl5/core_perl/CORE/libperl.so)
> ==25498==by 0x400D9A: main (in /usr/bin/perl)
> 
> 
> (i also get entries: "Use of uninitialised value")
> 
> I even tried to set the environment variables PERL_DESTRUCT_LEVEL,
> PERL_DEBUG, PERL_VALGRIND as described in
> http://www.perlmonks.org/?node_id=550371 
> Without any success.
> 
> Did someone encounter this kind of issue in the past? If yes, how it
> was solved? 
> 
> 
> My perl version is 5.22.0.
> 
> 
> Thank you and best regards,
> David Santiago
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v2
> 
> iQEcBAEBCAAGBQJWDWszAAoJEJ/OLjwuDYzK3y0H/0bJoCx6o6xdjTb3DUuoLQIp
> Gyt/I7URulDEzKY9KAPWiTOECWiC12ZbqYg/dNID/p+Ap3UhOYnHTjIqs7M6Cmaz
> RpzFU1nF/o8gpvkC3Rjr7iLsjtZuNIbMJvsH7Wp447qxD73JDtJdHGcS82a2wy3A
> aIzy6sfpFesHPHAZNmitRgWReEtwHHBuKdxJyGOgkIGm6pIQFo+h7GMJmbdIsG+R
> zBIOUIczMo8lRf9VbbhoYtgIZlhr1PQhG2LYHwOMeD2CXDj9vwNS47WagCv0fSv+
> a0ZR4uj8+q9HAoTUkbdaTZfbkMzi0p3Hd0Sgxw0BtjZ48uwtjZ+qkk7h8KKk5I0=
> =rI+0
> -END PGP SIGNATURE-

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJWDXKQAAoJEJ/OLjwuDYzK5tIIAJPd2ziiuEY6WdNbRoe2MUXl
mgG5XlaCJIgB12VVATi0AE6rCLVeXZnvdtga+3lYHxPhGZHymWR2vmW6mjf5/clZ
1wHe7etgtB/16nI5E8qHIuDI6zeWv6GFTW59aP4z+3SqH8ubGVvFFiYdTMewJlIt
p5qxreOkrzBjEvI/j47V4FcnVOdpNvXz66mVDtroxgRu65YL21vUBIUZNwRChtKP
w0sST1mNv5D2k2Srdgm8m/pFYysGZdR8C2B5Ia7GCNOJlg82boyTKGWQYJ9KyUp3
fMi2IFzAWkE2R2/IATS9uHI0dCLoZhHNDHSaNO/+fczAMO89Pt+JSU8/Uyy3Kv8=
=KAN6
-END PGP SIGNATURE-


newsup.pl.tar.bz2
Description: application/bzip
-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Perl memory leaks (and valgrind)

2015-10-01 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Hello all,

Is the valgrind tool a reliable tool for checking for memory leaks in
perl and in our perl scripts?

I have a perl script that's consuming all the memory available on my
system. I rechecked the script and since i didn't found anything
(algorithm wise) that can cause such memory consuption, I decided to
use valgrind. 

And valgrind is reporting a memory leak. :-(

I'm running it as:

valgrind --log-file=valgrind --trace-children=yes -v
- --show-leak-kinds=all --track-origins=yes --leak-check=full
perl /path/to/perl/script.pl


and i'm getting the output:
==25483== LEAK SUMMARY:
==25483==definitely lost: 23,347 bytes in 36 blocks
==25483==indirectly lost: 76,082 bytes in 33 blocks
==25483==  possibly lost: 14,593,429 bytes in 52,715 blocks
==25483==still reachable: 111,559 bytes in 2,948 blocks
==25483== suppressed: 0 bytes in 0 blocks


When i look into the log file i see a lot of entries like:


==25498== 2 bytes in 1 blocks are possibly lost in loss record 1 of
5,588 
==25498==at 0x4C29E6F: malloc
(in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==25498==
by 0x4EE1051: Perl_safesysmalloc
(in /usr/lib/perl5/core_perl/CORE/libperl.so) 
==25498==by
0x4EE13E6: Perl_savepvn (in /usr/lib/perl5/core_perl/CORE/libperl.so)
==25498==by 0x4F16B38: Perl_sv_magicext
(in /usr/lib/perl5/core_perl/CORE/libperl.so) 
==25498==by
0x4F1A38A: Perl_sv_magic (in /usr/lib/perl5/core_perl/CORE/libperl.so)
==25498==by 0x4E8F85E: Perl_gv_fetchpvn_flags
(in /usr/lib/perl5/core_perl/CORE/libperl.so) 
==25498==by
0x4E89E2B: perl_parse (in /usr/lib/perl5/core_perl/CORE/libperl.so)
==25498==by 0x400D9A: main (in /usr/bin/perl)


(i also get entries: "Use of uninitialised value")

I even tried to set the environment variables PERL_DESTRUCT_LEVEL,
PERL_DEBUG, PERL_VALGRIND as described in
http://www.perlmonks.org/?node_id=550371 
Without any success.

Did someone encounter this kind of issue in the past? If yes, how it
was solved? 


My perl version is 5.22.0.


Thank you and best regards,
David Santiago
-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJWDWszAAoJEJ/OLjwuDYzK3y0H/0bJoCx6o6xdjTb3DUuoLQIp
Gyt/I7URulDEzKY9KAPWiTOECWiC12ZbqYg/dNID/p+Ap3UhOYnHTjIqs7M6Cmaz
RpzFU1nF/o8gpvkC3Rjr7iLsjtZuNIbMJvsH7Wp447qxD73JDtJdHGcS82a2wy3A
aIzy6sfpFesHPHAZNmitRgWReEtwHHBuKdxJyGOgkIGm6pIQFo+h7GMJmbdIsG+R
zBIOUIczMo8lRf9VbbhoYtgIZlhr1PQhG2LYHwOMeD2CXDj9vwNS47WagCv0fSv+
a0ZR4uj8+q9HAoTUkbdaTZfbkMzi0p3Hd0Sgxw0BtjZ48uwtjZ+qkk7h8KKk5I0=
=rI+0
-END PGP SIGNATURE-


Re: reading from socket

2015-09-17 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256



Hello Chris.

Can you provide the yenc files?
Both the good one and the bad one?

Thx,
Regards,
David Santiago


On Thu, 17 Sep 2015 23:19:22 +0200
"Chris Knipe"  wrote:

> Just an example, and no, the decoder isn't written by me, it's from
> the official yEnc web site (www.yenc.org), so I'm 100% it's right.
> 
> Both files are encoded and uploaded by the same piece of software
> (again, not written by me), and again, I'm 100% that software is
> correct as well, it's used by thousands of people on a daily basis... 
> 
> Both these messages, are sent through the EXACT code, as it's
> below... 
> 
> This works absolutely fine:
> Trigger: =ybegin part=1 line=128 size=76 name=New Text Document.txt
> DesFileName = (New Text Document.txt)
> yDecoder started...
> =ypart-line:  =ypart begin=1 end=76
> part-begin: 1
> part-end  : 76
> Last line.
> Endline (76 bytes): =yend size=76 part=1 pcrc32=f00625ae
> Included CRC: $f00625ae - calculated CRC: $f00625ae
> Write part to target file (start: 0, size: 76)
> yDecode successful
> AddPart (New Text Document.txt(76).dec) [1 - 76]
> Complete multipart --> New Text Document.txt
> Decoded multipart --> New Text Document.txt
> 
> 
> This fails:
> Trigger: =ybegin part=1 line=128 size=77859 name=Capture.PNG
> DesFileName = (Capture.PNG)
> yDecoder started...
> =ypart-line:  =ypart begin=1 end=77859
> part-begin: 1
> part-end  : 77859
> Last line.
> Endline (77862 bytes): =yend size=77859 part=1 pcrc32=083aa26d
> Partial message corrupt: longer than (end-begin)!
> yDecode failed (reason: 11)
> 
> Note that the failed file has 3 extra bytes... It should end at
> 77859, but instead, it ends at 77862.  So where does those extra
> three bytes come from?
> 
> *completely, lost*
> 
> 
> 
> 
> 
> -Original Message-
> From: Chris Knipe [mailto:sav...@savage.za.org] 
> Sent: Thursday, September 17, 2015 10:56 PM
> To: 'beginners@perl.org' 
> Subject: RE: reading from socket
> 
> Hi All,
> 
> I'm SERIOUSLY starting to cry here :-(  It's been over a month since
> I started this thread, and it's pretty much been close to a year of
> me battling to get this right with perl, and yes - still not there... 
> 
> Running under Net::Server, I have:
> 
> sub process_request {
> while () {
>   $_ =~ s/\r?\n$//; # Remove any instance of \r or \n
> from the end of the received line $_ =~ s/\t//; # Remove
> any instance of \t from the received line $_ =~ s/[^[:ascii:]]//g;
>   my @Command = split(/\ /, $_);
>   switch($Command['0']) {
> 
> case m/^\bPOST\b/i {
>   print $sock "340 Ok, send message\r\n";
> 
> my $filename = '/tmp/test.txt';
> open(OUTFILE, ">", $filename);
> binmode(OUTFILE);
> binmode(STDIN);
> my $size=0;
>   while (read(*STDIN, local $_, 4))
> { $self->log(0,Dumper($_)); print OUTFILE $_; $size=$size+length($_);
> if ($_ =~ /^\.\r\n$/) { last;
> }
>   }
> close(OUTFILE);
>   print "240 article received\r\n";
> $self->log(0,"received: " . $size);
> }
>   }
> }
>   }
> }
> 
> 
> 1) From the client, I can confirm via Wireshark, all data has been
> transmitted (correctly, and successfully), 2) On the server, I can
> confirm via tcpdump, all the data has been received (correctly, and
> successfully), 3) In perl, I can confirm the entire message has been
> received in sequence (whether it's correct or not, I do not know),
> 
> Certain messages that are sent, decodes correctly (a manual decode of
> the file that has been written.  Other messages that are sent, does
> not decode correctly (but the data has been transmitted and received
> correctly).  I am able to reproduce, and have data sets of data that
> works, and data that doesn't work.  The problem is that after the
> file is saved to disk, certain files can decode successfully, whilst
> others, does not decode correctly.
> 
> Can someone *PLEASE* just help me to get this sorted out?  I am even
> willing to go off list and pay someone to fix this for me.  It's now
> a serious, serious issue that I need to get resolved asap, and more
> than likely either something very trivial that I'm overlooking, or a
> bug somewhere in perl / Net::Server.
> 
> Yes, this is still yEnc related binary data...  And a gazillion
> things has been tried already (a lot coming from this mailing list). 
> 
> I fail (really, I do), to see why SOME yEnc messages that is saved
> decodes

Re: Yenc encoder performance

2015-07-31 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256



Hello.


Thanks for your reply.


I remember that i did some performance tests and 

$string = $string ."something"

had better performance than 

$string .= "something"

which matched the results of (link to stack overflow)
http://stackoverflow.com/questions/3104493/performance-with-perl-strings


I'm going to try your suggestions an i will report back.



Best regards,
David Santiago




Em Thu, 30 Jul 2015 16:56:31 -0400
Uri Guttman  escreveu:

> On 07/30/2015 04:14 PM, David Emanuel da Costa Santiago wrote:
> >
> > my @YENC_CHAR_MAP = map{($_+42)%256;} (0..0x);
> > my $YENC_NNTP_LINESIZE=128;
> >
> > sub _yenc_encode{
> >my ($string) = @_;
> >my $column = 0;
> >my $content = '';
> >
> >my @hexString = unpack('W*',$string); #Converts binary string to
> > hex 
> >for my $hexChar (@hexString) {
> >  my $char= $YENC_CHAR_MAP[$hexChar];
> >
> when checking multiple values like this use a hash. it will be
> quicker than all those == ops. or even a array using ord() for the
> index. you can populate it outside the sub like this:
> 
> my @is_special ;
> $is_special[0] = 1 ;# NUL
> $is_special[10] = 1 ;#LF
> > #null || LF || CR || =
> >  if ($char == 0 || $char == 10 || $char == 13 || $char == 61 ||
> >
> > # TAB || SPC
> > (($char == 9 || $char == 32) && ($column ==
> > $YENC_NNTP_LINESIZE || $column==0)) ||
> >
> > ($char==46 && $column==0) # .
> then a single check will do:
> 
>  if ( $is_special[$char] && blah blah ) {
> 
> 
> > ) {
> >
> >$content =$content. '=';
> ever heard of .=? it is one of my favorite perl ops. it just means 
> append. building strings is best done with .=. in fact a rule i use
> is for one sub to build some result string but its caller decides
> what to do with it. that way you can print, log, email, whatever to
> the string and not change the builder code. output should be done
> only when you have the full result as then you can decide what to do
> with the result. too often you see code where text is generated and
> output immediately. when they need to fork the output they have to
> resort to tied handles and other wacky stuff. the rule to remember is
> 
>   print rarely, print late.
> >$column+=1;
> >
>  $column++ ;
> >$char=($char + 64);#%256;
> you used += before so why not there too?
> 
> also use more horizontal white space to make your code easier to read.
> >  }
> >
> >  $content = $content.chr $char;
> like that looks like a .method call but perl doesn't use that
> syntax. .= is the win here again.
> >  
> >  $column+=1;
> >  
> >  if ($column>= $YENC_NNTP_LINESIZE ) {
> you can merge thos two lines:
>  if( ++$column >= $YENC_NNTP_LINESIZE ) {
> >$column=0;
> >$content = $content."\r\n";
> .= again. see how common its usage is? because it is a assignment op 
> which some newbies don't learn early on, it is missed. it is just
> append and simple and very needed in almost all code that builds up
> text.
> 
> uri
> 

-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJVu158AAoJEJ/OLjwuDYzKOoYH/2SAEQJu2q0j6fcGywhKV+kA
KGOWgtVSCgwiTQyVDqhrOI2EJr2PSW/7/iwVB7pMdVI1OrFsJyH+q0YoGbWMV9Mf
sl6nONanVGobdF/B1liDtdGchr6UtoO2QK5Elz2PWjtHiC2zgQEw2qYw+p72EJtW
4dXA7QdmtXyiABby3WcZr1mZ1D8SbqUtiJ48DWjcR1hA7mAcz8Erda9hm8jT+L5J
yiviE1gieA6BUXT2Ri6VzqX7/ScUO0JVTpHkqu2IGIzaXuqqV7kkOuihCwhN30EB
vV1Dt/WFcsdD/c3KFB4il/HBZxPfiY5/gHDTcsOn4fkqj6xy+qFs0p9vpIwvux0=
=SGdA
-END PGP SIGNATURE-


Re: Yenc encoder performance

2015-07-30 Thread David Araujo Souza
Hi Shiome.I'm the very, very beginner in Perl. In moment i can't help you. 
I'm iniciate study in Perl on this week.
Sorry. 

 


 Em Quinta-feira, 30 de Julho de 2015 18:27, Shlomi Fish 
 escreveu:
   

 Hi David,

see below for my comments on your code.

On Thu, 30 Jul 2015 22:14:55 +0200
David Emanuel da Costa Santiago  wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA256
> 
> 
> Hello,
> 
> I'm developing a perl yenc encoder but unfortunatelly it's not having
> the performance i was expecting to have (and it's CPU heavy).
> 
> Can i get some help improving it's performance?
> 
> This is the code i have (41 lines).
> 
> Initially i though about memoize it, but unfortunately i don't think
> it's possible.
> 
> Replacing the for loop with a map, would be a good option? Does anybody
> has any idea to improve it's performance?
> 
> 
>  
> Regards,
> David Santiago
> 
> 
> CODE: The following function receives a binary string.
> 
> 
> 
> my @YENC_CHAR_MAP = map{($_+42)%256;} (0..0x);

$X % 256 can also be written as ($X & 0xFF) (and should be a bit faster). Also
note that you can easily use (@LIST) x $N to repeat it here.

> my $YENC_NNTP_LINESIZE=128;
> 
> sub _yenc_encode{
>  my ($string) = @_;
>  my $column = 0;
>  my $content = '';
> 
>  my @hexString = unpack('W*',$string); #Converts binary string to hex
>  
>  for my $hexChar (@hexString) {

Since @hexString is used only once, you can put it inside the for :

    for my $hexChar (unpack('W*',$string)) {

>    my $char= $YENC_CHAR_MAP[$hexChar];
> 
>     #null || LF || CR || =
>    if ($char == 0 || $char == 10 || $char == 13 || $char == 61 ||
> 

Since you have many $char == $X || $char == $Y , you can convert it to a hash
or in your case using perldoc -f vec may do the trick:

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

>     # TAB || SPC
>     (($char == 9 || $char == 32) && ($column == $YENC_NNTP_LINESIZE
>     || $column==0)) || 
> 
>      ($char==46 && $column==0) # . 
>     ) {
>      
>      $content =$content. '=';

This can be written as:

    $content .= '=';

>      $column+=1;

    $column++;

>      
>      $char=($char + 64);#%256;

    $char += 64;

>    }
> 
>    $content = $content.chr $char;

    $content .= chr($char);

>    
>    $column+=1;

    $column++;

>    
>    if ($column>= $YENC_NNTP_LINESIZE ) {
>      $column=0;
>      $content = $content."\r\n";

    $content .= "\r\n";

And note that \r\n is not very portable:

>    }
> 
>  }
> 
>  return $content;
> }
> 

Finally, note that you may opt to write such speed-critical code in C or C++ and
bind it to Perl using XS or Inline::C :

* https://metacpan.org/pod/distribution/Inline-C/lib/Inline/C.pod

Regards,

    Shlomi Fish

-- 
-
Shlomi Fish      http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang

Chuck Norris can only convince you that you're deceased. Summer Glau can
also convince you that you're alive, which is much harder.
    — http://www.shlomifish.org/humour/bits/facts/Summer-Glau/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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



  

Yenc encoder performance

2015-07-30 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Hello,

I'm developing a perl yenc encoder but unfortunatelly it's not having
the performance i was expecting to have (and it's CPU heavy).

Can i get some help improving it's performance?

This is the code i have (41 lines).

Initially i though about memoize it, but unfortunately i don't think
it's possible.

Replacing the for loop with a map, would be a good option? Does anybody
has any idea to improve it's performance?


 
Regards,
David Santiago


CODE: The following function receives a binary string.



my @YENC_CHAR_MAP = map{($_+42)%256;} (0..0x);
my $YENC_NNTP_LINESIZE=128;

sub _yenc_encode{
  my ($string) = @_;
  my $column = 0;
  my $content = '';

  my @hexString = unpack('W*',$string); #Converts binary string to hex
 
  for my $hexChar (@hexString) {
my $char= $YENC_CHAR_MAP[$hexChar];

#null || LF || CR || =
if ($char == 0 || $char == 10 || $char == 13 || $char == 61 ||

# TAB || SPC
(($char == 9 || $char == 32) && ($column == $YENC_NNTP_LINESIZE
|| $column==0)) || 

($char==46 && $column==0) # . 
) {
  
  $content =$content. '=';
  $column+=1;
  
  $char=($char + 64);#%256;
}

$content = $content.chr $char;

$column+=1;

if ($column>= $YENC_NNTP_LINESIZE ) {
  $column=0;
  $content = $content."\r\n";
}

  }

  return $content;
}



-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQEcBAEBCAAGBQJVuoW/AAoJEJ/OLjwuDYzKvMcH/2Q2B1p3L+Q/iPvjfeTNoQfX
V+MsLkSelzjl/rKKRnYgYerHacaTpWxy6sKKnSlTQy2c2XXIXOLLxKZxHjw869bA
hwnlKrl2UnABekJF270J7wIk0K6+zw1BTjHjcPlibfBPTCX6lOoaO0PHy5cHycXC
XQ3+Pjo3e7Ux7dx16vFJq/XJl70LmV5CFShvQoLRtSV3fxvOEE25uGzRm6zCEVSd
cEISGgLXBHwzvpU5+ma4SIuXDcYWDpfNOUukPF7zLHtn+WEjr/CImcM75MvnjUtg
CZn9SIwgOeeNZ22T5SbOitX5uhqyFGOln8Y8DOcHjTCKTD1uem//IclcqZUrXBU=
=r+/T
-END PGP SIGNATURE-


Re: Should I add ExtUtils::MakeMaker as prerequisite to Makefile?

2015-02-27 Thread David Precious
On Fri, 27 Feb 2015 23:41:22 +0100
Alex Becker  wrote:
> So my idea was to specify EUMM v6.46 as minimum in PREREQ_PM. This
> way, I would not have to do all this 'if the EUMM version is greater
> than X then add Y to Makefile.PL' stuff.
>
> So, would it work? Does it make sense?

I don't think so; I think older EU::MM versions would choke on the
options they don't support before they'd get as far as checking
PREREQ_PM and seeing that you've asked for a newer EU::MM version.

You could always try it and see, but I don't think it'd work for you.



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




Re: is faster a regexp with multiple choices or a single one with lower case?

2015-01-08 Thread David Precious
On Wed, 7 Jan 2015 10:59:07 +0200
Shlomi Fish  wrote:
> Anyway, one can use the Benchmark.pm module to determine which
> alternative is faster, but I suspect their speeds are not going to be
> that much different. See:
> 
> http://perl-begin.org/topics/optimising-and-profiling/
> 
> (Note: perl-begin.org is a site I originated and maintain).

And this is the answer I'd give - if you're curious as to which of two
approaches will be faster, benchmark it and find out.  It's often
better to do this yourself, as the results may in some cases vary widely
depending on the system you're running it on, the perl version, how
Perl was built, etc.

The sure-fire way to see which of multiple options is faster is to use
Benchmark.pm to try them and find out :)

For an example, I used the following (dirty) short script to set up
1,000 test filenames with random lengths and capitalisation, half of
which should match the pattern, and testing each approach against all
of those test filenames, 10,000 times:


[davidp@supernova:~]$ cat tmp/benchmark_lc.pl 
#!/usr/bin/perl

use strict;
use Benchmark;

# Put together an array of various test strings, with random
# lengths and case
my @valid_chars = ('a'..'z', 'A'..'Z');
my @test_data = map { 
join('', map { $valid_chars[int rand @valid_chars] } 1..rand(10))
. (rand > 0.5 ? '.bat' : '.bar')
} (1..1000);

Benchmark::cmpthese(10_000,
{
lc_first => sub {
for my $string (@test_data) {
$string = lc $string;
if ($string =~ /\.bat$/) {
}
}
},
regex_nocase => sub {
for my $string (@test_data) {
if ($string =~ /\.bat$/i) {
}
}
},
},
);




And my results suggest that, for me, using lc() on the string first
before attempting to match was around 30% faster:


[davidp@supernova:~]$ perl tmp/benchmark_lc.pl 
   Rate regex_nocase lc_first
regex_nocase 2674/s   -- -24%
lc_first 3509/s  31%   --


Of course, YMMV.



-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: Anonymous functions and safe compartments

2014-12-28 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Hello Brandon.

Thanks for your reply.

> I think that the first part that you're having trouble with is
> sharing data (or code) with the "compartment". I think that if
> you want for $anon_func to be available to the Safe compartment,
> and therefore the untrusted_script.pl when executed with the Safe
> compartment, you need to pass the name of the anonymous function
> variable instead of the variable itself.

Yes you're right.

> It also appears that you cannot share lexical variables with
> these compartments (see perldoc Safe) so you need to make
> $anon_func a package variable (or copy it to one). 

Once again you're right.

I was able to fix my example: 

### Example.pl
use strict;
use warnings;
use Safe;
use 5.020;

my $UNTRUSTED_SCRIPT="untrusted_script.pl";


sub run_me{

  my $sandbox = Safe->new("Sandbox");
  $sandbox->permit(qw(:base_core :base_loop :base_mem :base_io :load 
:base_orig));

  our $operation = "Dangerous";
  sub anon_func{
my $param = shift;
print "$operation $param operation!\n";
  }

  $sandbox->share('&anon_func');
  $sandbox->rdo($UNTRUSTED_SCRIPT);
  if ($@) {
say "Erro! $@";
  }
  $sandbox->reval('func1()');
}

run_me();


### untrusted_script.pl
use 5.020;
use warnings;

sub func1{
  say "About to run shared function!";
  anon_func("func1");
}

##

The output is what i was expecting:
$ perl Example.pl 
About to run shared function!
Dangerous func1 operation!


Thanks Brandon for your help.

Best regards,
David Santiago



Em Sun, 28 Dec 2014 16:35:50 -0500
Brandon McCaig  escreveu:

> David:
> 
> On Sun, Dec 28, 2014 at 07:35:14PM +0100, David Emanuel da Costa
> Santiago wrote:
> > Hello!
> 
> Hello,
> 
> > How do i run and pass arguments to an anonymous subroutine that i
> > shared on a safe compartment?
> > 
> > My goal is to have a function that perform "dangerous" operations,
> > that must be available to untrusted code, however this function
> > needs some parameters that are only available when the code is
> > executing, and the untrusted code doesn't know them (hence the
> > anonymous subroutine).
> > 
> > Example (35 lines):
> > 
> > ### Example.pl file
> > use strict;
> > use warnings;
> > use Safe;
> > use 5.020;
> > 
> > my $UNTRUSTED_SCRIPT="untrusted_script.pl";
> > 
> > sub run_me{
> > 
> >   my $sandbox = Safe->new("Sandbox");
> >   $sandbox->permit(qw(:base_core :base_loop :base_mem :base_io :load 
> > :base_orig));
> >   $sandbox->deny(qw(die exit));
> > 
> >   my $operation = "Dangerous";
> >   
> >   my $anon_func = sub{
> > my $param = shift;
> > print "$operation $param operation!\n";
> >   };
> > 
> >   $sandbox->share($anon_func);
> >   $sandbox->rdo($UNTRUSTED_SCRIPT);
> >   $sandbox->reval('func1()');
> > }
> > 
> > run_me();
> > 
> > ### untrusted_script.pl
> > use 5.020;
> > use warnings;
> > use utf8;
> > 
> > 
> > sub func1{
> >   say "About to run shared function!";
> >   $anon_func->("func1");
> > }
> > 
> > ##
> > 
> > The output of the Example.pl script is:
> > 
> > Error while executing the sandbox: Global symbol "$anon_func"
> > requires explicit package name at untrusted_script.pl line 5. 
> > 
> > I was expecting of the output of the Example.pl script to be
> > "Dangerous func1 operation!". What am i doing wrong?
> 
> I am not familiar with Safe.pm so I don't know anything about
> that, but $anon_func is a lexically scoped variable in
> Example.pl. That variable is not available within the
> untrusted_script.pl file. I also don't see a subroutine or
> anything at all named "func1" defined so I can't imagine how
> $sandbox->reval('func1()') is supposed to work.
> 
> I think that the first part that you're having trouble with is
> sharing data (or code) with the "compartment". I think that if
> you want for $anon_func to be available to the Safe compartment,
> and therefore the untrusted_script.pl when executed with the Safe
> compartment, you need to pass the name of the anonymous function
> variable instead of the variable itself.
> 
> $sandbox->share('$anon_func');
> 
> It also appears that you cannot share lexical variables with
> these compartments (see perldoc Safe)

Anonymous functions and safe compartments

2014-12-28 Thread David Emanuel da Costa Santiago
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256


Hello!


How do i run and pass arguments to an anonymous subroutine that i shared
on a safe compartment?

My goal is to have a function that perform "dangerous" operations, that
must be available to untrusted code, however this function needs some
parameters that are only available when the code is executing, and the
untrusted code doesn't know them (hence the anonymous subroutine).

Example (35 lines):

### Example.pl file
use strict;
use warnings;
use Safe;
use 5.020;

my $UNTRUSTED_SCRIPT="untrusted_script.pl";

sub run_me{

  my $sandbox = Safe->new("Sandbox");
  $sandbox->permit(qw(:base_core :base_loop :base_mem :base_io :load 
:base_orig));
  $sandbox->deny(qw(die exit));

  my $operation = "Dangerous";
  
  my $anon_func = sub{
my $param = shift;
print "$operation $param operation!\n";
  };

  $sandbox->share($anon_func);
  $sandbox->rdo($UNTRUSTED_SCRIPT);
  $sandbox->reval('func1()');
}

run_me();

### untrusted_script.pl
use 5.020;
use warnings;
use utf8;


sub func1{
  say "About to run shared function!";
  $anon_func->("func1");
}

##

The output of the Example.pl script is:

Error while executing the sandbox: Global symbol "$anon_func" requires
explicit package name at untrusted_script.pl line 5. 

I was expecting of the output of the Example.pl script to be
"Dangerous func1 operation!". What am i doing wrong?


Best regards,
David Santiago
-BEGIN PGP SIGNATURE-
Version: GnuPG v2

iQIcBAEBCAAGBQJUoE1jAAoJEP84J6veUQ8lC1kQAIohVyMk3XSru1SP+fP1z+EE
YhhvERKKDCM4ccZ53qVlcc0t8XurOIAStjh2h0MTdn9ntwF9I9mAYFeINIPoac4h
yyb0eqD8Qxr18HTmQ6Q+eikLR6opO7Q7f6gzBqXnhf0rrk2FhU8BEWnZRkmOUIsN
b1ZRRKDiVFdD82usAsrNqatmO2/SCBufMF7pA7jClRlP8us8tHVm0eZTlTdMNE+A
mcxNSmpP2ExiEcSZKrDGlagiZqKsbD2d93pBuau0k6cvQHUugrQ/JVTVNBqm9mey
yDXzcZNuzhD60N9i5ubquzUFFfD6gNQWN/fzLMOCxj0DlxuBHfQlygEzhMbJl/HQ
dsEl7POmZLdkmJ6Xp7k8GjEAoBYPAycNHje11NcITZmiKk1yBg/il+yBE44e+o35
klrAGJvQAPw+ljt9OAkdh1riENd7e0dqUqFbb1M4NZxvunchagOdkl5Ckb86eL18
vQ/C1zuZ6KRsNSLL8bZ8rnQk2LG4ye54rohV/EMC5aVmjYsWNe85CIA1SG51A8xV
JlvyrA4hXfrSKZmhlKdHDau+PbFuSIhN/batVUGVAqbuEH13dGvpWbg3+39VPRTa
s0f0cDFFrAGhqyMqqEDjM3TUNlD9ZBS4IfjA8ykQhjy9K6NRsdH7xyIQfElCmCyl
4cdhwvFCfYMgmvKGcWJl
=PbOC
-END PGP SIGNATURE-


Re: LDAP server

2014-12-05 Thread David Precious
On Fri, 5 Dec 2014 10:05:20 +0530
Jitendra Barik  wrote:

> Could you please let me know please, why this code is not working as
> I am trying to validate my email id to my mail server?

What happens when you try it?  "Not working" is incredibly unhelpful.

Also, LDAP to validate an email address?  Are you confusing LDAP and
IMAP, by any chance?  Sounds like you want to log in to an email server
by IMAP to confirm the mailbox username and password are correct, not
use an LDAP server.

http://en.wikipedia.org/wiki/LDAP
http://en.wikipedia.org/wiki/IMAP




-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: map vs foreach

2014-09-30 Thread David Precious
On Tue, 30 Sep 2014 14:08:14 -0700
SSC_perl  wrote:

>   Is the output of these two lines equivalent?
> 
> map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}};
> 
> $hash->{$_} = shift @record foreach @{$self->{'FIELDNAMES'}};
[...]
>   Is one more appropriate than the other in a situation like
> this, or is it simply a styling difference?

map is more suitable when using it to transform a list; it doesn't make
sense to use it as a flow-control statement where a for loop would be
more sane.

In other words, if you're not assigning or using the result of the map
call, you're using it wrong, and a for loop would be more readable
(and, in older perls at least, likely more efficient).

So, e.g.:

my %fields = map { $_ => shift @record } @{$self->{'FIELDNAMES'}};

... would make sense, because you're using map to produce the data you
want, rather than using it instead of a for loop.

Like most things, TIMTOWTDI, but I believe Perl Best Practices advises
against using map in void context.

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




Re: activity indicator

2014-09-28 Thread David Precious
On 27 Sep 2014 19:44:22 -
sharda  wrote:

>  retrieving the information from the DB in this time delay i.e the
> time it takes to fetch the data from the server i have to show an
> ajax activity indicator how can i do this.

First off, if your DB query is taking long enough to require a progress
indicator, you're probably writing horribly inefficient queries, and
could just make them run much quicker by proper use of indexes etc.

However, if you do need an indicator but don't need it to be an actual
progress bar as such (just showing that something's going on, as
opposed to how far through it is), then if you're using Javascript and
AJAXy stuff, just fire off the request to the server to fetch data via
AJAX, display a spinner, and when the response arrives, hide the
spinner and process the response data.


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




Re: Need to understand what this script does.

2014-08-11 Thread David Precious
On Mon, 11 Aug 2014 11:12:34 +0100
David Precious  wrote:

> On Sun, 10 Aug 2014 16:40:07 +0530
> Uday Vernekar  wrote:
> 
> > i din't understand what the whole script is doing.I need to
> > understand first what this script is all about.  
> 
> Well, it calls some Oracle stored procedures, [...]

I guess I should expand on that, in case you're not familiar with them
- stored procedures are essentially functions stored in the database
itself, written in PL/SQL or Java typically:

http://en.wikipedia.org/wiki/Stored_procedure

So, that means that there's code in the database system itself that
we're not seeing, so could only guess at what that code does.

I think you're somewhat beyond what this list can help with - we can
help with specific beginner questions etc, but giving a whole script
you've no idea about, and not being able to give the missing chunks
(stored procs) is somewhat beyond that.  We're not here to do the work
for you :)



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




Re: Need to understand what this script does.

2014-08-11 Thread David Precious
On Sun, 10 Aug 2014 16:40:07 +0530
Uday Vernekar  wrote:

> i din't understand what the whole script is doing.I need to understand
> first what this script is all about.

Well, it calls some Oracle stored procedures, so without knowing what
they do, we can only really tell you "it does something with data"
which isn't too helpful :)

Are you able to find someone there who wrote it / can maintain it?
You'd have more luck that way, I think.





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




Re: LWP/UserAgent fail authentication:

2014-07-31 Thread David Precious
On Thu, 31 Jul 2014 17:01:38 +0100
"mimic...@gmail.com"  wrote:

> I receive error when calling GitHub API from Perl (LWP). Basically,
> the problemis authentication failure, although the username and
> password combination is valid.
> 
> In the code snippet below, I have tried changing the values passed to
> credentials() in several ways, however it does not work.
[...]
> my $auth_token = "username:token"
[...]
> my $ua = LWP::UserAgent->new;
> $ua->agent('Mozilla/8.0');
> 
> #$ua->credentials("www.github.com:443","Authorization: token",
> "$login", "$pass");
> $ua->credentials("api.github.com:443","Authorization
> Basic:","$auth_token","");

The problem, I believe, is that LWP will send the details you've given
it for that hostname and realm, when it sends a request and the remote
server responds with a 401 Unauthorized response with a
WWW-Authenticate header, stating the auth type and realm.
Unfortunately, as you mentioned, GitHub does not do that; instead, it
pretends the resource requested does not exist.

So, LWP, never being asked to authenticate, will not do so.

You'll need to manually do it by creating a HTTP::Request object
rather than letting LWP do it for you. 

From some code of mine that successfully worked:

my $url = "http://github.com/api/v2/json/pulls/"; . $project;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $url);

# Auth if necessary
if (my $auth = $self->auth_for_project($project)) {
my ($user, $token) = split /:/, $auth, 2;
$req->authorization_basic("$user/token", "$token");
}   

my $res = $ua->request($req)
or return "Unknown - error fetching $url";


(this is from:
https://github.com/bigpresh/Bot-BasicBot-Pluggable-Module-GitHub/blob/master/lib/Bot/BasicBot/Pluggable/Module/GitHub/PullRequests.pm#L67-L77
)

... reading it, though, I'm not sure quite why it worked; GitHub's docs
say that, if you're using a token, the username should be set as
"$token:x-oauth-basic", and the password being unimportant; maybe this
code worked against their older, now withdrawn v2 API.

Anyway, I believe the lack of a proper WWW-Authenticate header from
GitHub is what's causing your problems - they don't ask for auth, so
LWP (correctly) doesn't send it.


-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: Suddenly this script is not working!

2014-07-30 Thread David Precious
On Wed, 30 Jul 2014 11:55:46 -0400
ESChamp  wrote:

> Robert Wohlfarth wrote on 7/30/2014 10:31 AM:
> > On Wed, Jul 30, 2014 at 1:00 AM, ESChamp  > > wrote:
> > 
> > 0D 0A 3C 70 72 65 3E 0D 0A
> > 
> > so no invisible characters.
> > 
> > > Or maybe  occurs on the first line, in which case $index
> > > will have the value 0, which is treated as false, and your
> > > program will die.
> > 
> >  is line 709. No other line is simply 
> > 
> > 
> > How about the 0D 0A at the end of the line? Unless the script used
> > "chomp", the exact line is "\r\n".
> 
> Thanks, Robert. I changed the  to \r\n but it made no
> difference.

Did you also replace the single quotes with double quotes, so that the
\r\n will actually be a carriage return and newline, rather than
literal?

You're probably better off doing it via a regex, or ensuring chomp() is
used, so it'll be portable between platforms with different line
endings.




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




Re: Can't Install Mojolicious on Shared Server

2014-07-30 Thread David Precious
On Tue, 29 Jul 2014 18:25:34 -0500
Mike Flannigan  wrote:
> I pay about $8 per month for a shared server hosting
> at Hostgator.  I recently asked Hostgator to install
> Mojolicious on my account because I wanted to migrate
> away from CGI.
> 
> Hostgator tells me they can't install Mojolicious "on a
> shared server [...]

Yeah, typically hosts won't install custom modules on shared servers,
and won't support long-running processes, as the box is shared with
lots of other customers.

> It appears dedicated servers are $200 per month, or slightly more.
> http://www.hostgator.com/dedicated
> I'm not likely to do that.

As Hao Wu says, a VPS will give you full control and ability to install
and run whatever you want for a LOT less than that - in fact, some
budget providers will be able to give you a VPS for about what you're
currently paying.



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




Re: Suddenly this script is not working!

2014-07-29 Thread David Precious
On Mon, 28 Jul 2014 20:48:51 -0400
Shawn H Corey  wrote:

> > copy $htmfile, $copy;  
> 
> # always check for error
> copy $htmfile, $copy or die "could not copy $htmlfile\n";

And even more helpfully, include in the message $! - which will contain
the error message to show you what actually went wrong:

copy $htmlfile, $copy
or die "Failed to copy $htmlfile to $copy - $!";






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




Re: module installation error from cpan cli

2014-07-14 Thread David Precious
On Tue, 15 Jul 2014 00:07:16 +1200
Benjamin Fernandis  wrote:

> Hi,
> 
> I got below error while installing Devel::Trace module to trace each
> line of script like sh -x.
[...]
> cpan[1]> install Devel::Trace
[...]
> t/compile.t .. Can't locate Test/More.pm in @INC (@INC contains:
[...]
 
> what could be wrong ?

Hmm - I'd say it looks like you need to install Test::More first - but
corelist says Test::More has been in core since perl 5.6.2 (unless your
distro's packaging installs perl but not the core modules, or
something).

Either way - looks like installing Test::More first should get you
going.



-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: Apologetic request for simple one-off script

2014-07-13 Thread David Precious
On Sun, 13 Jul 2014 16:43:41 -0400
ESChamp  wrote:

> I apologize for having to ask this but my nearly-80-year-old brain
> just could not come up with a solution.
> 
> I have a text file consisting of several space-separated fields:
> 
> lastname firstname other other other ... emailaddress
> 
> I wish to write a new file that contains only the emailaddress field
> contents.
> 
> There's no need to test the emailaddress field.
> 
> I just need a quick and dirty solution as this will be used just one
> time.

Is the email address always the last field?

If so, a perl one-liner would do - using Perl's auto-split feature (the
-a) option:


$ perl -lane 'say $F[-1]' in.txt

That will read through the file, split the fields, and print the last
field of each line.

You can, of course, redirect the output to a new file using normal
shell redirection, e.g.:

$ perl -lane 'say $F[-1]' in.txt > out.txt



-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: script to match a valid email id

2014-07-11 Thread David Precious
On Fri, 11 Jul 2014 10:20:10 +0300
Yosef Levy  wrote:
> 2014-07-10 21:30 GMT+03:00 Sunita Pradhan
> :
> 
> > I want to write a script which will verify a valid email address .
> > Could anybody give some ideas , how to write a pattern for this ?

> \b[\w\.-]+@[\w\.-]+\.\w{2,4}\b
> This was taken from:
> http://www.regexr.com/

And is hopelessly naive, and will reject lots of perfectly valid email
addresses.  For instance, there are lots of new TLDs these days longer
than 4 characters - .ninja, .museum, .report, .services, .vision, etc
etc etc.

It would also match e.g.  --a...@--.abcd' and lots of other completely
invalid addresses.

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




Re: script to match a valid email id

2014-07-10 Thread David Precious
On Thu, 10 Jul 2014 11:54:07 -0700
Bob goolsby  wrote:

> Nota Bene:
> xxx@zzz.www IS NOT the only valid format for an email
> address  In this case, you really need to rethink your objection
> to using the proper CPAN module

*This*.

Use the well-trusted, battle-tested CPAN module.  Whatever you build
will likely be sub-par, and will let through invalid email addresses,
or, more likely, reject perfectly valid addresses you hadn't thought
about.  (Not casting any aspersions on your skill level - email
validation is notoriously tricky to get right!)

Note: you're testing for "valid looking" - the only way to know for
sure if an email address is actually OK is to send an email to it
and have them click a link.  With all the new TLDs coming out all the
time, you can no longer rely on a pre-set list of TLDs which exist -
new TLDs (some pointless, some uttterly stupid) are being released all
the time, and older email validation code that thinks it knows of all
TLDs which exist will reject perfectly valid email addresses.

(You can do some checks, for instance, does the domain part resolve
with an MX or A record; if not, it's probably not an email address you
want to accept.

However, even that could trip you up.

For example, "mickey mouse"@[127.0.0.1] is a valid email address :)


-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github




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




Re: Teach script to look for modules in the same prefix when using MakeMaker

2014-06-16 Thread David Precious
On Sun, 15 Jun 2014 20:37:55 +0200
drupsspen  wrote:
> I'm building an application in Perl and is looking into using a build
> system for it. So far I've looked at ExtUtils::MakeMaker. I have
> divided my application like this:
> 
> bin/foo
> lib/Foo/Bar.pm
> lib/Foo/Baz.pm
> 
> The user runs the program by invoking foo, which is an executable
> defined in EXE_FILES. Most of the application is however located in
> lib/Foo and is imported by foo.
> 
> The problem that I have is that I want to allow the user to install
> the program in any directory by specifying PREFIX=/some/path when
> running my Makefile.PL. It won't find the installed modules from lib
> unless it's installed in a path where perl already looks for them. So
> what I need is a way to make foo tell perl where to look for modules.
> 
> Is there a way to do this?

You want "use lib" - in particular, to say "look in a directory named
lib, under the current directory", you can say just:

use lib './lib';

(in fact, "use lib 'lib'" would be enough, but I think it's mildly
clearer with the ./ notation)



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




Re: Failed: PAUSE indexer report JUNIPER/Net-Netconf-0.01.zip

2014-06-02 Thread David Precious

Hi Priyal,

On Mon, 2 Jun 2014 11:17:29 +
Priyal Jain  wrote:
> I am uploading my module Net::Netconf, after making changes in cpan.
> But it is giving below error. I cross checked my module and it is
> complete and has all the dependency, do not know why it is giving
> this error.

The error was:

>> This distribution name can only be used by users with permission for
>> the package Net::Netconf, which you do not have.

The error is fairly clear - you're trying to upload something under a
name you don't have permissions on.

PAUSE tells me that Net::Netconf's permissions are just first-come for
user JPRIYAL - but from the report you got, you were trying to upload
it as the account JUNIPER.

So, you'll need to either:

(a) upload as JPRIYAL, not as JUNIPER;

(b) log in to PAUSE as JPRIYAL, and assign the appropriate permissions
to JUNIPER for that module, using:
https://pause.perl.org/pause/authenquery?ACTION=share_perms

(You can either give JUNIPER co-maint, or transfer primary
maintainership.)




-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: How to change username in Pause account

2014-05-29 Thread David Precious
On Thu, 29 May 2014 10:59:56 +
Priyal Jain  wrote:

> Hello,
> 
> I want to change my username in PAUSE, how should I do that.

I don't think you can change your PAUSE username.  After all, it would
require all mirrors to know to rename your PAUSE dir, invalidate old
links, etc.

You can, however, change the display name used for it, at:

https://pause.perl.org/pause/authenquery?ACTION=edit_cred



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




Re: close dbh in the subroutine

2014-05-08 Thread David Precious
On Thu, 08 May 2014 16:54:32 +0400
Jeff Pang  wrote:

>  Hi,
> 
> I created a $dbh object in the main body, and pass it to a
> subroutine, then close the $dbh in the subroutine, does it  influence
> the $dbh in the main?

Yes.  It's a reference to the same DBI object.



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




Re: [irc Q] how to extract info to disk

2014-05-08 Thread David Precious
On Wed, 07 May 2014 15:24:05 -0400
Harry Putnam  wrote:

> I am a complete and total novice to irc.  Never used and never wanted
> to before.
> 
> Rather than put my vast ignorance on display and annoy the heck out of
> others on the channel, I'd like to extract information from the irc
> servers and write it to disk.
> 
> My most often used scripting language is perl so I wondered if there
> is already a perl module that will allow me to do that.

Bot::BasicBot will allow you to write an IRC bot; irssi, XChat and
others support Perl scripting for "plugins" to add features to the
client.

However, I think there are better tools at your disposal...

> Here is an example of the kind of mess you can get into:
> 
> Connecting to irc.freenode.net and foolishly but at least not
> malignantly using the 'list' command.
> 
> /list
> 
> It spewed out so many lines that just kept coming until I finally
> ^ c'ed over and over to get it to stop.

Yeah, /list is a useless command on big networks.

On Freenode, try "/q alis help" - alis is a channel listing service
which allows you to search for channels by pattern.

There's also irc.netsplit.de's IRC server/channel search engine:

http://irc.netsplit.de/channels/?net=freenode


-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: unsatisfied configure_requires in perl 5.12.3?

2014-04-22 Thread David Golden
On Tue, Apr 22, 2014 at 3:17 PM, Daniel Staal  wrote:
> My point is that if you think that's necessary, why not say so in your
> module?  (Where others can see that requirement, and fulfil it on a
> case-by-case basis, instead of a large scale toolchain modification for all
> users.)


It's a chicken and egg situation.  Before Perl v5.10.1, CPAN clients
that shipped with Perl did not support configure_requires.
Presumably, once at least those versions of the clients are installed,
any distribution can configure_requires (or use other prereqs) to
specify and get what it wants.

And, as we occasionally see, even still the toolchain is buggy.

A PASS report from an updated toolchain tells people what is possible
to achieve.

A FAIL report from an outdated toolchain tells people either that the
distribution is bad or that the toolchain is broken.

Ambiguous causes of failure are a problem because it takes extra human
effort to disambiguate -- effort which is often in short supply.
Providing an updated toolchain as much as possible for smoking
optimizes for people's time.

The more people find CPAN testers to be a waste of time, the less
attention they will pay to the information it generates.

David

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




Re: time based test

2014-02-15 Thread David Precious
On Fri, 14 Feb 2014 16:21:52 -0500
shawn wilson  wrote:

> What I want is to be able to make a program /think/ that it's like a
> week in the future without messing with the system time at all. So
> something that overrides gmtime and localtime and the like with some
> starting point would be awesome. Anything like that?
> 

Time::Fake on CPAN appears to fit the bill:

https://metacpan.org/pod/Time::Fake

Install it, then e.g.:

perl -MTime::Fake="+20y" yourscript.pl

(See the docs for the various options etc.)


-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: mv Is Not Working the Same Way Under System as Under a Shell.

2014-01-30 Thread David Precious
On Wed, 29 Jan 2014 16:10:25 -0600
"Martin G. McCormick"  wrote:
>   I keep getting the "Usage"  help message and a
> permission denied. If I su to root and manually make the move,
> it works.

change "system" to "print" to print out the command that would be run,
and (a) you'll likely see the problem, or (b) you can try running that
exact command.


> system(
> "mv $directories$filename \"$directories\"deleted_keys/"
> );

I doubt you meant that extraneous \" in the middle there - I suspect
you meant \"$directories/deleted_keys\".

Oh, and make sure that the variables you're interpolating there aren't
from an untrusted source :)

Cheers

Dave P 


-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: baby perl to get the right date

2014-01-28 Thread David Precious
On Tue, 28 Jan 2014 08:32:20 +0100
Luca Ferrari  wrote:

> Hi all,
> often I find myself writing something like the following to get the
> "human" date:
> 
> my ($day, $month, $year) = (localtime())[3..5];
> $month++, $year += 1900;
> print "\nToday is $month / $day / $year \n";
> 
> 
> I was wondering if there's a smarter pattern to get the right value in
> one single line. At least there's no simple "map" I can think of.

I tend to use DateTime to manipulate dates, so I'd say
something like DateTime->now->dmy('/');

It's a bit of a slow and heavyweight way to go about it if you're not
already planning to use DateTime, though :)


-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: Still Trying to Understand Perl Module Control Process

2014-01-14 Thread David Precious
On Mon, 13 Jan 2014 10:32:14 -0600
"Martin G. McCormick"  wrote:

>   I am using cpanp or cpan plus to handle perl modules on
> a FreeBSD system. If I give the command
> 
> cpanp -i Net::DNS
> it installs Net::DNS 0.73. Normally, this is exactly what one
> would want it to do but Net::DNS0.73 is buggy. At least one bug
> causes domain name server or DNS updates to fail so I either
> want the version just below Net:DNS0.73 or there are a couple of
> versions of it dated January of 2014, basically anything but the
> one it keeps trying to install.

You can provide the exact one you want, e.g.:

cpanp -i NLNETLABS/Net-DNS-0.72.tar.gz


(I don't use cpanplus, but I'd imagine that'll work; it does with cpan
and cpanm)



-- 
David Precious ("bigpresh") 
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



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




Re: series of numbers to range notation

2014-01-07 Thread David Precious
On Tue, 7 Jan 2014 13:35:39 -0600
Hal Wigoda  wrote: 
> On Tue, Jan 7, 2014 at 1:28 PM, Rajeev Prasad <.ne...@yahoo.com>
> wrote:
> > so i have this series of numbers:
> > [...]
> > there are two ranges in there: 349-396, 425-496
> >
> > any hints as to how to write a perl code to represent the series of
> > numbers as a range?

> (349..396,425..496);

The impression I got was that the OP wanted code to take that list of
numbers, and identify the ranges within.  I may be wrong, though.

If I'm right, though, simply looping through the range in numerical
order, checking if the number you're looking at is one higher than the
last, and either adding it to the "current" range if so, or starting a
new range if not, should be pretty easy - e.g. assuming @nums contains
that sorted list of numbers, then:


my @ranges;
my @current_range;
my $lastnum;

for my $num (@nums) {
# See if we're starting a new range - if so, add the last range 
# to the set of ranges we've found:
if (@current_range && $num != $lastnum +1 ) {
push @ranges, (@current_range > 1)
? $current_range[0] . '-' . $current_range[-1]
: @current_range;
@current_range = ($num);
# Otherwise, just add this number to the current range
} else {
push @current_range, $num;
}

$lastnum = $num;
}

# Finally, when we've run out of numbers, we might have a remaining
range left # to add, so handle that:
if (@current_range) {
push @ranges, (@current_range > 1)
? $current_range[0] . '-' . $current_range[-1]
: @current_range;
}


There are likely shorter and more elegant ways than the above, though.

In fact, I'd be surprised if something like this doesn't exist on CPAN
already.

There's https://metacpan.org/pod/Number::Rangify which is most of hte
way there, but returns ranges as Set::IntRange objects, which is
probably a little OTT.


-- 
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   >