Re: Benchmark for Scala, Ruby and Perl

2022-01-17 Thread Paul Procacci
Hey John,

One more follow up and then it's bedtime for me.  I wanted to further this
discussion just a little bit more by implementing the mmap solution that I
applied to perl to ruby instead.  Now all of a sudden, ruby is much much
faster.  My ruby source code follows:

Goodnight!

# ruby -W0 ./doit.rb | md5
786be54356a5832dcd1148c18de71fc8
# perl ./doit2.pl | md5
786be54356a5832dcd1148c18de71fc8


# truss -c ruby -W0 ./doit.rb

  - --- ---
0.0141115021855 260

# truss -c perl ./doit2.pl

  - --- ---
0.049820267 777  52



-
require 'mmap';

stopwords = {}
mmap_s = Mmap.new('stopwords.txt')
mmap_s.advise(Mmap::MADV_SEQUENTIAL)
mmap_s.each_line do |s|
  s.strip!
  stopwords[s] =1
end

count = {}
mmap_c = Mmap.new('words.txt')
mmap_c.advise(Mmap::MADV_SEQUENTIAL)
mmap_c.each_line do |s|
  s.strip!
  if ! stopwords.has_key?(s)
if count.has_key?(s)
   count[s] += 1
else
   count[s] = 1
end
  end
end

z = count.sort {|a1,a2| a2[1]<=>a1[1]}
z.take(20).each do |s| puts "#{s[0]} -> #{s[1]}" end

On Sat, Jan 15, 2022 at 3:48 AM Paul Procacci  wrote:

> Hey John,
>
> On Sat, Jan 15, 2022 at 3:04 AM Jon Smart  wrote:
>
>>
>> Hello Paul
>>
>> Do you mean by undef $/ and with <$fh> we can read the file into memory
>> at one time?
>>
>
> In most cases the short answer is yes.
> I have problems with your wording however given the 'geek' that I am.  'At
> one time'  not quite.  In your example there were over 4000 read(2)
> syscalls by the operating system for instance.  This wouldn't have been 'at
> one time'.  ;)
>
>
> Yes that would be faster b/c we don't need to read file by each line,
>> which increases the disk IO.
>>
>>
> It actually doesn't make it faster.
> Perl buffers it's reads as does all modern programming languages.  If you
> ask perl to give you 10 bytes it certainly will, but what you don't know is
> that perl has really read up to 8192 bytes.  It only gave you what you
> asked for and the rest is sitting in perl buffers.
> To put this another way, you can put 8192 newline characters in a file and
> read this file line by line.  This doesn't equate to 8192 separate read(2)
> syscalls ... it's just 1 read syscall.  It won't be faster nor slower.
>
>
>
>> Another questions:
>> 1. what's the "truss" command?
>>
>
> truss is akin to strace.  If you're on linux, you can install strace and
> get the samish type of utility.
> It allows you to trace system calls and see how much of your time for a
> given program is waiting on the kernel and/or how often it's asking the
> kernel to do something.
>
> 2. what's the syntax "<:mmap"?
>>
>> mmap is a method of mapping a file (among other things) into memory on an
> on-demand basis.
> Given the example you provided, this is actually where the speed up comes
> from.  This is because my version removes the 4000+ read(2) syscalls in
> favor of just 2 mmap(2) syscalls.
>
> Thank you.
>
>
> ~Paul
>


-- 
__

:(){ :|:& };:


Re: Benchmark for Scala, Ruby and Perl

2022-01-17 Thread Paul Procacci
Hey Jon,

The most glaringly obvious thing I could recommend is that at least in your
perl routine (and probably the other languages) most of your time is
context switching reading from the disk.
Now, my perl version is indeed faster, but one has to ask themselves, was
.015193256 seconds really worth the effort?  /shrug   -- If this is for a
financial industry perhaps, but then they'd just have written it in C.
Otherwise, probably not.
Also note, there's other ways to speed this up even further, but at that
point it isn't really worth the time.  We're talking a couple of
microseconds at best.  I've included my version for your reference.

Before closing, I happen to like micro benchmarks whether or not you think
'I know this benchmark is maybe meaningless' as your site says.
If anything, it can absolutely be useful.  I personally think sometimes
they are and others not so much.  Just depends on the context.

Your perl source (doit) .. my perl source (doit2):
# ./doit2.pl | md5
786be54356a5832dcd1148c18de71fc8
root@nas:~ # ./doit.pl | md5
786be54356a5832dcd1148c18de71fc8

# truss -c ./doit.pl

syscall seconds   calls  errors
read0.0368288134140   0

  - --- ---
0.0378218215227 284



# truss -c ./doit2.pl

syscall seconds   calls  errors
read0.000245121  19   0

  - --- ---
0.022628565 804  59


-
use strict;

$/ = undef;
my %stopwords = do {
open my $fh, '<:mmap', 'stopwords.txt' or die $!;
map { $_ => 1; } split /\n/, <$fh>;
};

my %count = do {
my %res;
open my $fh, '<:mmap', 'words.txt' or die $!;
map { $res{$_}++ unless $stopwords{$_}; } split /\n/, <$fh>;
%res;
};

my $i=0;
for (sort {$count{$b} <=> $count{$a}} keys %count) {
if ($i < 20) {
print "$_ -> $count{$_}\n"
} else {
   last;
}
$i ++;
}

On Sat, Jan 15, 2022 at 12:37 AM Jon Smart  wrote:

> Hello,
>
> May I show the result of my benchmark for perl5, ruby, and scala?
> https://blog.cloudcache.net/benchmark-for-scala-ruby-and-perl/
>
> Welcome you to give any suggestion to me for improving this.
>
> Thanks.
>


-- 
__

:(){ :|:& };:


Re: Benchmark for Scala, Ruby and Perl

2022-01-17 Thread Paul Procacci
Hey John,

On Sat, Jan 15, 2022 at 3:04 AM Jon Smart  wrote:

>
> Hello Paul
>
> Do you mean by undef $/ and with <$fh> we can read the file into memory
> at one time?
>

In most cases the short answer is yes.
I have problems with your wording however given the 'geek' that I am.  'At
one time'  not quite.  In your example there were over 4000 read(2)
syscalls by the operating system for instance.  This wouldn't have been 'at
one time'.  ;)


Yes that would be faster b/c we don't need to read file by each line,
> which increases the disk IO.
>
>
It actually doesn't make it faster.
Perl buffers it's reads as does all modern programming languages.  If you
ask perl to give you 10 bytes it certainly will, but what you don't know is
that perl has really read up to 8192 bytes.  It only gave you what you
asked for and the rest is sitting in perl buffers.
To put this another way, you can put 8192 newline characters in a file and
read this file line by line.  This doesn't equate to 8192 separate read(2)
syscalls ... it's just 1 read syscall.  It won't be faster nor slower.



> Another questions:
> 1. what's the "truss" command?
>

truss is akin to strace.  If you're on linux, you can install strace and
get the samish type of utility.
It allows you to trace system calls and see how much of your time for a
given program is waiting on the kernel and/or how often it's asking the
kernel to do something.

2. what's the syntax "<:mmap"?
>
> mmap is a method of mapping a file (among other things) into memory on an
on-demand basis.
Given the example you provided, this is actually where the speed up comes
from.  This is because my version removes the 4000+ read(2) syscalls in
favor of just 2 mmap(2) syscalls.

Thank you.


~Paul


Re: Benchmark for Scala, Ruby and Perl

2022-01-17 Thread Paul Procacci
Sorry, it's 5:00am here and needless to say it's wy past my bedtime and
I'm making mistakes.
The comparison should have been between both ruby versions  ugh.

I'll let you play though.  Have a great night.

On Sat, Jan 15, 2022 at 4:57 AM Paul Procacci  wrote:

> Hey John,
>
> One more follow up and then it's bedtime for me.  I wanted to further this
> discussion just a little bit more by implementing the mmap solution that I
> applied to perl to ruby instead.  Now all of a sudden, ruby is much much
> faster.  My ruby source code follows:
>
> Goodnight!
>
> # ruby -W0 ./doit.rb | md5
> 786be54356a5832dcd1148c18de71fc8
> # perl ./doit2.pl | md5
> 786be54356a5832dcd1148c18de71fc8
>
>
> # truss -c ruby -W0 ./doit.rb
> 
>   - --- ---
> 0.0141115021855 260
>
> # truss -c perl ./doit2.pl
> 
>   - --- ---
> 0.049820267 777  52
>
>
>
> -
> require 'mmap';
>
> stopwords = {}
> mmap_s = Mmap.new('stopwords.txt')
> mmap_s.advise(Mmap::MADV_SEQUENTIAL)
> mmap_s.each_line do |s|
>   s.strip!
>   stopwords[s] =1
> end
>
> count = {}
> mmap_c = Mmap.new('words.txt')
> mmap_c.advise(Mmap::MADV_SEQUENTIAL)
> mmap_c.each_line do |s|
>   s.strip!
>   if ! stopwords.has_key?(s)
> if count.has_key?(s)
>count[s] += 1
> else
>count[s] = 1
> end
>   end
> end
>
> z = count.sort {|a1,a2| a2[1]<=>a1[1]}
> z.take(20).each do |s| puts "#{s[0]} -> #{s[1]}" end
>
> On Sat, Jan 15, 2022 at 3:48 AM Paul Procacci  wrote:
>
>> Hey John,
>>
>> On Sat, Jan 15, 2022 at 3:04 AM Jon Smart  wrote:
>>
>>>
>>> Hello Paul
>>>
>>> Do you mean by undef $/ and with <$fh> we can read the file into memory
>>> at one time?
>>>
>>
>> In most cases the short answer is yes.
>> I have problems with your wording however given the 'geek' that I am.
>> 'At one time'  not quite.  In your example there were over 4000 read(2)
>> syscalls by the operating system for instance.  This wouldn't have been 'at
>> one time'.  ;)
>>
>>
>> Yes that would be faster b/c we don't need to read file by each line,
>>> which increases the disk IO.
>>>
>>>
>> It actually doesn't make it faster.
>> Perl buffers it's reads as does all modern programming languages.  If you
>> ask perl to give you 10 bytes it certainly will, but what you don't know is
>> that perl has really read up to 8192 bytes.  It only gave you what you
>> asked for and the rest is sitting in perl buffers.
>> To put this another way, you can put 8192 newline characters in a file
>> and read this file line by line.  This doesn't equate to 8192 separate
>> read(2) syscalls ... it's just 1 read syscall.  It won't be faster nor
>> slower.
>>
>>
>>
>>> Another questions:
>>> 1. what's the "truss" command?
>>>
>>
>> truss is akin to strace.  If you're on linux, you can install strace and
>> get the samish type of utility.
>> It allows you to trace system calls and see how much of your time for a
>> given program is waiting on the kernel and/or how often it's asking the
>> kernel to do something.
>>
>> 2. what's the syntax "<:mmap"?
>>>
>>> mmap is a method of mapping a file (among other things) into memory on
>> an on-demand basis.
>> Given the example you provided, this is actually where the speed up comes
>> from.  This is because my version removes the 4000+ read(2) syscalls in
>> favor of just 2 mmap(2) syscalls.
>>
>> Thank you.
>>
>>
>> ~Paul
>>
>
>
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Benchmark for Scala, Ruby and Perl

2022-01-17 Thread Paul Procacci
On Sat, Jan 15, 2022 at 5:03 AM Jon Smart  wrote:

>
> Thanks Paul. I am surprised that mmap has that huge IO advantages
> comparing to the classic way. So ruby take more benefit from this mmap
> calling. Just get learned from your case.
>
> Regards
>
>
It's not always beneficial.  There are cases where one is more beneficial
over the other.
. and unfortunately it's not really well defined when to use one over
the other.

The general consensus has been that if you plan on jumping around a lot in
a file, then mmap will be superior to read.
Otherwise when sequentially reading, then read will be superior to mmap.

The inclusion of my perl mmap version clearly disproves that ON MY OS.
It may be different ON YOUR OS.

This is partially why micro benchmarking is hard to do.  Your scripts will
almost certainly act one way on your machine, and different on others.
You may very well try an mmap version on your machine and scratch your head
because it's slower  there's more than just the userland processes
involved here.  ;)

Goodnight (again).


Re: function alias

2019-09-04 Thread Paul Johnson
On Wed, Sep 04, 2019 at 02:17:36PM +0800, Wesley Peng via beginners wrote:
> Hello,

This is actually two different questions with two different answers:

> How to make a function alias in perl?

$ perl -E 'sub b { say 67 } *says = \ says(83)'
67
$

>   for example, says() is alias to
> print().

This is not possible.  Though it is with some core functions.

See https://perldoc.perl.org/CORE.html for details.

-- 
Paul Johnson - p...@pjcj.net

-- 
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 Paul Johnson
On Thu, Nov 22, 2018 at 12:33:25PM -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';

Or, nowadays, and if your perl version(s) support it, as:

sub hello ($name = "Anonymous Person") {

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




Re: return the list content

2018-06-14 Thread Paul Johnson

On Mon, Aug 18, 2014 at 06:07:59AM -0700, John SJ Anderson wrote:

On Mon, Aug 18, 2014 at 1:38 AM, Paul Johnson  wrote:
> On Mon, Aug 18, 2014 at 04:17:53PM +0800, Ken Peng wrote:

>> which one is the better way to return the list content? And if the
>> method is an instance method?



To expand further (because you asked about "list context")


Not to take away from your explanation, but it was actually "list
content" (both times) rather than "list context".  (Three bits
difference.)  I had initially thought there was something to do with
context in the question too  and had to double check to be sure.  Then
your message made me doubt all over again.

The problem of having brains that are too good at finding what they are
looking for.

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




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

2018-01-29 Thread Paul Johnson
On Sun, Jan 28, 2018 at 10:57:25PM +, Chas. Owens wrote:
> $#array is the index of the last element of @array, so it will be one less
> than scalar @array which is the number of elements in @array (since Perl
> arrays start with index 0). Therefore, to get the number of elements, you
> would need to add one to $#array, which makes scalar @array faster. To get
> the last index you would need to say @array - 1, which would make $#array
> faster.

But it doesn't matter, because this is not a bottleneck in your code.

So code for clarity.



> On Sun, Jan 28, 2018, 13:39 Peng Yu <pengyu...@gmail.com> wrote:
> 
> > Hi,
> >
> > For the following two expressions, are they of the same speed or one
> > of them is faster?
> >
> > `$#array` vs `scalar @array`

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




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

2017-08-05 Thread Paul Johnson
On Fri, Aug 04, 2017 at 05:45:08PM +0200, hw wrote:
> Paul Johnson wrote:
> > On Thu, Aug 03, 2017 at 08:44:45PM +0200, hw wrote:
> > > 
> > > Hi,
> > > 
> > > suppose I have a class FOO and a class BAR.  The parent of BAR is FOO.
> > > 
> > > I would like FOO to /use/ BAR because BAR has some methods needed by FOO.
> > > BAR is /decended/ from FOO because FOO has many methods needed by BAR.
> > > 
> > > Is this possible, or does it lead to some endless recursion when 
> > > compiling?
> > 
> > You may well find that using roles would provide a better solution than
> > using inheritance.  The methods would wish to call from both classes
> > could be defined in one or more roles.  Roles are more usually called
> > traits in other languages.  You can use roles within Moose or Moo, or by
> > using other CPAN modules.  You can read more about roles/traits at
> > 
> >   https://en.wikipedia.org/wiki/Trait_(computer_programming)
> > 
> 
> Hmm.  I think I almost half-way understand what this is about.  For this
> case, it seems as if I would have FOO and BAR use traits instead of having
> FOO use BAR, and it would be somehow different from using a metaclass FOO
> and BAR descend from, like upside-down inheritance turned upside-down again
> to finally get it right.

That's certainly along the right lines, yes.

> I´m not using Moose or anything yet, though.  I thought it might be better
> to start with something basic and perhaps later upgrade.

That's not a bad idea.  But if this is real work rather than an
exercise, be careful unless you know you will have a chance to rewrite
it after the first version.

> Why do I have to run into such a problem?  I´m just starting to learn this.

You might have a particularly difficult problem to solve.  But if you're
just starting it's more likely that you are not decomposing the problem
as well as you could.

If you don't want to use roles you can get a similar effect using
multiple inheritance, where the contents of the role would be included
in a base class.  But there are reasons why roles are generally
recommended over multiple inheritance.

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




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

2017-08-04 Thread Paul Johnson
On Thu, Aug 03, 2017 at 08:44:45PM +0200, hw wrote:
> 
> Hi,
> 
> suppose I have a class FOO and a class BAR.  The parent of BAR is FOO.
> 
> I would like FOO to /use/ BAR because BAR has some methods needed by FOO.
> BAR is /decended/ from FOO because FOO has many methods needed by BAR.
> 
> Is this possible, or does it lead to some endless recursion when compiling?

You may well find that using roles would provide a better solution than
using inheritance.  The methods would wish to call from both classes
could be defined in one or more roles.  Roles are more usually called
traits in other languages.  You can use roles within Moose or Moo, or by
using other CPAN modules.  You can read more about roles/traits at

  https://en.wikipedia.org/wiki/Trait_(computer_programming)

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




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

2017-08-03 Thread Paul Johnson
On Thu, Aug 03, 2017 at 09:27:42PM +0200, hw wrote:
> 
> It is nonsense to logically negate a string, and it is nonsense to convert
> undefined values into 'false'.  Either are neither false, nor true.
> 
> For undefined values, there is no way of deciding whether they are true or 
> false
> because they are undefined.
> 
> When you convert undefined values to false, then you must also convert false
> to undefined values.  Logic dictates that otherwise undefined values are
> not equal to undefined values.  Yet perl claims that they are:
> 
> perl -e 'print "true\n" if(undef == undef);'
> perl -e 'print "true\n" if(0 == undef);'
> 
> Both is just wrong.  The value 0 is defined to the point where you can´t 
> define
> it any more.

If you try to fight against Perl, or any language, you will have an
unsatisfying experience.  The trick is to work with the language.  Then
programming becomes productive and enjoyable, the sun shines, ponies
frolic through meadows, and unicorns graze contentedly beneath rainbows.

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




Re: Module to extract patterns

2017-06-13 Thread Paul Johnson
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/




DBI and hosts file entries

2017-06-01 Thread Paul M via beginners
I have a hosts entry that points to a specific IP address. It does not appear 
that DBI:Sybase:server uses the /etc/hosts file? Is this correct?Ping works 
fine in shell.How would I point a server name to different IP addresses 
locally?What does DBI use for name resolution?
Thanks!

Re: How to delete multiple indices from array

2017-04-12 Thread Paul Johnson
On Wed, Apr 12, 2017 at 11:12:45PM +0200, David Emanuel da Costa Santiago wrote:

> Thank you all for your replies.
> 
> I tested the 5 solutions with the benchmark module, and the splice one
> is the fastest:

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

But they're all fast enough.  Or none of them are.  So choose the
solution which is the clearest.

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




Re: [OT] app (ncurses?) mechanizer?

2017-03-27 Thread Paul Johnson
On Tue, Mar 28, 2017 at 12:20:29AM +0300, Shlomi Fish wrote:
> Hi Paul!
> 
> On Mon, 27 Mar 2017 22:21:06 +0200
> Paul Johnson <p...@pjcj.net> wrote:
> 
> > On Mon, Mar 27, 2017 at 04:04:22PM +0200, Luca Ferrari wrote:
> > > Hi all,
> > > I've to run a very old application from the command line (unix), it
> > > seems to me a ncurses application but I'm not sure that is the real
> > > case (let's say it seems ncurses).
> > > Anyway, I have to launch the application with a file name, do a couple
> > > of menu interactions and exit, then do it again for a hundred or so
> > > files.
> > > Is there any kind of "app-mechanize" similar to www::mechanize?  
> > 
> > Nothing to do with perl, but you could try xdotool
> > 
> >   http://www.semicomplete.com/projects/xdotool/
> > 
> 
> The original poster was asking about automating an ncurses/etc. unix 
> *terminal*
> app - not an X11-based app which is what xdotool is for.

You'll have to run your terminal under X, it's true.  But otherwise it
seems a simple, low-tech solution to a one-off problem.  You could even
generate your xdotool script with perl, to bring us back a little on
topic.

Expect may well work, and would be a more robust solution, but I suspect
there's a reasonable chance of running into difficulties, hence the
alternative suggestion.

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




Re: [OT] app (ncurses?) mechanizer?

2017-03-27 Thread Paul Johnson
On Mon, Mar 27, 2017 at 04:04:22PM +0200, Luca Ferrari wrote:
> Hi all,
> I've to run a very old application from the command line (unix), it
> seems to me a ncurses application but I'm not sure that is the real
> case (let's say it seems ncurses).
> Anyway, I have to launch the application with a file name, do a couple
> of menu interactions and exit, then do it again for a hundred or so
> files.
> Is there any kind of "app-mechanize" similar to www::mechanize?

Nothing to do with perl, but you could try xdotool

  http://www.semicomplete.com/projects/xdotool/

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




Re: deprecated idiom to simulate state variables

2017-01-10 Thread Paul Johnson
On Tue, Jan 10, 2017 at 09:04:00AM +0100, Luca Ferrari wrote:
> Hi all,
> I guess it was in Modern Perl that I saw this deprecated idiom to
> simulate state <http://perldoc.perl.org/functions/state.html>
> variables:
> 
> sub foo{
>my $initialized_once = 1 if 0;
>   ...
> }
> 
> Now, why is that working and initializing the variable the first time?
> I mean, each time such row is evaluated the condition is 0 = false, so
> the variable should not be initialized never.
> What am I missing?

You are missing a fairly complicated interaction between the
implementation details of variable handling at compile-time and
run-time.  The details are probably not important unless you want to
understand them in order to hack on the core, or just for interest.  In
that case you will be able to find out all the gory details by searching
the perl5-porters mailing list archives or doing some git archaeology.

The behaviour came about by accident, but then people found out that it
filled a hole in the language and started using it.  The important point
is that it was deprecated in perl-5.10.0 and will be removed in
perl-5.30.0 (in a couple of years).  As you note, the correct way to get
this behaviour nowadays is to use the "state" keyword.

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




Re: XML::Simple Umlaute

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

As a general rule, use "utf8::all" instead of just "utf8" and a lot of
the problems go away.

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

Quite.  And utf8::all tries to encapsulate as much of that boilerplate
as it can.

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




Re: Are there (non-obvious) differences in string quoting methods?

2016-04-06 Thread Paul Johnson
On Wed, Apr 06, 2016 at 08:20:24PM +0100, Jonathon Fernyhough wrote:
> Hi,
> 
> I'm working my way through Learning Perl (6th) and Modern Perl (4th) and
> was wondering whether there are any (non-obvious) drawbacks to the
> different string quoting methods.
> 
> First up, double-quoted strings. The "usual method" of double-quoting
> has an alternative of qq{For a string with $variable interpolation};
> 
> qq{} obviously wins when there would otherwise be a lot of escaping, but
> are there any downsides of using this method more generally (other than
> double-quotes being two characters shorter)? For example, is it "faster"
> for Perl to parse a double-quoted string or does the compiler optimise
> this out so the methods are fundamentally equivalent?

Whether it is faster, slower or the same speed (and I really don't know
which it is) I am all but certain that you wouldn't be able to measure
the difference.  Do not optimise for speed until you really have to and
you have profiled your code to know which bits are taking the time.  I
guarantee you that it won't be this.

> Second, single- vs. double-quoted strings for non-interpolated use. In
> Ruby, I've been given the impression* that single-quoted strings are
> "more efficient" than double-quoted strings when interpolation is not
> required (that is, prefer 'Bob' over "Bob"). I'm wondering whether there
> is any similar recommendation for Perl?

Copy and paste my previous paragraph here.

With regard to single versus double quoting where there is no
interpolation, people have different ideas.  My considered opinion is to
use double quotes in all cases, except where it would lead to escaping
which would not be necessary with single quotes.

My reason is that Perl is by default interpolative and using single
quotes fights against that.  I have frequently had to change the quotes
when either adding or removing a variable from a string when working on
a code-base that prefers single quotes.

A few years ago, Tom Christiansen expanded on this in more detail than
you want to read.  Thankfully, he also summarised his argument.

Summary:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-08/msg00402.html

Full (bottom section):
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-08/msg00390.html

You'll notice that I disagree with Uri.  You should follow the coding
guidelines of any existing project you are working on, and make up your
own mind about what to do when you get to decide the guidelines.

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




Re: returning arrays

2016-01-25 Thread Paul Johnson
On Mon, Jan 25, 2016 at 12:24:04AM +0100, lee wrote:
> Paul Johnson <p...@pjcj.net> writes:
> 
> > On Sun, Jan 24, 2016 at 05:44:14PM +0200, Shlomi Fish wrote:
> >> Hi lee,
> >> 
> >> On Sun, 24 Jan 2016 13:11:37 +0100
> >> lee <l...@yagibdah.de> wrote:
> >> 
> >> > Paul Johnson <p...@pjcj.net> writes:
> >> > >
> >> > > In scalar context the comma operator evaluates its left-hand side,
> >> > > throws it away and returns the right-hand side.  
> >> > 
> >> > What is the useful use for this operator?
> >> > 
> >> 
> >> Well, I believe its use was originally inherited from
> >> https://en.wikipedia.org/wiki/C_%28programming_language%29 where one can do
> >> something like:
> >> 
> >>x = (y++, y+2);
> >> 
> >> In Perl 5 though it is preferable to use do { ... } instead:
> >> 
> >>$x = do { $y++; $y+2; };
> >
> > In both Perl and C the comma operator is probably most usually 
> > (deliberately)
> > seen in for statements:
> >
> > #!/usr/bin/env perl
> >
> > use strict;
> > use warnings;
> >
> > for (my ($x, $y) = (1, 7); $x < 5; $x++, $y--) {
> > print "$x $y\n";
> > }
> >
> > and
> >
> > #include 
> >
> > int main() {
> > int x, y;
> > for (x = 1, y = 7; x < 5; x++, y--)
> > printf("%d %d\n", x, y);
> > return 0;
> > }
> >
> > both of which produce the output:
> >
> > 1 7
> > 2 6
> > 3 5
> > 4 4
> 
> Ok and how is the comma operator usefully useful?

Beyond what I have written above, I have had little use for it in C and
less in Perl.  But I have no doubt that were you to ask in the
appropriate forum then someone could provide a compelling argument.

>Obviously, I could
> use it to create convoluted code, which is usually not my intention.
> 
> Consider with these examples that an expression like (1, 3) might
> unexpectedly evaluate to 3, and you start to think that you don't like
> things like
> 
> 
> sub s {
> my $a = 1;
> my $b = 3;
> 
>return ($a, $b);
> }
> 
> 
> anymore because you could, by mistake (or intentionally), write
> 
> 
> my $x = s;
> 
> 
> .  So let me re-phrase my original question to: How do you /safely/
> return arrays?
> 
> That means I need an error message for '$x = s;' because I'd write that
> only by mistake.

You could add to your subroutine:

  die "Only call in list context" unless wantarray;

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




Re: returning arrays

2016-01-24 Thread Paul Johnson
On Sun, Jan 24, 2016 at 05:44:14PM +0200, Shlomi Fish wrote:
> Hi lee,
> 
> On Sun, 24 Jan 2016 13:11:37 +0100
> lee <l...@yagibdah.de> wrote:
> 
> > Paul Johnson <p...@pjcj.net> writes:
> > >
> > > In scalar context the comma operator evaluates its left-hand side,
> > > throws it away and returns the right-hand side.  
> > 
> > What is the useful use for this operator?
> > 
> 
> Well, I believe its use was originally inherited from
> https://en.wikipedia.org/wiki/C_%28programming_language%29 where one can do
> something like:
> 
>   x = (y++, y+2);
> 
> In Perl 5 though it is preferable to use do { ... } instead:
> 
>   $x = do { $y++; $y+2; };

In both Perl and C the comma operator is probably most usually (deliberately)
seen in for statements:

#!/usr/bin/env perl

use strict;
use warnings;

for (my ($x, $y) = (1, 7); $x < 5; $x++, $y--) {
print "$x $y\n";
}

and

#include 

int main() {
int x, y;
for (x = 1, y = 7; x < 5; x++, y--)
printf("%d %d\n", x, y);
return 0;
}

both of which produce the output:

1 7
2 6
3 5
4 4

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




Re: uniq array creation

2015-11-29 Thread Paul Johnson
On Sun, Nov 29, 2015 at 03:30:53PM +0530, Pritish Pattanaik wrote:

> *Remove duplicate elelments from an array, It will maintain the original
> order*
> 
> __CODE__
> @array = qw(11 2 3 4 55 4 3 2);
> %hash = ();
> for(my $i=0;$i<=$#array;$i++){
> # store the position from array. use array element as key n position as
> value
> $hash{$array[$i]} = $i if ( ! exists $hash{$array[$i]} ) ;
> }
> @array = ();
> push(@array, $_) for (sort { $hash{$a} <=> $hash{$b}}  keys %hash ) ;
> print "Array:@array\n";
> __END__

Ooh, you're working far too hard!

  my @array = qw(11 2 3 4 55 4 3 2);
  my %seen;
  my @unique = grep !$seen{$_}++, @array;

This method is mentioned in the Perl Cookbook that was linked to earlier
in the thread.  But I doubt that link should have been online, so get
hold of a legal copy if this is useful to you.

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




Re: To use signatures or not to use?

2015-07-22 Thread Paul Johnson
On Wed, Jul 22, 2015 at 09:11:59PM +0200, Alex Becker wrote:
 Dear all,
 
 Perl now has subroutine signatures. However, they are marked as
 experimental feature.
 As I really like it, I'm always tempted to use it.
 On the other hand, I don't want to wast efforts to something I have to roll
 back.
 
 So, does anyone know the tendency if signatures are going to stay?
 
 I'm not looking for the standard do if you want to do it because
 TIMTOWTDI answer, it will not help me. I'm looking for rumors, or
 eventually an assessment from the people who did the feature?

Rumours, I can do.  Or perhaps start.

The signatures are nice.  I have used them on a shortish, ~1000 line
script and I have been mostly happy with them.  They save a lot of
boilerplate.

There was one small change I needed to make between 5.20 and 5.22
because the ordering with respect to prototypes changed.

I also noted that some error messages were less accurate than before,
though I haven't had time to narrow that down into a proper bug report.

My best guess is that the feature will remain and there will be small
changes before it is brought out of experimental status.

But there might be larger changes that wouldn't be completely backwards
compatible.  Or the feature may be completely scrapped.  That's the risk
you take with experimental features.  But there is certainly a will to
make this feature stick.

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




Re: Devel::Cover Use case

2015-07-22 Thread Paul Johnson
On Wed, Jul 22, 2015 at 08:17:18PM +0530, Piyush Verma wrote:
 Thanks Paul, this solved me some part of problem. I was using Devel::Cover
 in wrong place of .pl file.
 Now putting this on start of .pl script works for me but not completely.
 
 There are 20 .pm modules present in my project directory but in coverage
 report I am able to see only 6 .pm modules.
 I think for such input it is calling code from those 6 files. Do we have
 such option so that I get coverage of all 20 files in report, doesn't
 matter code has been called or not?

At the moment, no.  And in general there is no way to know which modules
might have been used but weren't in a language as dynamic as Perl.  So
anything that does get implemented (and it is on the TODO list) would be
a heuristic.

As a workaround, you could use all the modules in your project inside
one of your .pl files, or make a new one especially for that purpose.

If you carry on down this path though, you will soon end up reinventing
Perl's testing system.  Give serious thought to whether moving to a
standard test layout now wouldn't be a bad use of your time.

  Coverage without tests is hard though.

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




Re: Devel::Cover Use case

2015-07-22 Thread Paul Johnson
On Wed, Jul 22, 2015 at 04:56:12PM +0300, Shlomi Fish wrote:
 Hi Piyush,
 
 On Wed, 22 Jul 2015 19:08:20 +0530
 Piyush Verma 114piy...@gmail.com wrote:
 
  Hi,
  
  I want to collect code coverage data of my perl project which has a .pl
  file and .pm files.
  If I include this module in each file, I'm getting result only of .pl file.
  
  Can anyone suggest about the use case so that I can collect code coverage
  data of all files.
  
 
 Devel::Cover is for determining the *tests'* coverage. Do you have any t/*.t
 files? Please follow the synopsis of running Devel::Cover here:
 
 https://metacpan.org/pod/Devel::Cover#SYNOPSIS

You can use Devel::Cover without tests too.  After all, a test is just a
program, the same as your .pl files.

However, unless your program doesn't require any input, you will need to
run it multiple times with varying input to get proper coverage
information.

And the way to do this is also in the synopsis that Shlomi pointed you
to.

Coverage without tests is hard though.

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




Re: How to check the index positions of an array element in perl in case of multiple occurance

2015-06-16 Thread Paul Johnson
On Wed, Jun 17, 2015 at 07:01:06AM +1200, Kent Fredric wrote:
 On 17 June 2015 at 04:46, Vincent Lequertier s...@riseup.net wrote:
 
  #!/usr/bin/perl
  use strict;
  use warnings;
  open my $fh, '', 'text';
  my @words = split ' ', $fh;
  my @matchs;
  while (my ($index, $elem) = each @words) {
  if ($elem eq 'bspwrt') {
  push @matchs, $index++;
  }
  }
  $, = ' ';
  print @matchs;
 
 
 Obviously that strategy may be limited and slow if you need to execute
 the lookup  3 times for several queries.
 
 That can be avoided by using a hash representation of the data, etc:
 
 my @words = split ' ', $fh;
 my %match_index;
 while (my ($index, $elem) = each @words) {
 $match_index{ $elem } = [] unless exists $match_index{ $elem };
 push @{ $match_index{$elem} }, $index;
  }
 $, = '';
 print @{ $match_index{'bspwrt'} };
 print @{ $match_index{'tnbcch'} };
 
 etc.

Or, if you can't be bothered writing all that code:

$ perl -E 'push @{$pos{$_}}, $i++ for map split, STDIN; say $_: @{$pos{$_}} 
for @ARGV' bccd sdcch  data
bccd: 8 98 188 278 368 458 548 638 728 818 908 998 1088 1178 1268 1358
sdcch: 81 171 261 351 441 531 621 711 801 891 981 1071 1161 1251 1341 1431
$

Thinking about it, there is (at least) one bug in there.  But with the
data provided it may not be important.

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




Re: Debug when script WILL NOT RUN

2015-02-01 Thread Paul Johnson
On Sun, Feb 01, 2015 at 09:56:39AM -0500, Harry Putnam wrote:
 Robert Wohlfarth rbwohlfa...@gmail.com writes:
 
  On Sat, Jan 31, 2015 at 3:58 PM, Harry Putnam rea...@newsguy.com wrote:
 
  I have about 100 lines or so inside a File::Find:

You will likely help yourself out a lot with not only this problem but
others that you don't yet know about if you:

1. Split those 100 lines out into a separate subroutine.
2. Break that 100 line subroutine into at least four or five smaller
   subroutines.

find (
   sub {
   }, $tdir;
 );
 
  Why the semi-colon after $tdir? Seeing the parenthesis, I would guess that
  $tdir is a parameter to a function call. It should not have a semi-colon
  after it.
 
 I did notice that before posting, and try removing it, but received
 exactly the same error.  So something is still going on somewhere
 else.

That may well be, but the semi-colon is an error, and the only one we
can see in the code you have posted.

$ perl -ce 'find ( sub {}, $tdir; )'
syntax error at -e line 1, near $tdir;
-e had compilation errors
$ perl -ce 'find ( sub {}, $tdir )'
-e syntax OK

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




Re: ignoring bad Prompt argument Warning.

2014-12-29 Thread Paul Johnson
On Mon, Dec 29, 2014 at 04:46:50PM +0530, Uday Vernekar wrote:
 Hi All,
 
 I am doing Pattern matching of the expected string  if matched then pass
 the next command.my code works as expected but gives warning.
 
 can any body please Explain me why i am getting this at every match.
 
 ignoring bad Prompt argument '//': missing opening delimiter of match
 operator at linux_diagnostics.pl line 189

Guessing:

You are using Net::Telnet and your prompt should be // rather than
'//'

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




Re: Make scalar tainted

2014-12-06 Thread Paul Johnson
On Sat, Dec 06, 2014 at 05:48:36PM +0300, Артём Варнайский wrote:
  Hello again! 
 Prompt me please elegant and 'vanilla' way to taint some scalar. My vars lost 
 their taint when I just split external string with regexp on param/val pairs, 
 without checking them for correctness.
 And What do you say about this:
 $foo.=substr($ENV{PATH},0,0); #$foo tainted if $ENV{PATH} is tainted
 Thank, and sorry for my runglish :)

I'm not sure whether there is a more elegant or more vanilla way to do
that.  Appending the zero length substr is also the way it is done in
the perl core.

You would normally take the substr from the original string before
splitting it, unless you wanted to taint $foo even if its source wasn't
tainted.

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




Re: Problem with regex

2014-10-06 Thread Paul Johnson
On Mon, Oct 06, 2014 at 09:34:15PM +0530, punit jain wrote:
 Hi,
 
 I have a regex problem. My current working program is like this :-
 
 
 #!/usr/bin/perl
 
 *$str='ldap:///uid=user1,ou=People,o=test.com
 http://test.com,mailto:us...@test.com
 us...@test.com,ldap:///uid=user2,ou=People,o=test.com
 http://test.com,ldap:///uid=user3,ou=People,o=test.com
 http://test.com';*
 
 while($str=~/^(?:mailto:|ldap:\/\/\/)(.*?),(?:mailto:|ldap:\/\/\/)/){
 print first = $1.\n;
 
# Process user for send email
  ProcessUser($first);
 if($str =~ /^(?:ldap:\/\/\/.+?|mailto:.+?),(ldap.*|mailto.*)/) {
 print remain = $1.\n;
 $str=$1;
 }
 }
 
 However when I have input string as :-
 
 'ldap:///uid=user1,ou=People,o=test.com,a...@test.com*,t...@test.com
 t...@test.com,r...@test.com r...@test.com*,ldap:///uid=user2,ou=People,o=
 test.com,ldap:///uid=user3,ou=People,o=test.com'
 
 it breaks. I tried multiple regex, however could not get it working.
 Any clue on regex changes to accomodate this ?

First, pay attention to what Kent has written.

But to get to specific problems:

I think you are misunderstanding how to use while with a regex match.
You don't need to replace the string with the unmatched part - perl will
handle that sort of low-level messing about for you.  You just need to
use the /g flag and the next match will start off where the previous one
finished.

To get this to work, don't anchor your regex to the start of the string,
or you'll only get one match.

Also, your check that you are only matching up to the next mailto or
ldap section needs to ensure that it doesn't consume that part of the
string.  This is done by using a positive lookahead assertion (?=).

Then you need to allow for matching the last part of the string, which
will not be followed by another part to match.

Finally, it looks like you have newline characters in your input.  If
that is the case then you need to add the /s flag so that . will also
match a newline.

And for style, you can pull out the duplicated parts of the regex and
use another delimiter to avoid Leaning Toothpick Syndrome.

Putting it together you get:

my $start = qr!mailto:|ldap:///!;
while ($str =~ /$start(.*?)(?=,$start|$)/sg) {
print first = $1\n;
}

Or you could avoid the messing about with the while condition and use
split:

say for split $start, $str;

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




Re: Difference between list and arrays.

2014-09-16 Thread Paul Johnson
On Tue, Sep 16, 2014 at 12:12:05PM -0500, Andy Bach wrote:

The other thing to think
 about is context - the LHS of the assigning = determines how the RHS
 list is treated.

So far, so good.  Well, almost ...

  In scalar context
 my $one_var = (1, 2, 3);
 
 the list returns its element count

But, I'm afraid, this is a common misconception.  The fact is that there
is no such thing as a list in scalar context.

So what, then, is the construct above?

You were correct in saying that it (whatever it is) is evaluated in
scalar context.  In scalar context, the parentheses () are used simply
for precedence.  The commas are operators.

The comma operator evaluates its LHS, throws it away, evaluates its RHS
and returns that.  The comma operator is left associative (see perlop).

So the result of evaluating the RHS (1, 2, 3) is:

  (1, 2, 3) - ((1, 2), 3) - (2, 3) - 3

And this is why $one_var gets the value 3 - not because there are three
elements in a list on the RHS.

This all becomes easier to understand if you don't use the values 1, 2
and 3 :)

See also perldoc -q 'difference between a list and an array'

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




Re: Difference between list and arrays.

2014-09-16 Thread Paul Johnson
On Tue, Sep 16, 2014 at 02:43:28PM -0500, Andy Bach wrote:
 On Tue, Sep 16, 2014 at 1:45 PM, Paul Johnson p...@pjcj.net wrote:
 
  The comma operator evaluates its LHS, throws it away, evaluates its RHS
  and returns that.  The comma operator is left associative (see perlop).
 
  So the result of evaluating the RHS (1, 2, 3) is:
 
(1, 2, 3) - ((1, 2), 3) - (2, 3) - 3
 
  And this is why $one_var gets the value 3 - not because there are three
  elements in a list on the RHS.
 
 
 D'oh! Somewhere I knew something like that, that a list in scalar context
 ends up assigning the last element to the scalar - though I'm pretty sure I
 didn't know it was due to the comma operator.  I should have though, as:
 # perl -we 'my $one = (3,2,1); print $one\n'
 Useless use of a constant in void context at -e line 1.
 Useless use of a constant in void context at -e line 1.
 1
 
 Those two warnings are the dropping of the result of the first to comma
 operations.  Hmm, so this works [1]:
 
 my $second_elem = ('first', 'second', 'third')[1];
 $second_elem eq 'second';
 
 because ...???

Aha, turning it up a notch ;-)

In this case, ('first', 'second', 'third') is actually a list.  Why?
Because the [1] indexes into it.  The [1] forces it into list context.
You can't index into something that is in scalar context.

 $ perldoc -q 'difference between a list and an array'

 Well, I would've found that if perldoc would've found this for queries on
 scalar context, list context, array context or, for that matter,
 plain old context but ..

Yes, I think the search is only on the title of the section, not its
contents.  Which can mean that you need to find what you are looking for
in order to know how to find it.

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




Re: returning arrays

2014-09-09 Thread Paul Johnson
On Tue, Sep 09, 2014 at 03:12:00PM -0700, Jim Gibson wrote:
 
 On Sep 9, 2014, at 2:57 PM, Shawn H Corey wrote:
 
  On Tue, 09 Sep 2014 23:09:52 +0200
  lee l...@yun.yagibdah.de wrote:
  
  my $i = 1;
  my $f = 2.5;
  my $s = 'string';
  my $list = (1, 2, 3);
  
  No, the count of items in the list gets stored in $list: $list == 3
 
 Unless the thing on the right-hand-side of the assignment is a 'list' and not 
 an 'array'. This is the one place I can think of where the distinction 
 between 'list' and 'array' actually makes a difference.
 
 Compare this:
 
   my $n = ( 4, 5, 6 );
 
 with this:
 
   my @a = ( 4, 5, 6 );
   my $n = @a;
 
 (Don't try this with the list ( 1, 2, 3 ), either!)


There's a little bit of confusion here.  When you write
  my $list = (1, 2, 3);
there isn't actually a list anywhere in that statement, appearances to
the contrary notwithstanding.

To understand what is happening, notice first that the assignment is to
a scalar (confusingly named $list in this case).  That means that the
assignment is in scalar context and so the expression (1, 2, 3) is
evaluated in scalar context.

In scalar context the parentheses () are used for controlling
precedence.  The values 1, 2 and 3 are just scalar values.  And the
commas are comma operators in scalar context.

In scalar context the comma operator evaluates its left-hand side,
throws it away and returns the right-hand side.

This means that the value of (1, 2, 3) in scalar context is 3, and this
is what gets assigned to $list.

What is not happening at all is the creation of a list of numbers and a
calculation of its length.

See also perldoc -q 'difference between a list and an array'

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




Re: return the list content

2014-08-18 Thread Paul Johnson
On Mon, Aug 18, 2014 at 04:17:53PM +0800, Ken Peng wrote:
 Hello,
 
 sub myfunc {
   my @x=(1,2,3);
   return \@x;
 }
 
 # or,
 
 sub myfunc {
   my @x=(1,2,3);
   return [@x];
 }
 
 which one is the better way to return the list content? And if the
 method is an instance method?

Functionally, the two are identical.  The first is returning a reference
to the array you have created.  The second is returning a reference to a
new array created from a (shallow) copy of the array you have created.

The fist is to be preferred in my opinion, because it does exactly what
you want.  The second does extra work and might cause someone to wonder
why you haven't just returned a reference to the array.

The second version is necessary when the array might persist between
subroutine calls and you effectively need to return a copy.

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




Re: Please check my logic

2014-07-21 Thread Paul Johnson
On Mon, Jul 21, 2014 at 10:05:29PM +, Danny Wong (dannwong) wrote:

 Do it the perl way, hash it.

Or do it the unix way:

 $ sort -u filename

The -u means unique.

You also have some lines that differ only in case, so you might prefer:

 $ sort -uf filename

The -f means fold, which ignores lines which differ only in case.

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




Re: Apologetic request for simple one-off script

2014-07-13 Thread Paul Johnson
On Sun, Jul 13, 2014 at 04:43:41PM -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.

Quick and dirty solution:

$ perl -nale 'print $F[-1]'  original_file.txt  just_email.txt

perldoc perlrun if you want to see why that works.  Take a look at the
-a option.  The -1 index into @F says use the last element of the array.

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




Re: Apologetic request for simple one-off script

2014-07-13 Thread Paul Johnson
On Sun, Jul 13, 2014 at 06:44:18PM -0400, ESChamp wrote:
 Paul Johnson has written on 7/13/2014 5:00 PM:
  perl -nale 'print $F[-1]'  original_file.txt  just_email.txt
 
 e:\Docs\perl -nale 'print $F[-1]'  4sam.txt  just_email.txt
 Can't find string terminator ' anywhere before EOF at -e line 1.

Ah, you're on Windows.  I think you need to use  quotes:

perl -nale print $F[-1]  4sam.txt  just_email.txt

but if I'm wrong, someone who knows more about Windows should be able to
say.

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




Re: List-AllUtils-0.07

2014-03-18 Thread Paul Johnson
On Tue, Mar 18, 2014 at 12:30:12PM +0200, Yosef Levy wrote:
 hello All,
 
 Trying install List-AllUtils-0.07.
 Need dependencies:
 
 
 C:\yosef\cpan\List-AllUtils-0.07perl Makefile.PL
 Set up gcc environment - gcc.exe (rubenvb-4.5.4) 4.5.4
 Checking if your kit is complete...
 Looks good
 Warning: prerequisite List::Util 1.31 not found. We have 1.27. 
 Warning: prerequisite Test::Warnings 0 not found.
 Writing Makefile for List::AllUtils
 Writing MYMETA.yml and MYMETA.json
 
 I can't find: List::Util 1.31 in CPAN distribute.

As I write this, the latest version seems to be 1.38:
  https://metacpan.org/pod/List::Util

Depending on the distribution you are using you might have a tool to
automate the process of recursively installing dependencies.

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




Re: Fail to match mixed quote pattern

2014-03-14 Thread Paul Johnson
On Fri, Mar 14, 2014 at 10:26:39PM +1100, Alex Chiang wrote:
 Hi all, 
 
 I tried to extract string from perl script, so I wrote following script: 
 
   5# matching string 
   6 # sting start with ' or .
   7 # assume string is on same line.
   8 #
   9 my $pattern = '(\|\').*\1';
  10 my @array;
  11 open (my $file, , str);
  12 while (my $line = $file) {.
  13 if($line =~ /$pattern/) {

 while ($line =~ /$pattern/g) {

  14 push (@array, $);
  15 }
  16 }
  17
  18 for (@array) { print $_\n; }
 
 
 the str sample file : 
 ---
 'okay i know now'
 from time to time 'let us go'
 this is awkward 
 
 
 I would expect the output match three of them, however, it only matches: 
 --
 'okay i know now'
 from time to time
 
 ---
 leaving 'let us go' unmatched.
 
 I don't know how to describe this problem, Can anyone help me with this ? 

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




Re: Delete key-value pair in perl

2014-03-09 Thread Paul Johnson
On Sun, Mar 09, 2014 at 04:40:54PM +1100, Alex Chiang wrote:
 Hi all, 
 
 I'm a perl beginner, and when I tries to practice delete built-in, I
 find it's not working as I supposed to. 
 
  44   my %employ_list = (e10220 = Du, e10250 = Vic); 
  45   # iterate thru with each
  46   while ( my ($k, $v) = each %employ_list ) {
  47 print  before: key:$k = value:$v \n;
  48 if ($k eq e10220) { delete $employ_list{$k}; }
  49 $employ_list{$k} = undef;
  50 print after: key:$k = value:$v \n;
  51   }

 the delete built-in doesn't work, the output of print after is still
 the original dict.

That is because you are not printing out the value in the hash, you are
printing $v.  $v is not an alias to the hash value, it is a copy of the
original value in the hash.  $v doesn't change when you delete the value
from the hash.

On Sun, Mar 09, 2014 at 11:00:29AM +0200, Shlomi Fish wrote:

 In addition to what Uri said, you should not delete a key out of a hash (or
 add keys) while iterating over it using each(), because this may confuse Perl:

This is true in general, but the documentation explicitly states that it
is safe to use delete on the key most recently returned from each, as
Alex is doing here.  This is good because, as we see here, it can
reasonably be expected to work.

  perldoc -f each

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




Re: match not matching

2014-03-01 Thread Paul Johnson
On Fri, Feb 28, 2014 at 11:13:05PM -0600, Bill McCormick wrote:
 Can somebody help me understand this? Given this loop, and the
 logged output following ...
 
 my $found;
 for( @$products ) {;
   $found = $$_ =~ m|$project|;

I think you might have meant:
$found = $project =~ m|$$_|;

   $dump = Data::Dumper-Dump([$_, $project, $$_, $found]);
   $logger-trace(qq(dump=$dump));
 }
 
 I can't explain why $found is not true on the 3rd pass. Does this
 have something to do with the way I'm dereferencing the blessed
 object?

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




Re: file edit

2014-02-27 Thread Paul Johnson
On Wed, Feb 26, 2014 at 03:59:40PM +, jet speed wrote:
 Chaps,
 
 Any quick one liner code in perl were i can get rid off the before and
 after wwn each line for file as below
 
 file
 ---
 device name lz_09_red_e10 vsan 200
  * fcid 0xef0013  [pwwn 50:00:00:00:99:00:66:7a]
  * fcid 0xegg015 [pwwn 10:00:00:55:99:a8:d9:c4] [ pe-tgh10-hostb]
 device name lz_09_blue_e10 vsan 200
 * fcid 0xef0013 [pwwn 50:00:00:00:99:00:66:7b] [ pe-tgh10-hostc]
 * fcid 0xegg015 [pwwn 10:00:00:55:99:a8:d9:c9]

perl -ne 'print /\b((?:[a-f0-9]{2}:){7}[a-f0-9]{2})\b/ ? $1\n : $_'  file

You should probably try and understand it before trusting it.

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




Re: Not mapping into a hash

2014-02-13 Thread Paul Johnson
On Thu, Feb 13, 2014 at 06:04:40AM -0500, shawn wilson wrote:
 I know I'm overlooking something real simple here but I just don't see it:

 And the method ('HERE' marking the branch that is having issues):

Are you sure?  I think you might not be getting that far.

 sub _list_set
 {
   my ($self, $params) = @_;
   my $name = $params-{name};
   return undef unless (exists($self-{set}{$name}{list}) and
 ref($self-{set}{$name}{list}) eq 'ARRAY');
   my @return;
 
   my %hDirection;
   if (ref(\$params-{hDirection}) eq 'SCALAR')

You probably don't want to be taking that reference here because that
will always be a scalar.  So I think you are executing this branch of
the conditional.

Your test should probably be if (!ref $params-{hDirection})

   {
 %hDirection = map {$_ = 1} split( , $params-{direction});
   }
   elsif (ref($params-{direction}) eq 'ARRAY')
   {
 %hDirection = map {$_ = 1} @{$params-{direction}};  # -- HERE
   }
   else

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




Re: Test::More is_deeply failed data

2014-02-11 Thread Paul Johnson
On Tue, Feb 11, 2014 at 01:17:15PM -0500, shawn wilson wrote:
 Is there a way to see the data difference when is_deeply fails? proove
 -d doesn't give me the structure that fails. I end up 'print STDERR
 one  . Dumper($foo); print STDERR 'two  . Dumper($bar);' which just
 doesn't seem like the right thing to do to figure out why a test is
 failing?

Perhaps you are looking for Test::Differences ?

https://metacpan.org/pod/Test::Differences

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




Re: package block

2014-02-09 Thread Paul Johnson
On Wed, Feb 05, 2014 at 10:51:56AM +0100, Manfred Lotz wrote:
 Hi all,
 Perl 5.14 allows package blocks.
 
 Assume i have a file Hello.pm
 
 package Hello; {
   ...
 
   1;
 }
 # or better here
 # 1;
 
 
 My question is: Would I put 1; inside the { } or should I put it
 outside the { }.

If you are programming nicely, that is you only have the single package
in the file, then it doesn't matter.  However, it is the file that needs
to return true and so it is better to put the true value as the very
last statement in the file.

Also, you'll want to delete the semicolon on the package declaration
line.  What you have currently is an old-style package declaration and
then an ordinary block, meaning that anything after the block is also in
package Hello.

Finally, 1 is a boring value to return.  Be creative!
See http://returnvalues.useperl.at/values.html

-- 
Paul Johnson - p...@pjcj.net

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




regex headache

2014-02-03 Thread Paul Fontenot
Hi, I am attempting to write a regex but it is giving me a headache.

I have two log entries

1. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR
[org.apache.commons.logging.impl.Log4JLogger]
2. Feb  3 12:54:28 cdrtva01a1005 [12: 54:27,532] ERROR [STDERR]

I am using the following
^\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\w+\s+\[\d{1,2}:\s+\d{1,2}:\d{1,
2},\d{3}\]\s+\w+\s+\[[a-zA-Z0-9.]\]

My problem is this greedy little '.' - I need to just be a period. How do I
match #1 and not match #2?


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




Re: Using Files in Chronological Order

2014-01-27 Thread Paul Johnson
On Fri, Jan 24, 2014 at 06:26:58AM -0600, Martin G. McCormick wrote:
   This is a perl philosophy question. I need to look for
 some files, newest first. If I use the glob directive in perl, I
 can fill an array with all the file names that match the pattern
 but they aren't sorted in to chronological order. I found a
 perlmonks posting in which the same question was asked and the
 suggestion was to use something like
 
 @files = `ls -t filenames*`
 
 Which, to me, is a perfectly good solution in the unix world. It
 probably isn't portable to, say, Windows but it doesn't need to
 be in this case.
 
   I have read perl instructional documentation that warns
 about using system(commands) and shell commands so my question
 is, what is wrong with that?
 
   The only thing I can see is that it is not portable and
 that since the commands are outside of perl, the results might
 change one day. It probably also takes extra CPU cycles but
 unless one is doing DSP, who's counting?

This is the correct attitude.  As a programmer it is your job to
determine, in each instance, where the line lies between beauty,
elegance, maintainability, portability, speed, just getting your job
done, and a whole bunch of other considerations.

In some cases shelling out to ls is exactly the correct thing to do, and
when you have decided where your line lies based on your understanding
of your requirements, don't let anyone without that understanding tell
you otherwise.

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




Re: regexp as hash value?

2014-01-25 Thread Paul Johnson
On Sat, Jan 25, 2014 at 03:51:53PM +0100, Luca Ferrari wrote:
 Hi,
 I'm just wondering if it is possible to place a regexp as a value into
 an hash so to use it later as something like:
 
 my $string =~ $hash_ref-{ $key };
 
 Is it possible? Should I take into account something special?

Yes, this is possible.  You need to use qr// to construct your RE:

$ perl -E '$h = { a = qr/y/ }; say $_ =~ $h-{a} for qw(x y z)'

1

$

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




Re: regexp as hash value?

2014-01-25 Thread Paul Johnson
On Sat, Jan 25, 2014 at 06:41:00PM +0100, Luca Ferrari wrote:
 On Sat, Jan 25, 2014 at 4:12 PM, Paul Johnson p...@pjcj.net wrote:
 
  $ perl -E '$h = { a = qr/y/ }; say $_ =~ $h-{a} for qw(x y z)'
 
 Thanks, but then another doubt: having a look at
 http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators I dont
 understand how I can use the regexp for substitution, that is s/// as
 an hash value. The following is not working:
 my $hash = { q/regexp/ = qr/s,from,to,/ };

You won't be able to do the full substitution this way, but you can use
the RE in the substitution:

$ perl -E '$h = { a = qr/y/ }; say $_ =~ s/$h-{a}/p/r for qw(x y z)'
x
p
z
$

If the replacement text is different for each substitution then you may
be better served storing anonymous subs in your hash:

$ perl -E '$h = { a = sub { s/y/p/r } }; say $h-{a}-() for qw(x y z)'
x
p
z
$

Good luck.

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




Re: Problem setting $= (Format Lines Per Page Special Variable)

2013-11-07 Thread Paul Johnson
On Thu, Nov 07, 2013 at 11:44:27AM +0800, Shaji Kalidasan wrote:
 Dear Perlers,
 
 I am facing problems while setting the $= special variable. Even
 though I set it to 10 it takes the default value which is 60. In line
 number 31, I set the variable to a value of 10. It is not printing the
 page number for every 10 lines
 
 Please guide me where I am going wrong.

The problem is that you need to have your format selected when you
assign to $= so perl knows to which format you are referring:

my $fh = select REPORT;
$= = 10;
select $fh;

Alternatively, you could call a method on the FileHandle:

REPORT-format_lines_per_page(10);

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




Re: hello from a newbie

2013-10-26 Thread Paul Johnson
On Sat, Oct 26, 2013 at 04:47:47PM +0530, Mayuresh Kathe wrote:

 btw, most people and web pages don't mention it, but, mastering
 algorithms with perl is also supposedly a good book, though only a very
 old edition is available.

It is, indeed, and excellent book.  And it doesn't really matter if the
edition is old.  The reason for that is that it isn't a book to use to
learn Perl - the preface explicitly states that.  It is a book from
which to learn algorithms if you already know Perl.

So if that's you, reading Mastering Algorithms with Perl will make you a
better developer.

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




Re: Problem rewinding the __DATA__ filehandle

2013-10-25 Thread Paul Johnson
On Fri, Oct 25, 2013 at 05:14:23PM -0700, Jim Gibson wrote:
 
 On Oct 25, 2013, at 4:46 PM, Shaji Kalidasan wrote:
 
  Dear Perlers,
  
  I am trying to print the matching lines using __DATA__ filehandle and for 
  the very first time it prints the desired lines, but as soon as I rewind it 
  using seek, it is printing lines from the very beginning which is not the 
  desired result (as it prints from the start). I want to rewind only the 
  __DATA__ part.
 
 As you have discovered, the special file handle DATA is opened to the source 
 file for your program. When the compiler encounters either of the following 
 lines in your source file:
 
 __END__
 __DATA__
 
 
 it stops reading the source file and leaves the file handle open at that 
 point. The first time you read the DATA file handle, you get the line after 
 the '__DATA__' line. But when you rewind the file handle, it is positioned at 
 the beginning of the file, and you get to read your program.
 
 You have two choices (at least):
 
 1. Save the position of the file handle when you enter the program:
 
   my $data_pos = tell(DATA);
 
 Then reposition the file to this point instead of at the beginning of the 
 file:
 
   seek( DATA, $data_pos, 0 );
 
 
 2. After rewinding the file to the beginning with seek(DATA,0,0), read the 
 file until you encounter the '__DATA__' line. Then start reading the data 
 lines.

The first choice is the correct one.

Ideally, you should use the SEEK_SET constant from Fcntl.  perldoc Fcntl
for details.

And for your third approach, you need C $. = 0; 

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




Re: checking for file existence

2013-09-25 Thread Paul Johnson
On Wed, Sep 25, 2013 at 04:38:22PM -0300, RENATO AUGUSTO CORREA DOS SANTOS 
wrote:
 Hi, all.
 
 I am a very begginer in PERL starting today in this mailing list! :-) I
 will try to work especially with Bioinformatics.

Welcome!

 I am trying to verify the existence of a file in PERL; however, is seems
 not to work. It always returns The file $file_seqs does not exist!!!.
 
 Do you know where I am making a mistake?

I don't know.  How are you calling your program?  Because it seems to
work correctly for me.

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




Re: negate, !!$a, $a!!, flip-flop

2013-09-10 Thread Paul Johnson
On Tue, Sep 10, 2013 at 08:29:00AM -0400, Shawn H Corey wrote:
 On Mon, 09 Sep 2013 11:00:31 +0200
 Hans Ginzel h...@matfyz.cz wrote:
 
  Is there a shorter way to write $a = ! $a, please?
 
 Why?

Because it's the sort of thing one might expect Perl to provide.  I know
that I have thought that before.

^= 1 is close, but you really want something that works on truthiness
rather than bits, and xor= doesn't exist.

However, since people will tell you off for using $a anyway, I suggest
using $| instead, and then the operation you are looking for is $|--

$ perl -E 'say $|-- for 1 .. 5'
0
1
0
1
0

[ If it's not obvious, my tongue was in my cheek for half of this post. ]

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




Re: how to retrieve the keys from hash in the sequence in which data is inserted

2013-04-23 Thread Paul Johnson
On Tue, Apr 23, 2013 at 04:56:55PM +0530, kavita kulkarni wrote:
 Hello All,
 
 Can you help me in finding the most time effective way to retrieve the
 key-values from hash in the same sequence in which data is inserted into
 the hash.

Hashes are unordered, so you cannot directly do what you are asking.

Time effective for whom?  If it's for you, take a look at
https://metacpan.org/module/Tie::IxHash

If you're talking about execution speed, go with the simple solution
first and time and profile the solution before looking to optimise it.
You may find that the simple solution is good enough.

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




Re: Any alternative for substr() function

2013-04-12 Thread Paul Johnson
On Wed, Apr 10, 2013 at 03:46:00PM +0530, kavita kulkarni wrote:
 Hi All,

Hello.

 I want to extract certain char sets (e.g. char 4 to 9, char 11 to 14 etc.)
 from each line of my file and store them in separate variables for further
 processing.
 My file size can vary from thousand to millions of lines.
 If I use perl in-built function substr() to data extraction, it has huge
 impact on performance.

Compared to what?

 Is there any alternative for this?

Perhaps unpack() or regular expressions, but I doubt either would be
much faster, if at all.

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




Re: .profile not being read

2013-02-17 Thread Paul Anderson
Odds are good that the original poster needs to use .bash_profile instead. 

Sent from my iPhone

On 2013-02-18, at 2:24 AM, Luca Ferrari fluca1...@infinito.it wrote:

 I suspect this has something to do with the PATH variable and alike.
 And it could have been set up at system wide level, for instance on
 /etc/profile.
 
 Luca
 
 -- 
 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: obfuscating code

2013-02-13 Thread Paul Johnson
On Tue, Feb 12, 2013 at 03:18:51PM -0800, John SJ Anderson wrote:
 
 On Feb 12, 2013, at 3:05 PM, Kevin Walzer k...@codebykevin.com wrote:
 
  Does Perl have the equivalent of Python bytecode files, i.e. pyc,
  that are obfuscated? If not, the OP's options may be limited.
 
 No such critter in Perl.

Whilst that's true to a first, second and maybe even a third
approximation, it's perhaps not absolutely correct:

$ echo 'sub X { print 42 } 1'  X.pm
$ perl5.8.9 -MX -eX
42
$ perl5.8.9 -MO=Bytecode,-H -MX -e1  XX.pmc
-e syntax OK
$ perl5.8.9 -MXX -eX
42
$ od -x XX.pmc
000 2123 2f20 7375 2f72 6f6c 6163 2f6c 6b70
020 2f67 6570 6c72 642f 6665 7561 746c 702f
040 7265 2d6c 2e35 2e38 2f39 6962 2f6e 6570
060 6c72 2e35 2e38 0a39 7375 2065 7942 6574
100 6f4c 6461 7265 3020 302e 3b36 500a 424c
120 7843 3638 365f 2d34 696c 756e 0078 2e30
140 3630 0800  0800  0c00 5938 0161
160    6400 650d 0c40 5760 6d50
...
0001440  0939  3b00 0010 7e3a 0003 8900
0001460 000e  
0001465


But, be that as it may, even this won't stop someone who knows what they
are doing being able to recover your source code, after a fashion.

In practice, it's all about degree.  Given that someone can run your
code locally, and that if they have sufficient time and skill they will
be able to reverse engineer your code to a greater or lesser extent
(which is true for all programs in all languages), often you just want to
hide your code from a curious observer, or to make it not worth
someone's while to put in that effort.

Modules such as Acme::Bleach have already been mentioned.  One approach
I have taken in the past is to embed perl inside a C program.  I have
encrypted the Perl source and added that as data in the C program.  The
C program decrypts the source and calls the embedded perl to execute it.
You also need to do something clever so that use and friends look at
the text in the C program first.  Then you distribute the executable.
That's probably more than sufficient for most purposes.

It's probably also more than sufficient for this list.

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




Re: obfuscating code

2013-02-12 Thread Paul Anderson
Simple, step by step directions:

1. Obtain large gun. 
2. Load with ammunition.
3. Fire squarely into foot.
4. Reload if necessary and repeat.



Sent from my iPhone

On 2013-02-12, at 12:01 PM, Rajeev Prasad rp.ne...@yahoo.com wrote:

 freinds,
 
 what is the advice just for obfuscating code? platform is solaris.
 
 ty.

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




Re: Abbreviating ordinals only in the middle of an address

2013-01-28 Thread Paul Anderson
I'm thinking {2,}\w to match two or more words after north. 

Sent from my iPhone

On 2013-01-28, at 2:57 PM, Angela Barone ang...@italian-getaways.com wrote:

 Hello,
 
I'm trying to abbreviate ordinals(?) that occur only in the middle of an 
 address and I'm having a problem.  The line below works:
 
 $test_data =~ s/(\S) North (\S)/$1 N. $2/i;
 
 however, if the address is something like 901 North St., it abbreviates that 
 as well.  I'm wanting it to work only on an address like 
 53 North Benson St. - 53 N. Benson St.
 
Is there a way to know whether or not 'North' is a street name as opposed 
 to a direction, or am I asking too much?  I was thinking of counting the 
 number of words between North' and 'St.' (and if it's zero then don't do 
 it), but I wouldn't know how to do that or even if that's the best way to do 
 it.
 
 Thanks!
 Angela
 -- 
 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: Regex issue

2013-01-03 Thread Paul Johnson
On Thu, Jan 03, 2013 at 03:53:20PM +0530, punit jain wrote:
 Hi,
 
 I am facing issues in parsing using Regex. The problem definition is as
 below : -

 I want to parse it in such a way that  all data with BEGIN and END goes in
 one file and BEGINDL and ENDDL goes in other with kind of processing I want
 to so.
 
 I am using below code but doesnot work : -

What doesn't work?  It seems fine to me.

 #!/usr/bin/perl
 my $file=shift;
 open( FH , $file ) or die(open failed: $!\n);
 open ($fh1, /tmp/a);
 open ($fh2, /tmp/b);
 my $check=0;

You probably want $check = 2 here.

 while (FH) {
 #next unless /BEGIN/ .. /END/ || /BEGINDL/ .. /ENDDL/ || eof;
 if($_ =~ /BEGIN$/ || ($check == 0) ) {
 print $fh1 $_;
 $check = 0;
 if($_ =~ /END$/) {
 $check = 2;
 }
 }elsif($_ =~ /BEGINDL/ || ($check == 1)) {
 print $fh2 $_;
 $check = 1;
 if($_ =~ /ENDDL/) {
 $check = 2;
 }
 }
 next unless($check == 2);
 }
 
 Any better suggestion ?

Depends on how you define better, but perhaps

 $ perl -ne 'print if /BEGIN$/ .. /END$/'  file  /tmp/a
 $ perl -ne 'print if /BEGINDL$/ .. /ENDDL$/'  file  /tmp/b

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




Re: substitution: interpolate capture buffer into variable?

2012-12-26 Thread Paul Johnson
On Wed, Dec 26, 2012 at 04:10:06PM +0100, gator...@yahoo.de wrote:
 Hi,
 
 I would like to store regular expressions and substitution strings in
 a hash variable. If a given string matches any of the stored patterns,
 the corresponding substitution should be applied. What originally looked
 trivial turned out to be quite a challenge, particularly if the
 substitution string contains references to capture buffers.
 
 Here's a minimal example:
 
 my $rx=qr/^_(.*)/;
 my $r='a_$1';
 my $s=_bla_fasel_blub;
 if ($s=~ /$rx/) { # pattern matches, apply substitution if you can
 # hardcoded, it's trivial:
 # $s =~ s/$rx/a_$1/;
 # but how to interpolate the capture buffer? Not like this:
 # eval '$s=$r';
 # eval { $s=$r };
 # $s =~ s/$rx/$r/e;
 }
 
 Can anybody think of a straightforward way to do this?

This is a situation where string eval is warranted:

eval \$s =~ s/\$rx/$r/;

Three points:
 - make sure you trust your input
 - be sure to check $@
 - there's no need to check if the pattern matches first, just attempt
   the substitution

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




Re: grouping in regex

2012-12-25 Thread Paul Johnson
On Mon, Dec 24, 2012 at 08:01:11PM +, Rob Dixon wrote:
 On 24/12/2012 13:08, Paul Johnson wrote:
 On Sun, Dec 23, 2012 at 06:57:38PM +0530, punit jain wrote:
 I am seeing which lines have both POP and Webmail as below :-
 
 if( $line =~ /AccessModes\s*=\s*.*(WebMail)*.*(POP).*(WebMail)*.*/ ) {
  if(defined $2) {
  print $2 $1.$line.\n;
  }
  }

 Hi Paul
 
 I think
 
 why, having specified what you are searching for, you then ask what was found
 
 could be expressed better, or at least needs an explanation.

Yes, you're probably correct.

My point was that the captured expressions are constants: WebMail,
POP and WebMail again.  So if $1, $2 and $3 are defined then you
already know what their values are.

So, once you know that the regular expression has matched, your only
interest in $1, $2 and $3 is to see whether $1 and $3 are defined which
will tell you the order of the items on the line.  My guess is that this
is not important to know.

So the specified what you are searching for part is the constant
captured expressions, and the ask what was found bit is examining $1,
$2 and $3.

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




Re: grouping in regex

2012-12-24 Thread Paul Johnson
On Sun, Dec 23, 2012 at 06:57:38PM +0530, punit jain wrote:
 Hi,
 
 I am doing grouping but seeing some weird behavior :-
 
 the strings in a file are like :-
 AccessModes =
 (18,Mail,POP,IMAP,PWD,WebMail,WebSite,Relay,Mobile,FTP,MAPI,TLS,LDAP,WebCAL);
 
 ...
 .
 multiple lines
 
 I am seeing which lines have both POP and Webmail as below :-
 
 if( $line =~ /AccessModes\s*=\s*.*(WebMail)*.*(POP).*(WebMail)*.*/ ) {
 if(defined $2) {
 print $2 $1.$line.\n;
 }
 }
 
 
 However I get these below :-
 
   POPUse of uninitialized value in concatenation (.) or string at
 test.plline 283, GEN85 line 2.
   POPUse of uninitialized value in concatenation (.) or string at
 test.plline 283, GEN86 line 2.
   POPUse of uninitialized value in concatenation (.) or string at
 test.plline 283, GEN87 line 2.
 
 Any clue why ?
 
 Regards.

It's unclear to me why, having specified what you are searching for, you
then ask what was found.  The only reason I can see for doing that would
be to find out in which order you found the items.

In any case, the easiest way to find out whether two substrings appear
in the same same string is to program the way you define the problem:

if (/WebMail/  /POP/) { ... }

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




Re: Regex help

2012-12-22 Thread Paul Johnson
On Sat, Dec 22, 2012 at 04:45:21PM +0530, punit jain wrote:
 Hi,
 
 I have a file like below : -
 
 BEGIN:VCARD
 VERSION:2.1
 EMAIL:te...@test.com
 FN:test1
 REV:20101116T030833Z
 UID:644938456.1419.
 END:VCARD
 
 From (S___-0003) Tue Nov 16 03:10:15 2010
 content-class: urn:content-classes:person
 Date: Tue, 16 Nov 2010 11:10:15 +0800
 Subject: test
 Message-ID: 644938507.1420
 MIME-Version: 1.0
 Content-Type: text/x-vcard; charset=utf-8
 
 BEGIN:VCARD
 VERSION:2.1
 EMAIL:te...@test.com
 FN:test2
 REV:20101116T031015Z
 UID:644938507.1420
 END:VCARD
 
 
 
 My requirement is to get all text between BEGIN:VCARD and END:VCARD and all
 the instances. So o/p should be :-
 
 BEGIN:VCARD
 VERSION:2.1
 EMAIL:te...@test.com
 FN:test1
 REV:20101116T030833Z
 UID:644938456.1419.
 END:VCARD
 
 BEGIN:VCARD
 VERSION:2.1
 EMAIL:te...@test.com
 FN:test2
 REV:20101116T031015Z
 UID:644938507.1420
 END:VCARD
 
 I am using below regex  :-
 
 my $fh = IO::File-new($file, r);
 my $script = do { local $/; $fh };
 close $fh;
 if (
$script =~ m/
 (^BEGIN:VCARD\s*(.*)
 ^END:VCARD\s+)/sgmix
 ){
 print OUTFILE $1.\n;
 }
 
 However it just prints 1st instance and not all.

It also prints the text between the two instances, right?

 Any suggestions ?

You need a non greedy match .*? instead of the greedy match .* that you
are using.  Then you'll need to use while instead of if.

Or perhaps you'd prefer:

 $ perl -ne 'print if /BEGIN:VCARD/ .. /END:VCARD/'  in  out

or

 $ perl -n00e 'print if /^BEGIN:VCARD/'  in  out

See perldoc perlrun for the switches and Range Operators from perdoc
perlop for ..

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




Re: Need a Perl Book for Beginner

2012-12-05 Thread Jamie Paul Griffin
* David Christensen dpchr...@holgerdanske.com [2012-12-04 18:36:13 -0800]:

 On 12/04/12 14:56, Asad wrote:
Would you guidance to start  develop logic for perl programming .
 Also I am looking for a book to start with .
  Which explains the basic of perl programming with examples also
 how to develop logic for programing.
 
 IMHO the canonical trilogy for aspiring Perl programmers is:
 
 1. Learning Perl -- this book gets you up the initial learning
 curve. Read it cover to cover, enter and play with the example code,
 and do the exercises:
 
http://shop.oreilly.com/product/0636920018452.do

I'm a beginner too and I have been using Learning Perl and can highly recommend 
it. 

It is well written for those just starting out with perl and explains 
everything in plain English so not to get confused. I haven't looked at or 
tried any others yet.

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




Re: Is this code which is using Switch.pm safe?

2012-11-22 Thread Paul Johnson
On Thu, Nov 22, 2012 at 11:37:15AM +0530, Chankey Pathak wrote:
 In our company we were using this code (given at the end) for about 10
 years and it worked fine.
 
 Some days ago we faced some issues and we had to re-code the complete
 package, we decided to replace this code with Switch module by Damian (in
 order to improve the readability of code).
 
 Everything is working fine for us.
 
 Later I found on Perlmonks http://www.perlmonks.org/?node_id=486756 that
 Damian had put this module under
 
 Damian modules you shouldn't use in production because their purpose is to
 explore and prototype future core language features.
 
 But it is working fine for us because we are not hitting the limitations of
 this module (I guess).
 
 Now I ask you guys to please have a look at the both implementations
 (nested if else vs switch) and let me know whether using Switch in the
 newer implementation is fine or are we creating some future problems for
 us? Is using Switch in the code given below fine or are there any hidden
 bugs/problems?
 
 I've already read the bugs and reviews of this module on CPAN and Perlmonks
 and I guess our code is far away from hitting those bugs (I think so).
 
 We are using Perl 5.8.5.
 
 *PS:* I know the alternatives of Switch, we have given/when in Perl 5.10,
 we can use dispatch table and other solutions which are specified
 herehttp://stackoverflow.com/questions/844616/obtain-a-switch-case-behaviour-in-perl-5,
 but right now we just want to compare the new implementation which uses
 Switch.
 
 
 Code:
 
 *Using nested if else: http://paste.debian.net/211434/*
 
 *Using Switch: http://paste.debian.net/211435/*

Hmmm.  Is it safe?  I suppose that depends on what you mean by that.  If
it works in the conditions you care about I suppose you could say it is
safe.

 or are we creating some future problems

That's very possible.  Or perhaps it will run for ten years before
someone else does a rewrite.

I'm not sure why, knowing what you obviously do, you are wedded to using
Switch.  I would be trying to use a dispatch table, if the case
insensitivity of $command would allow it.  Failing that, I see nothing
particularly unreadable about the if/elsif/else chain.  Especially if
you make the bodies into sepatate subs so you only have one sub call per
condition.  Then you could line everything up vertically to make it very
readable.

Even if you update your perl version I would stay away from given/when
for now.

And you should update your perl version.  It's unsuported, buggy and I'm
sure it has security problems which have been fixed in the last eight
years.  (That's always a good case to make to management folk.)

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




Re: Fwd: Is this code which is using Switch.pm safe?

2012-11-22 Thread Paul Johnson
You asked exactly this question in both stackoverflow
(http://stackoverflow.com/questions/13506511/is-this-code-which-is-using-switch-pm-safe)
and perlmonks (http://perlmonks.org/?node_id=1005070).

Please don't do that.  Or, if you think there is a good reason to do so,
link to where you have previously posted.  Otherwise you are just
wasting people's time.  By the time I answered your post here you
already had a perfectly good answer on stackoverflow.


On Thu, Nov 22, 2012 at 03:59:23PM +0530, Chankey Pathak wrote:
 I discussed with the teammates and convinced them to go for 'dispatch
 table'. Just want to know will the 'dispatch table' allow regex? As you can
 see we're using regex in if-else part. I guess I'll have to use
 Tie::RegexHash.
 
 Regarding the Perl version: I don't know when are they (my company) gonna
 update it. I heard that we'll use 5.12 in the next update of our product
 though.
 
 
 On Thu, Nov 22, 2012 at 3:32 PM, Paul Johnson p...@pjcj.net wrote:
 
  On Thu, Nov 22, 2012 at 11:37:15AM +0530, Chankey Pathak wrote:
   In our company we were using this code (given at the end) for about 10
   years and it worked fine.
  
   Some days ago we faced some issues and we had to re-code the complete
   package, we decided to replace this code with Switch module by Damian (in
   order to improve the readability of code).
  
   Everything is working fine for us.
  
   Later I found on Perlmonks http://www.perlmonks.org/?node_id=486756
  that
   Damian had put this module under
  
   Damian modules you shouldn't use in production because their purpose is
  to
   explore and prototype future core language features.
  
   But it is working fine for us because we are not hitting the limitations
  of
   this module (I guess).
  
   Now I ask you guys to please have a look at the both implementations
   (nested if else vs switch) and let me know whether using Switch in the
   newer implementation is fine or are we creating some future problems for
   us? Is using Switch in the code given below fine or are there any hidden
   bugs/problems?
  
   I've already read the bugs and reviews of this module on CPAN and
  Perlmonks
   and I guess our code is far away from hitting those bugs (I think so).
  
   We are using Perl 5.8.5.
  
   *PS:* I know the alternatives of Switch, we have given/when in Perl 5.10,
   we can use dispatch table and other solutions which are specified
   here
  http://stackoverflow.com/questions/844616/obtain-a-switch-case-behaviour-in-perl-5
  ,
   but right now we just want to compare the new implementation which uses
   Switch.
  
  
   Code:
  
   *Using nested if else: http://paste.debian.net/211434/*
  
   *Using Switch: http://paste.debian.net/211435/*
 
  Hmmm.  Is it safe?  I suppose that depends on what you mean by that.  If
  it works in the conditions you care about I suppose you could say it is
  safe.
 
   or are we creating some future problems
 
  That's very possible.  Or perhaps it will run for ten years before
  someone else does a rewrite.
 
  I'm not sure why, knowing what you obviously do, you are wedded to using
  Switch.  I would be trying to use a dispatch table, if the case
  insensitivity of $command would allow it.  Failing that, I see nothing
  particularly unreadable about the if/elsif/else chain.  Especially if
  you make the bodies into sepatate subs so you only have one sub call per
  condition.  Then you could line everything up vertically to make it very
  readable.
 
  Even if you update your perl version I would stay away from given/when
  for now.
 
  And you should update your perl version.  It's unsuported, buggy and I'm
  sure it has security problems which have been fixed in the last eight
  years.  (That's always a good case to make to management folk.)
 
  --
  Paul Johnson - p...@pjcj.net
  http://www.pjcj.net
 
 
 
 
 -- 
 Regards,
 Chankey Pathak http://www.linuxstall.com

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




Re: Regex one-liner to find several multi-line blocks of text in a single file

2012-11-01 Thread Paul Johnson
On Thu, Nov 01, 2012 at 12:44:08AM -0700, Thomas Smith wrote:
 Hi,
 
 I'm trying to search a file for several matching blocks of text. A sample
 of what I'm searching through is below.
 
 What I want to do is match # START block # through to the next
 # END block # and repeat that throughout the file without
 matching any of the text that falls between each matched block (that is,
 the ok: some text lines should not be matched). Here is the one-liner I'm
 using:
 
 perl -p -e '/^# START block #.*# END block #$/s' file.txt
 
 I've tried a few variations of this but with the same result--a match is
 being made from the first # START block # to the last # END
 block #, and everything in between... I believe that the .*,
 combined with the s modifier, in the regex is causing this match to be
 made.
 
 What I'm not sure how to do is tell Perl to search from START to the next
 END and then start the search pattern over again with the next START-END
 match.
 
 How might I go about achieving this?

perl -ne 'print if /# START block #/ .. /# END block #/' 
file.txt

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




Re: changing $/ for recognising empty line

2012-10-29 Thread Paul Johnson
On Mon, Oct 29, 2012 at 12:48:33PM +0100, Hermann Norpois wrote:

 But still: What is wrong with $/=^\s+$ ?

From perldoc perlvar:

  Remember: the value of $/ is a string, not a regex.  awk has to be
  better for something. :-)

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




Re: perl interpreter output meaning?

2012-09-22 Thread Jamie Paul Griffin
[ 'lesleyb' wrote on Sat 22.Sep'12 at  9:22:09 +0100 ]

 On Sat, Sep 22, 2012 at 09:45:08AM +0200, Anne Wainwright wrote:
  Hi,
  
  this is the output.
  
  Use of uninitialized value $9 in concatenation (.) or string at
  pg_delim2htm_01.pl line 89,  line 1.
  Use of uninitialized value $9 in concatenation (.) or string at
  pg_delim2htm_01.pl line 89,  line 4.
  Use of uninitialized value $9 in concatenation (.) or string at
  pg_delim2htm_01.pl line 89,  line 6.
  
  and so on
  
  What do the  with the 'lines' to the right mean?
  
  (I do seem to have a problem on line 89 which is a regex with the /x
  modifier)
  
  many thanks
  Anne
 Hi Anne
 
 From what I can see of your code, you have an uninitialised value in your 
 regex.
 
 Gazing deeply into the crystal ball we rent especially for such occasions, 
 you've at
 least nine bracketed matches in your regex.

Nothing like a bit of sarcasm to get you started in the morning lol

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




Re: My long script

2012-09-16 Thread Paul Anderson
On 2012-09-15, at 11:25 PM, jmrhide-p...@yahoo.com wrote:

 It would be WONDERFUL to get an email every time it glitched! Does anybody 
 have 
 a 
 
 
 sub written for that?

Just bear in mind that you need to add logic to the while(1) loops to determine 
that they are running too long. If it gets hung up in them, *that is not an 
error unless you turn it into one*. Otherwise the program will blithely sit 
there, looping for all eternity without producing any errors at all.

It's like the open BORK, foo or die $!  code you see. You have to write the 
logic to detect that the internal state of the program is misbehaving. 


Re: Clarification re Perl Script out of control

2012-09-14 Thread Paul Anderson

On 2012-09-14, at 1:56 AM, jmrhide-p...@yahoo.com wrote:

 I can see from the responses so far that I was unclear in the way I phrased 
 my 
 question, so please let me emphasize the following: MY SCRIPT, THOUGH COMPLEX 
 (500 LINES), PRODUCES EXACTLY THE OUTPUT I EXPECT EVERY TIME I RUN IT. In 
 particular, it never hangs or gives a wrong answer. Also, with all error 
 trapping enabled, no error appears on the server log. 
 

You were quite clear. The problem is that the script as it stands is very 
complex, and not well organized. This makes debugging much more difficult. The 
design of the script also makes it exceedingly difficult to make any changes to 
things like the style of the pages it generates. It adds a great deal of 
headaches. Rewriting, while indeed time consuming, quite probably will not take 
terribly long, resolve the current bug, and result in a script that is much 
more easily maintained and debugged. It's an investment in the future of the 
script, making your own life easier.

 
 Where I think we need to focus is, assuming the script runs to its end every 
 time, why doesn't it release its hold on the server resources?

Ahh, there's the rub! You assume it ends every time! This assumption is not 
necessarily valid. Just because it behaves itself for you doesn't necessarily 
mean there isn't something weird that you aren't reproducing. It could even be 
a bug that is very unlikely. If it only happens once in a thousand times of 
being run, a busy site can make it go off many times per day, whereas debugging 
at home will never make it go off. The while(1) loops themselves are perilous. 
If the logic doesn't behave in the way you hope, and something weird happens, 
you have an infinite loop. That would cause the exact problem you describe. It 
would also not produce any errors - because no real error has occurred. The 
program has just assumed an internal state you didn't anticipate. I think at 
the top of your list is reworking those loops so that they are no longer 
infinite.

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




Re: My script is OUT OF CONTROL!

2012-09-13 Thread Paul Anderson
Just checked on my machine, looks like it produces a floating point number 
between 0 and 1. 


Paul Anderson -- VE3HOP

On 2012-09-13, at 8:07 PM, Jim Gibson jimsgib...@gmail.com wrote:

 
  (I am not sure exactly what rand(0) returns). 

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




Re: Recommendations for emacs customizations for editing Perl

2012-09-12 Thread Paul Anderson
Install PDE from CPAN and it'll work alright. It works fine for me on 5.16.1. 




Paul Anderson -- VE3HOP

On 2012-09-12, at 12:08 AM, Vic Sage vic.s...@me.com wrote:

 I'd like to hear some recommendations from this list for customizations to 
 emacs for coding Perl.
 
 One seemingly respected site, http://emacswiki.org/emacs/PerlLanguage, has a 
 lot of links but unfortunately some are pretty dated.  For example, the Perl 
 Develop Environment, http://search.cpan.org/dist/Emacs-PDE/, last updated in 
 2008, wouldn't install because my version of Perl (5.16.1) was too far out 
 of date.  So of course the question is, how up-to-date is emacswiki.org?
 
 

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




Re: Recommendations for emacs customizations for editing Perl

2012-09-12 Thread Paul Anderson
Another thing to look out for in PDE is a line like:

use 5.14;

It appears that newer versions of perl will interpret this as version 5.140, 
not 5.14. You need a zero, like so:

use 5.014;

Then it will interpret it properly. 

From my ~/.emacs:

(defalias 'perl-mode 'cperl-mode)
(setq cperl-hairy t)
(setq cperl-auto-newline t)

(add-to-list 'load-path ~/.emacs.d/pde/)
(load pde-load)

Of course, I have the PDE stuff in ~/.emacs.d/pde. C-c r will run your code, as 
I recall if you use git C-x v v will check in your changes. C-c C-t C-b IIRC 
will run perltidy on the entire buffer. 

I recommend editing the templates that PDE uses with vi, it has stuff that 
emacs interprets. Created something of a nuisance when I tried using emacs to 
edit them. 


Paul Anderson -- VE3HOP

On 2012-09-12, at 9:42 AM, Vic Sage vic.s...@me.com wrote:

 On Sep 11, 2012, at 11:45 PM, Shlomi Fish shlo...@shlomifish.org wrote:
 
 
 
 But, yes, as Shlomi points out, I *am* looking for suggestions from 
 experienced Perl programmers who use - and customize - Emacs :-)
 
 

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




Re: Help with the can() method

2012-09-12 Thread Paul Anderson
Did you copy and paste that code? Do you know that when calling can() you are 
using $ojb instead of $obj?


Paul Anderson -- VE3HOP

On 2012-09-12, at 9:17 AM, pangj pa...@riseup.net wrote:

 Hi,
 
 Today I wrote a script for validate a dns record from the specified DNS
 server, the subroutine is:
 
 sub validate_dns {
my $host = shift;  # a host, for example, www.google.com
my $value = shift;  # the host's DNS value, may be a CNAME or an iP
 address
my $dns = shift;  # DNS server, for example, 8.8.8.8
 
my $method = $value =~ /^\d+\.\d+\.\d+\.\d+$/ ? address : cname;
my $res = Net::DNS::Resolver-new(nameservers = [$dns]);
my $answer = $res-query($host);
 
if (defined $answer) {
return 1 if ($answer-answer)[0]-$method eq $value;
}
 
return 0;
 }
 
 Sometime the $host's value is an A record, thus it has a address() method.
 Sometime the $host's value is a CNAME record, it has a cname() method.
 When calling the subroutine abve with the wrong arguments, it will cause
 warnings.
 For example,
 validate_dns(www.nsbeta.info,1.2.3.4,8.8.8.8);
 This will throw up warnings, because www.nsbeta.info is a CNAME but the
 subroutine calls address() method which belongs to the A record.
 
 So I modified the subroutine as:
 
 sub validate_dns {
my $host = shift;
my $value = shift;
my $dns = shift;
 
my $res = Net::DNS::Resolver-new(nameservers = [$dns]);
my $answer = $res-query($host);
 
if (defined $answer) {
 my $obj = ($answer-answer)[0];
 if ($ojb-can(address) ) {
  return 1 if $obj-address eq $value;
 } elsif ($ojb-can(cname) ) {
  return 1 if $obj-cname eq $value;
 }
}
 
return 0;
 }
 
 This totally can't work, it seems $obj-can() method has no effect.
 What's wrong with my code? thanks.
 
 
 -- 
 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: Recommendations for emacs customizations for editing Perl

2012-09-12 Thread Paul Anderson
I haven't had any trouble. I think IIRC it may be confused a little by 
given/when, but not badly. 


Paul Anderson -- VE3HOP

On 2012-09-12, at 12:20 PM, Vic Sage vic.s...@me.com wrote:

 
 On Sep 12, 2012, at 5:59 AM, Paul Anderson wackyvor...@me.com wrote:
 
 Install PDE from CPAN and it'll work alright. It works fine for me on 
 5.16.1. 
 
 So it does.  
 
 I was concerned that PDE hadn't been updated in four years.  But I gather it 
 works well with all the recent syntax?
 
 V
 
 -- 
 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: Perl Code

2012-08-31 Thread Paul Anderson
I smell homework:p


Paul Anderson -- VE3HOP

On 2012-08-29, at 12:46 PM, Ashwin Rao T ashwin...@gmail.com wrote:

 1)Check if IP address is in the range 172.125.1.0 and 172.125.25.0 using only
 return functions  regular expressions in Perl. 
 2)Check if the name is valid (has atleast 3 letters and one vowel) using only
 return functions and regular expressions in Perl.
 3)Check if email address is valid using only return functions and regular 
 expressions in Perl.
 4)Convert the number into words using only return functions and regular 
 expressions in Perl. (15 = one five) 
 
 
 
 
 -- 
 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: greater circle

2012-08-30 Thread Paul Anderson
It looks like 2*10^-13 miles is about twice the inter atomic distance in 
diamond:)


Paul Anderson -- VE3HOP

On 2012-08-30, at 4:22 AM, Chris Stinemetz chrisstinem...@gmail.com wrote:

 
 
 Because floating-point arithmetic as done by limited precision computers is 
 always an approximation. An IEEE 754 double-precision 64-bit floating point 
 number uses a 53-bit fraction and therefore has about 16 decimal digits of 
 precision. So calculating zero within 13 digits (e-013) is pretty good.
 
 The calculation done by the Geo::Ellipsoid module is very complex and 
 involves sines, cosines, tangents, etc. Precision will be lost with each 
 floating-point operation.
 
 Have you worked out what 2e-13 miles is in inches? For all practical 
 purposes, it IS zero.
 
 
 Thank you for the great explanation. Yes that is close enough to 0 for
 me :) Thanks again.
 
 -Chris
 
 -- 
 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: greater circle

2012-08-30 Thread Paul Anderson
Works great until you start using a coordinate system that places points on a 
sphere:)


Paul Anderson -- VE3HOP

On 2012-08-30, at 2:31 PM, Uri Guttman u...@stemsystems.com wrote:

 On 08/30/2012 12:20 PM, Paul Anderson wrote:
 It looks like 2*10^-13 miles is about twice the inter atomic distance in 
 diamond:)
 
 
 i don't know why you need to calculate great circle distances. it is obvious 
 to any observer that the earth is flat so simple geometric distances should 
 be fine.
 
 uri
 
 
 
 -- 
 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: greater circle

2012-08-29 Thread Paul Anderson
That is 99.99780441116897988% error. 16 9's is better than any 
measuring instrument in existence. I think it'll do:)


Paul Anderson -- VE3HOP

On 2012-08-30, at 1:29 AM, Jim Gibson jimsgib...@gmail.com wrote:

 
 On Aug 29, 2012, at 5:53 PM, Chris Stinemetz wrote:
 
 Just one question. If the two sets of coordinates are the same I was
 expecting the distance to be 0 miles.
 
 Instead I get:
 
 2.19558883102012e-013
 
 For the following example.
 
 #!/usr/bin/perl
 
 use strict;
 use warnings;
 
 use feature qw(say);
 use Geo::Ellipsoid;
 
 my $lat1 = 39.316858;
 my $lat2 = 39.316858;
 my $lon1 = -94.963194;
 my $lon2 = -94.963194;
 
 
 my $ellipsoid = Geo::Ellipsoid-new(units='degrees', dist='miles');
 say $ellipsoid-range($lat1,$lon1,$lat2,$lon2);
 
 If it is the expected outcome would you please explain why?
 
 
 Because floating-point arithmetic as done by limited precision computers is 
 always an approximation. An IEEE 754 double-precision 64-bit floating point 
 number uses a 53-bit fraction and therefore has about 16 decimal digits of 
 precision. So calculating zero within 13 digits (e-013) is pretty good.
 
 The calculation done by the Geo::Ellipsoid module is very complex and 
 involves sines, cosines, tangents, etc. Precision will be lost with each 
 floating-point operation.
 
 Have you worked out what 2e-13 miles is in inches? For all practical 
 purposes, it IS zero.
 
 
 -- 
 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: array match- help !

2012-08-17 Thread Paul Anderson
Isn't this basically the format of YAML? Couldn't a YAML CPAN module handle 
this data?

On 2012-08-17, at 10:10 AM, jet speed speedj...@googlemail.com wrote:

 Hi Shomi,
 
 Appreciate your comments.Thanks
 
 I find it difficult to understand, if i convert into has.  i.e  keys and
 corresponding values.  if my keys are from @actzone and values are from
 @all. How can i do this, becuase there are 2 keys and values are multiple
 ex. alias: wwn: etc.
 how can i match this in a hash ?
 


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




Unable to change pos in regex match

2012-08-14 Thread Paul Anderson
I'm working on code to solder the Project Euler problem #8. This is the one 
where you have to find the greatest product of any five digits found in order 
in a one thousand digit number. It sort of worked briefly. This version 
worked(I had line 25 uncommented):

https://github.com/wackyvorlon/marker/blob/02eff43b98597ad45e38c7269ad4e4ae55eb7165/euler8.pl

I pushed the product on to the end of @products. I then had to sort them, and 
was having trouble making that work. This is the current state of the code:

https://github.com/wackyvorlon/marker/blob/master/euler8.pl

/usr/bin/myperl is a link to the perlbrew install in my home directory, 
currently version 5.16.1. This is the output when I try running it:

Use of uninitialized value in subtraction (-) at ./euler8.pl line 24 (#1)
(W uninitialized) An undefined value was used as if it were already
defined.  It was interpreted as a  or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl will try to tell you
the name of the variable (if any) that was undefined.  In some cases
it cannot do this, so it also tells you what operation you used the
undefined value in.  Note, however, that perl optimizes your program
anid the operation displayed in the warning may not necessarily appear
literally in your program.  For example, that $foo is usually
optimized into that  . $foo, and the warning will refer to the
concatenation (.) operator, even though there is no . in
your program.



I've copied and pasted the code below, for those who don't want to trek over to 
github:

#!/usr/bin/myperl -w
# euler8.pl --- Euler Problem 8
# Author: Paul Anderson wackyvorlon@paul-andersons-macbook-pro-3.local
# Created: 14 Aug 2012
# Version: 0.01

use warnings;
use diagnostics;
use 5.16.0;

#use strict;
no strict;


# The number.

$numb=73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280686645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263210937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450;

$result=0;


for ($numb=~/(\d)(\d)(\d)(\d)(\d)/gi) {
$cur = $1*$2*$3*$4*$5;  # Put the product in a variable.
pos $numb=(pos $numb) - 4;   # Reset position where 
matching will begin.
 # pos $num defaults to the 
position just
 # after the *last* character in 
our match.
 # We want our second match to 
begin after
 # the *first* character.
if ( $cur  $result) {
$result=$cur;

}

}

print \nGreatest value is $result.\n;


As it stands, I'm baffled. Any thoughts on what's wrong?
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Unable to change pos in regex match

2012-08-14 Thread Paul Anderson
Well, now I feel sufficiently stupid:) Thanks!

On 2012-08-14, at 6:19 PM, Jim Gibson jimsgib...@gmail.com wrote:

 
 
 
 Have you tried printing out the values of ($1,$2,$3,$4,$5), $cur, and pos 
 $numb for each iteration? I think you will find it most informative to do so.
 
 Hint: you should be using a while loop instead of a for loop.
 


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




Re: Net::NNTP inconsistent article retrieval

2012-08-12 Thread Paul Anderson
From here:

http://perldoc.perl.org/utf8.html

I wonder if utf8::upgrade() might do the trick. With write_file() in 
File::Slurp, passing it a hash with binmode set to :utf8 will get it to write 
UTF8 output to disk. Perl has in general very good internal UTF support. 

An aside: You know that leafnode basically does everything you're trying to 
write, right?


Paul Anderson -- VE3HOP

On 2012-08-11, at 5:21 PM, Chris Knipe sav...@savage.za.org wrote:

 Hi Paul,
 
 I will be inclined to disagree - it depends on whether or not the content
 was encoded to begin with (perhaps I should not use the term 'binary' as
 such, but rather refer to it as unreadable characters).  File::Slurp did
 make my life allot easier now yes, but my problem is still not solved.
 Looking at the packet capture, it is as clear as daylight to me that either
 Net::NNTP or File::Slurp is replacing all \r\n line breaks with \n.  The
 NNTP RFCs require all lines to be terminated by \r\n.  I can't simply use a
 regex to replace all instances of \n to \r\n as it will affect the
 attachments (binary or not, encoded or not).  I've attached an screen
 capture out of Wireshark for a single packet (available at
 http://www.savage.za.org/capture.png as well) which is showing my problem as
 clearly as daylight.
 
 Line 1: 220 0 message-id\r\nThis is sent by my application, and it is
 properly terminated as required.
 Line 2 through to 15 original headers as received by my upstream news
 server.  The lines WAS previously terminated with \r\n but either Net::NNTP
 or File::Slurp, changed all the \r\n values to \n
 Line 18 through to next packet is definitely not text anymore, and neither
 is it terminated by \r\n anymore as it was previously... 
 
 I am not trying to process, alter, or extract articles in any way, I simply
 want to download them, store them, and forward them... 
 
 --
 Chris.
 
 
 
 -Original Message-
 From: Paul Anderson [mailto:wackyvor...@me.com] 
 Sent: 11 August 2012 18:17
 To: Chris Knipe
 Cc: beginners@perl.org
 Subject: Re: Net::NNTP inconsistent article retrieval
 
 Umm... Are you aware that binary attachments on usenet aren't actually *in*
 binary? They're encoded in ASCII using one of a number of different methods.
 They're just text, until decoded on the receiving end. 
 
 I recommend looking into File::Slurp and CHI. CHI basically implements the
 entire caching back-end for you. 
 
 Sent from my iPhone
 
 On 2012-08-11, at 5:21 AM, Chris Knipe sav...@savage.za.org wrote:
 
 Hi All,
 
 I'm using Net::NNTP to transfer articles from servers.  My aim is to write
 an NNTP proxy.
 
 I obtain the article from my parent news server, write the file to disk,
 and
 serve the current, as well as future requests for that article from the
 local file on the disk.  My results are very inconsistent, and I suspect
 that it is related to the \r\n line breaks conflicting with the binary
 data
 retrieved from the article (a line may include \r\n when it's not actually
 meant to indicate the END of a line).  I am currently using:
 
 local $/ = \r\n;
 my $Article = $nntp-article($Command['1']);
 if ($Article) {
   # The parent has the article!  Let's take it.
   open FILE, :raw, $File or die $!;
   binmode(FILE);
   foreach my $line (@$Article) {
 print FILE $line;
   }
   close(FILE);
   $ReturnStr = 220 0  . $Command['1'] . \r\n;
   open FILE, :raw, $File;
   binmode(FILE);
   foreach my $line (FILE) {
 $ReturnStr .= $line;
   }
   close(FILE);
   $ReturnStr .= .\r\n;
   binmode(STDOUT);
   print $ReturnStr;
 
 For some articles, the above code is absolutely fine and no problems are
 returned.  For others, the binary attachments to the article (regardless
 of
 type of file), is corrupt, and cannot be opened.   The results are also
 very
 inconsistent, and being binary I'm not exactly sure how to provide samples
 of what works and what doesn't.  I've analysed packet captures
 excessively,
 and I am definitely getting all the data correctly, and consistently from
 my
 parent news server - the problem is related to me writing the file to the
 local disk, and serving the content of that file from the local disk.
 
 Hopefully someone can assist and point me towards the right direction -
 after spending close to a week on this, I'm ready to pull out my hair! :-(
 --
 Chris.
 
 
 
 -- 
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/
 
 
 capture.png

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




Re: Net::NNTP inconsistent article retrieval

2012-08-11 Thread Paul Anderson
Umm... Are you aware that binary attachments on usenet aren't actually *in* 
binary? They're encoded in ASCII using one of a number of different methods. 
They're just text, until decoded on the receiving end. 

I recommend looking into File::Slurp and CHI. CHI basically implements the 
entire caching back-end for you. 

Sent from my iPhone

On 2012-08-11, at 5:21 AM, Chris Knipe sav...@savage.za.org wrote:

 Hi All,
 
 I'm using Net::NNTP to transfer articles from servers.  My aim is to write
 an NNTP proxy.
 
 I obtain the article from my parent news server, write the file to disk, and
 serve the current, as well as future requests for that article from the
 local file on the disk.  My results are very inconsistent, and I suspect
 that it is related to the \r\n line breaks conflicting with the binary data
 retrieved from the article (a line may include \r\n when it's not actually
 meant to indicate the END of a line).  I am currently using:
 
  local $/ = \r\n;
  my $Article = $nntp-article($Command['1']);
  if ($Article) {
# The parent has the article!  Let's take it.
open FILE, :raw, $File or die $!;
binmode(FILE);
foreach my $line (@$Article) {
  print FILE $line;
}
close(FILE);
$ReturnStr = 220 0  . $Command['1'] . \r\n;
open FILE, :raw, $File;
binmode(FILE);
foreach my $line (FILE) {
  $ReturnStr .= $line;
}
close(FILE);
$ReturnStr .= .\r\n;
binmode(STDOUT);
print $ReturnStr;
 
 For some articles, the above code is absolutely fine and no problems are
 returned.  For others, the binary attachments to the article (regardless of
 type of file), is corrupt, and cannot be opened.   The results are also very
 inconsistent, and being binary I'm not exactly sure how to provide samples
 of what works and what doesn't.  I've analysed packet captures excessively,
 and I am definitely getting all the data correctly, and consistently from my
 parent news server - the problem is related to me writing the file to the
 local disk, and serving the content of that file from the local disk.
 
 Hopefully someone can assist and point me towards the right direction -
 after spending close to a week on this, I'm ready to pull out my hair! :-(
 --
 Chris.
 
 
 
 -- 
 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: system command not working in different versions

2012-08-09 Thread Paul Anderson
Silly question: Does submit_now.pl have its execute bit set?


Paul Anderson -- VE3HOP

On 2012-08-09, at 3:17 AM, venki neeli venki_neeli...@yahoo.com wrote:

 hi Midhun/Hal
 
 Shebang lines of Script-1 and Script-2 and perl location are same.
 Where as Script one is executing and Script-2 is not executing.
 The other machines are calling the Script-2 perfectly with command  
 ./submit_now.pl $.
 Full path also I have tried. What may be the problem? is it with version of 
 perl? or perl module problem?
 
 
 Regards,
 Neeli
 
 
 
 
 From: midhun midhun...@gmail.com
 To: Hal Wigoda hal.wig...@gmail.com 
 Cc: venki neeli venki_neeli...@yahoo.com; perl list beginners@perl.org 
 Sent: Thursday, 9 August 2012 12:01 PM
 Subject: Re: system command not working in different versions
 
 Neeli, Hal is right. Try $which perl from your shell. The location may
 differ.
 
 Regards,
 Midhun
 
 On Thu, Aug 9, 2012 at 11:46 AM, Hal Wigoda hal.wig...@gmail.com wrote:
 
 What about the location of perl itself?
 
 
 On Thu, Aug 9, 2012 at 12:13 AM, venki neeli venki_neeli...@yahoo.com
 wrote:
 Yes Midhun,
 
 I have compared both. Even I have tried keep both same.
 
 See the shebang line.
 #!/usr/bin/perl -w
 
 
 Note:- Script-1 and script-2(submit_now.pl) are in same location.
 
 Regards,
 Neeli
 
 
 
   From: midhun midhun...@gmail.com
 To: venki neeli venki_neeli...@yahoo.com
 Cc: perl list beginners@perl.org
 Sent: Wednesday, 8 August 2012 3:55 PM
 Subject: Re: system command not working in different versions
 
 One suggestion. Did you compare the shebang line of your submit_now.pland
 your perl executable location in the 2nd machine where it is not working.
 
 Regards,
 Midhun
 
 On Wed, Aug 8, 2012 at 11:48 AM, venki neeli venki_neeli...@yahoo.com
 wrote:
 
 Dear Friends,
 
 I am developing a script in which I need to call another perl script.
 In one linux machine it is working and in the other machine it is not
 working.
 
 Ex:- system(./submit_now.pl $);
 
 can you please, tell me what is the reason?
 and as well solution.
 
 Regards,
 Neeli
 
 
 
 
 --
 -Midhun
 
 
 
 --
 -
 Chicago
 Hal Wigoda

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




Re: Catfile function not working on windows 7 machine

2012-08-09 Thread Paul Anderson
Per the message by Mr. Adigun, using a single quote instead of double quotes 
tells perl not to interpolate the string. That will prevent it from eating all 
those backslashes. 


Paul Anderson -- VE3HOP

On 2012-08-09, at 8:53 AM, Sandip Karale sandipkar...@gmail.com wrote:

 Hi Shlomi,
 
 Thanks for your reply.
 
 my $d=machdir;  This UNC path I'm getting from arguments, and
 then I want to process it. That is I want to concatenate the file name to
 it and open the file and print its contents. So your solution will not help.
 
 Sincerely,
 Sandip
 
 
 On Wed, Aug 8, 2012 at 4:13 PM, Shlomi Fish shlo...@shlomifish.org wrote:
 
 Hi Sandip,
 
 On Wed, 8 Aug 2012 15:47:24 +0530
 Sandip Karale sandipkar...@gmail.com wrote:
 
 Hello ,
 
 I'm new to perl.
 
 I am on windows 7 64 bit machine. with Strawberry Perl (64-bit)
 5.14.2.1.
 
 It's good that you are using Strawberry Perl.
 
 
 *My Code:*
 
 use File::Spec::Functions;
 my $f=foo.txt;
 my $d=machdir;
 
 Why do you have four backslashes here and then two? (Note that they are
 escaped, so
 it's twice as that)? In Perl, a backslash is a backslash, even when it
 is interpolated. So you can just do something like:
 
 my $BS = \\;
 my $d = $BS${BS}mach${BS}dir;
 
 That may be the source of part of your problem.
 
 Regards,
 
Shlomi Fish
 
 
 print $f \n;
 print $d \n;
 print catfile($d,$f);
 
 
 *Output:*
 *
 *
 foo.txt
 mach\\dir
 \mach\dir\foo.txt
 
 *My Problem:*
 *
 *
 After concatenating with UNC path and file, the concatenated path is
 wrong! the expected path is \\mach\dir\foo.txt
 Please help me out if I'm missing something.
 
 Sincerely,
 Sandip
 
 
 
 --
 -
 Shlomi Fish   http://www.shlomifish.org/
 Humanity - Parody of Modern Life - http://shlom.in/humanity
 
 Larry Wall *does* know all of Perl. However, he pretends to be wrong
 or misinformed, so people will underestimate him.
 
 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/




Re: Concatenation in Perl

2012-07-22 Thread Paul Johnson
On Sun, Jul 22, 2012 at 11:09:10PM +0200, Jenda Krynicky wrote:
 From: Paul Johnson p...@pjcj.net
  You need a mixture of the two approaches: map to prepend not in: and
  join to join them.
  
my $query = join  and , map not in:$_, @folders;
 
 
 @folders = ('one', 'two');
 my $query = not in: . join(  and not in:, @folders);
 print $query;
 
 will be quicker.

Maybe.  But I'm not sure that's a particularly compelling argument.
Better to aim for correctness, clarity and maintainability, wouldn't you
think?

  no need to map and join just because you need some 
 text even before the first value.

That's a good point.

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




Re: Concatenation in Perl

2012-07-19 Thread Paul Johnson
On Thu, Jul 19, 2012 at 04:07:49PM +0530, punit jain wrote:
 Hi ,
 
 I am doing a concat operation in Perl  for a string like below : -
 
 if( @folders ) {
 
 map {$query .= not in:$_ and; } @folders;
 print \n $query \n;
 
 }
 
 @folders contain - Inbox, Sent
 
 Output from above is - *not in:Inbox and not in:Sent and*
 Expected is = *not in:Inbox and not in:Sent*  ie without extra and
 after Sent
 
 I tried this with join as well as :-
 
 my $query=;
 foreach my $folder ( @folders )
 {
 $query = join and not in:$folder $query;
 }
 
 but still same result and this time extra and in beginning.
 
  Any idea how to do this ?

You need a mixture of the two approaches: map to prepend not in: and
join to join them.

  my $query = join  and , map not in:$_, @folders;

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




Re: Regex character classes: n OR m

2012-07-06 Thread Paul Johnson
On Fri, Jul 06, 2012 at 06:59:00PM +0100, Adam J. Gamble wrote:
 Dear All,
 
 I'm taking a (highly belated) first look at Perl today. From a background
 in Python, I'm coming to Perl, primarily out of curiosity with what it can
 do with regular expressions.

Welcome!

 To get to the point— is it possible to match a character class with a
 repeater that requires an exactly *n* OR *m* matches, rather than the
 traditional *{n, m}*. I've taken a look at
 http://perldoc.perl.org/perlrequick.html#Using-character-classes, which
 implies this wouldn't be possible? But, putting faith Perl's reputation for
 inherent quirkiness... if possible, I'd love to know what a solution would
 look like?

You're correct that there is no way to do this directly, but if you look
at the section just below (Matching this or that) you can see the basis
for a solution.

So, to match either three or five as for example, you could do this:

  /^(?:a{3}|a{5})$/

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




Re: How to create a user manual window

2012-06-12 Thread Paul Johnson
On Tue, Jun 12, 2012 at 07:11:57PM +0300, Shlomi Fish wrote:

 OK. For Windows there is now http://dwimperl.com/ which is open-source and is
 considered better than Activestate Perl. 

[citation needed]

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




Re: subroutine returning data

2012-06-04 Thread Paul Johnson
On Mon, Jun 04, 2012 at 11:30:30AM -0500, Chris Stinemetz wrote:
 I have a subroutine that I want to return 1 only if the value of
 %{$href-{$_[0]}} is equal to 'ND' for the whole 24 occurences.
 
 Any suggestions is greatly appreciated.
 
 Thank you,
 
 Chris
 
 sub site_offAir {
   for (values %{$href-{$_[0]}}) {
 return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND'
   }
   return '';
 }

I would imagine it to be much easier to look at it from the other way.
Return 0 any time you find a value that does not equal NO.  Then
return 1 at the end.

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




Re: Using perlbrew to change Perl version for scripts

2012-06-03 Thread Paul Johnson
On Sun, Jun 03, 2012 at 10:53:08AM -0700, sono...@fannullone.us wrote:
 On Jun 3, 2012, at 6:06 AM, Octavian Rasnita wrote:
 
  but a user has only a single cron job so it is not such a big issue.
 
   And therein lies the rub.  Why can't perlbrew do that for us 
 automatically?  perlbrew switch XXX... and you're done!
 
   Your solution makes sense if you only change versions once a year when 
 Perl is upgraded, but my original inquiry was for testing purposes.  If you 
 want to test all your scripts with different versions of Perl, making only 
 one change in perlbrew would be sooo nice.  It does it for the command line - 
 why not for the shebang line?
 
   I've written the author to ask for this.  For those who agree and would 
 like to do the same, here's his e-mail address:  gu...@gugod.org

I'm sure gugod doesn't need a dozen people asking him the same thing.
Why not wait until you get a reply?

But I've read through this whole thread a couple of times and I'm still
not sure what you are after, so I hope you've explained it better to
him.  Or perhaps that's just my problem.  But your question:

 It does it for the command line - why not for the shebang line?

doesn't make a lot of sense to me.  Are you asking why perlbrew can't
make a shebang line that points to your system perl (wherever that might
be) instead execute whatever perl you've switched to?  If you are, I
don't think you really want that (some important programs on your system
may stop working).  If not, what are you asking?

Are you actually looking for this?

$ perlbrew exec perl my_snazzy_program.pl

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




Re: Help required to extract multiple text fields from a text string

2012-05-26 Thread Adams Paul

Sent from my LG phone

Jim Gibson jimsgib...@gmail.com wrote:


On May 26, 2012, at 5:51 AM, pa...@fsmail.net wrote:

 split is slower than the correct regex matching.
 

Did you know that split uses a regular expression to find the separators on 
which to split the string? So your claim is unlikely to be true. In any case, 
the difference between using split and a single regular expression is likely 
to be insignificant. You shouldn't worry about such trivial differences. Chris 
just wants to extract some data from a file, and it won't matter to him if it 
takes a few extra nanoseconds.

The best advice when worrying about efficiency is this: first get the program 
working correctly. Then, if it is too slow, find out where the bottlenecks are 
and try to optimize those. Usually, the most gains can be achieved by changing 
the algorithm, not the implementation. 
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/





Re: shift vs @_

2012-05-21 Thread Paul Johnson
On Mon, May 21, 2012 at 12:40:15PM -0700, sono...@fannullone.us wrote:
 On May 20, 2012, at 10:07 PM, David Christensen wrote:
 
  I've updated function_arguments.pl with Benchmark, below. f_direct() is the 
  fastest, f_shift() is in the middle (12% slower), and f_assign() is the 
  slowest (37%).
 
 David,
 
   Are you saying that it would be faster to do:
 
 my $this_date = shift;
 my $output = shift;
 
   as opposed to:
 
 my ($this_date, $output) = @_;
 
   or am I not reading your assessment correctly?

Please don't care about this until your code is running correctly but
too slowly and profiling has determined that this is the bottleneck.

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




Re: Perl preprocessor

2012-05-04 Thread Paul Johnson
On Fri, May 04, 2012 at 08:16:01PM +0200, Manfred Lotz wrote:
 Hi there,
 I'm looking for a Perl preprocessor which should roughly be able to
 work like this: 
 
 In a document I have begin and end markers, say ## for both. Between
 those end markers there is perl code which should be run and everything
 which goes to stdout should go into the document. 
 
 The code should be lexical in a way that later code could refer to
 previous code.
 
 Primitive example: 
 
 This is a sample document.
 ## my $name = 'Manfred Lotz';  ## 
 My name is ## print $name; ## and I'm from Germany.
 
 should result in the following document:
 
 This is a sample document.
 My name is Manfred Lotz and I'm from Germany.
 
 
 Does anybody know about a module which could do this?

That sounds like something that Mason could do.  And I expect there are
a number of other modules on CPAN that could also manage that.  But take
a look at https://metacpan.org/module/Mason

Good luck,

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




Re: how to get two matches out

2012-05-03 Thread Paul Johnson
On Sun, Apr 29, 2012 at 11:10:33AM -0500, Lawrence Statton wrote:

 From perlretut, I quote:
 
In list context, //g returns a list of matched groupings,
or if there are no groupings, a list of matches to
the whole regexp.  So if we wanted just the words, we could use
 
@word = ($x =~ /(\w+)/g);  # matches,
# $word[0] = 'cat'
# $word[1] = 'dog'
# $word[2] = 'house'
 
 (Note that the docs (at least on my copy of perl) have a typo ... it
 says @words, not @word.)

This has now been fixed by
http://perl5.git.perl.org/perl.git/commitdiff/5a0c7e9d45ff6da450098635b233527990112d8a?hp=68cd360812f9eaa2d34c45c501e2fef87c44ccde
and will be in the upcoming 5.16.0 release.

Thanks for mentioning it.

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




  1   2   3   4   5   6   7   8   9   10   >