Re: First line of input file not accessible.

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

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

What's essentially happening is:

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

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

#!/usr/bin/env perl

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

Cheers

Dave P   (BIGPRESH)



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




Re: Please help: perl run out of memory

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

> OP maybe need the streaming IO for reading files.

Which is what they were already doing - they used:

while () {
...
}

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

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


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


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


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

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

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

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


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




Re: Please help: perl run out of memory

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

> Hi wilson
> Try  this module file::slurp

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


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




Re: perl script question

2019-10-10 Thread David Precious


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

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

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

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

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

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

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


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

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

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




Re: Question re remove_tree and symlinks

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

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

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

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

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

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

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

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


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

Cheers

Dave P (BIGPRESH)

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




Re: Google Sheets API Data Structure

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

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

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

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

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

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

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

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

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

Cheers

Dave P (BIGPRESH)

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




Re: Device::SerialPort on debian Buster

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

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

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

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

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

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

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

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

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

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

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


Cheers

Dave P (BIGPRESH)

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




Re: proper use of TEMPLATE

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

An empty string would work just fine.

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

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



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

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

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

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

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

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

  [% END %]


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

Cheers

Dave P

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




Re: Syntax "||" before sub

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

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

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

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

That strikes me as sub-optimal :)

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




Re: Syntax "||" before sub

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

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

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

For instance,

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

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

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

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

e.g.

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


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

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

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

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

Cheers

Dave P

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




Re: Read a huge text file in perl

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

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

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

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

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

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

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

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

close $FILE;

print "Saw $lines lines\n";


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

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

Cheers

Dave P

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




Re: captcha

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

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

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

So - how are you running this code? 

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




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

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

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

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

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

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




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

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

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

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

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

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

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




Re: TLS and Perl

2018-01-25 Thread David Precious

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

Well, how do you interact with Authorize.net?

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

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

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

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

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

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

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




Re: Getting the results of a remote script

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

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

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

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

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


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

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

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


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

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




Re: Device::SerialPort Am I Missing Something?

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

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

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

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




Re: Problems Receiving this List

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

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

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


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

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


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

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


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

Cheers

Dave P

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




Re: "Information station" using Perl and Raspberry Pi

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

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

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

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

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

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




Re: How to match words with a consecutive character

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

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

Demo:

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

use strict;

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

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

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

Cheers

Dave P (bigpresh)

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




Re: Problem using Email::Mailer on laptop

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

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

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

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

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

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

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

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

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


Cheers

Dave P  (BIGPRESH)

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




Re: Strange HASH(0x2ced735) values

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

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

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

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

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




Re: having trouble understanding the built-in Perl sort with regards to mixed numbers and strings

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

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

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

e.g.:

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

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

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

  111 aaa
  111 zzz
  999 aaa
  999 zzz


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




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

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

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

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



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




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

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

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

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

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


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

use strict;
use Benchmark;

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

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




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


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


Of course, YMMV.



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



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




Re: LDAP server

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

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

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

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

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




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



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




Re: map vs foreach

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

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

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

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

So, e.g.:

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

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

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

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




Re: activity indicator

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

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

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

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


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




Re: Need to understand what this script does.

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

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

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

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

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

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



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




Re: Need to understand what this script does.

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

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

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

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





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




Re: LWP/UserAgent fail authentication:

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

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

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

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

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

From some code of mine that successfully worked:

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

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

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


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

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

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


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



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




Re: Suddenly this script is not working!

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

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

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

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




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




Re: Can't Install Mojolicious on Shared Server

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

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

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

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



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




Re: Suddenly this script is not working!

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

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

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

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






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




Re: module installation error from cpan cli

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

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

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

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



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



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




Re: Apologetic request for simple one-off script

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

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

Is the email address always the last field?

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


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

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

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

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



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



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




Re: script to match a valid email id

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

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

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

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

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




Re: script to match a valid email id

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

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

*This*.

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

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

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

However, even that could trip you up.

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


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




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




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

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

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

use lib './lib';

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



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




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

2014-06-02 Thread David Precious

Hi Priyal,

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

The error was:

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

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

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

So, you'll need to either:

(a) upload as JPRIYAL, not as JUNIPER;

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

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




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



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




Re: How to change username in Pause account

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

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

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

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

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



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




Re: close dbh in the subroutine

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

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

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



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




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

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

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

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

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

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

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

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

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

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


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



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




Re: time based test

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

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

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

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

Install it, then e.g.:

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

(See the docs for the various options etc.)


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



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




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

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

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


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

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

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

Cheers

Dave P 


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



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




Re: baby perl to get the right date

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

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

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

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


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



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




Re: Still Trying to Understand Perl Module Control Process

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

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

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

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


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



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



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




Re: series of numbers to range notation

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

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

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

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


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

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

$lastnum = $num;
}

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


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

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

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


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




Re: Gigantic file size processing error

2014-01-02 Thread David Precious
On Thu, 02 Jan 2014 12:19:16 -0500
Uri Guttman  wrote:
> On 01/02/2014 12:08 PM, David Precious wrote:
> > Oh, I was thinking of a wrapper that would:
> >
> > (a) open a new temp file
> > (b) iterate over the source file, line-by-line, calling the provided
> > coderef for each line
> > (c) write $_ (potentially modified by the coderef) to the temp file
> > (d) finally, rename the temp file over the source file

[...]
> it wouldn't be a bad addition to file::slurp. call it something like 
> edit_file_loop. if you write it, i will add it to the module. you can 
> likely steal the code from edit_file_lines and modify that. i would 
> document it as an alternative to edit_file_lines for very large files.
> 
> it will need pod, test files and good comments for me to add it.
> credit will be given :)

Righto - I'll add it to my list of things awaiting tuit resupply :)



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



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




Re: Gigantic file size processing error

2014-01-02 Thread David Precious
On Thu, 02 Jan 2014 11:56:26 -0500
Uri Guttman  wrote:

> > Part of me wonders if File::Slurp should provide an in-place (not
> > slurping into RAM) editing feature which works like edit_file_lines
> > but line-by-line using a temp file, but that's probably feature
> > creep :)  
> 
> that IS tie::file which i didn't want for efficiency reasons. it has
> to read/write back and forth every time you modify an element.
> edit_file (and _lines) are meant to be fast and simple to use for
> common editing of files. as with slurping, i didn't expect them to be
> used on .5GB files! :)

Oh, I was thinking of a wrapper that would:

(a) open a new temp file
(b) iterate over the source file, line-by-line, calling the provided
coderef for each line
(c) write $_ (potentially modified by the coderef) to the temp file
(d) finally, rename the temp file over the source file

Of course, it's pretty easy to write such code yourself, and as it
doesn't slurp the file in, it could be considered out of place in
File::Slurp.  I'd be fairly surprised if such a thing doesn't already
exist on CPAN, too.  (If it didn't, I might actually write such a
thing, as a beginner-friendly "here's how to easily modify a file, line
by line, with minimal effort" offering.)


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



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




Re: Gigantic file size processing error

2014-01-02 Thread David Precious
On Thu, 02 Jan 2014 11:18:31 -0500
Uri Guttman  wrote:
> On 01/02/2014 10:39 AM, David Precious wrote:

> > Secondly - do you need to work on the file as a whole, or can you
> > just loop over it, making changes, and writing them back out?  In
> > other words, do you *need* to hold the whole file in memory at one
> > time? More often than not, you don't.
> >
> > If it's per-line changes, then File::Slurp::edit_file_lines should
> > work
> > - for e.g.:
> >
> >use File::Slurp qw(edit_file_lines);
> >my $filename = '/tmp/foo';
> >edit_file_lines(sub { s/badger/mushroom/g }, $filename);
> >
> > The above would of course replace every occurrence of 'badger' with
> > 'mushroom' in the file.
> 
> if there is a size issue, that would be just as bad as slurping in
> the whole file and it would use even more storage as it will be an
> array of all the lines internally.

Oh - my mistake, I'd believed that edit_file_lines edited the file
line-by-line, writing the results to a temporary file and then
renaming the temporary file over the original at the end.

In that case, I think the docs are a little unclear:

"These subs read in a file into $_, execute a code block which should
modify $_ and then write $_ back to the file. The difference between
them is that edit_file reads the whole file into $_ and calls the code
block one time. With edit_file_lines each line is read into $_ and the
code is called for each line..."

and 

"These subs are the equivalent of the -pi command line options of
Perl..."

... to me, that sounds like edit_file_lines reads a line at a time
rather than slurping the whole lot - but looking at the code, it does
indeed read the entire file contents into RAM.  (I probably should have
expected anything in File::Slurp to, well, slurp the file... :) )

Part of me wonders if File::Slurp should provide an in-place (not
slurping into RAM) editing feature which works like edit_file_lines but
line-by-line using a temp file, but that's probably feature creep :)

OP - what didn't work about Tie::File?



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



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




Re: Gigantic file size processing error

2014-01-02 Thread David Precious
On Thu, 2 Jan 2014 23:21:22 +0800 (SGT)
mani kandan  wrote:

> Hi,
> 
> We have file size of huge size 500MB, Need to Manipulate the file,
> some replacement and then write the file, I have used File::slurp and
> works for file size of 300MB (Thanks Uri) but for this huge size
> 500MB it is not processing and come out with error. I have also used
> Tie::file module same case as not processing, any guidance.

Firstly, be specific - "come out with error" doesn't help us - what is
the error?

Secondly - do you need to work on the file as a whole, or can you just
loop over it, making changes, and writing them back out?  In other
words, do you *need* to hold the whole file in memory at one time?
More often than not, you don't.

If it's per-line changes, then File::Slurp::edit_file_lines should work
- for e.g.:

  use File::Slurp qw(edit_file_lines);
  my $filename = '/tmp/foo';
  edit_file_lines(sub { s/badger/mushroom/g }, $filename);

The above would of course replace every occurrence of 'badger' with
'mushroom' in the file.

Cheers

Dave P


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



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




Re: inserting a variable server name into code

2013-12-19 Thread David Precious
On Thu, 19 Dec 2013 12:27:09 -0600
Rick T  wrote:

> I wanted to make the program more portable, which means changing the
> name of the server. My idea was to put the following
> 
>   my $server = “newserver”
> 
> at the beginning of the code, then have the later lines of code use
> this string. That way I’d only have to change the one line when
> moving to another server. What I tried first was
> 
>   use constant TMPL_FILE => "/big/dom/x” . $server .
> “/www/templates/open_courses3.html";
> 
> This gave me a syntax error, as did several variants I tried.

What did the error say?

If the above code is verbatim, it's because you missed the trailing
semicolon after the variable definition - i.e.:

  my $server = "newserver";



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



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




Re: please help correct my script

2013-11-07 Thread David Precious
On Wed, 6 Nov 2013 23:51:04 +
"Wang, Li"  wrote:

> Dear Perl Users
> 
> I have hundreds of input files, named as geneName_paml_formated.mlc
> In each file, there are some contents similar as follows:
> 
> w (dN/dS) for branches:  0.00010 1.07967 145.81217 0.00010
> dN & dS for each branch
>  branch  t   N   S   dN/dS  dN  dS  N*dN  S*dS
>4..5  0.000  1103.3   327.7  0.0001  0.  0.   0.0   0.0
>5..1  0.043  1103.3   327.7  1.0797  0.0144  0.0134  15.9   4.4
[...]

> I want to extract the line start with "5..1" to be extracted and [...]
[...]
> It turns out that it didnot work, and seems that the script cannot
> find my expression pattern $line=~m/^5\.\.1/

You're looking for a line starting with 5, but it doesn't - it starts
with some whitespace, then 5.

/^\s*5\.\.1/ should match.

(\s* allowing zero or more whitespace characters before the 5)



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



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




Re: Help on saving and retrieving data structures

2013-09-13 Thread David Precious
On Fri, 13 Sep 2013 19:10:58 +0800 (SGT)
*Shaji Kalidasan*  wrote:
> I am saving the data structure to a file and retrieving it back
> again, but, when I 'use strict' it is giving the following error
> message
> 
> Global symbol "%game" requires explicit package name

Others have pointed out that the problem will be solved by adding a "my
%game" before you eval the contents of the file.

However, I personally would suggest serialising the data to JSON and
storing that in the file, rather than using Data::Dumper - that way
you've got your data in a more standard format in case you want to work
with it with other tools, and also you're not eval'ing code, so if
someone had decided to edit that file and add, say, system('rm -rf /')
in it, you won't suddenly execute things you didn't expect.

It can be as simple as saying "use JSON" to load JSON.pm, then using
JSON::to_json(\%game) to generate JSON to write to the file, then
saying e.g. my $game = JSON::from_json($json) (where $json is the JSON
you just read in from the file).

Alternatively, you could even look at DBM::Deep, which would abstract
away the saving/loading for you (although you'd lose the bonus of
having stored the data in a standard widely-supported format).

 
> 
> It is working fine without 'strict'. Please help
> 
> [code]
> #Code for saving the data structure
> use strict;
> use warnings;
> use Data::Dumper;
> 
> my %game = (
>             worldcup => {
>             series => "WC 2011",
>             play => ["monday", "wednesday", "friday"],
>             players => [
>                     {name => "dhoni", role => "captain", country =>
> "india"}, {name => "tendulkar", role => "batsman", country =>
> "india"}, {name => "yuvraj", role => "batsman", country => "india"}
>             ],
>             },
>             
>             ipl => {
>             series => "Edition 5",
>             play => ["saturday", "sunday"],
>             players => [
>                     {name => "sehwag", role => "captain", country =>
> "india"}, {name => "muralidharan", role => "batsman", country =>
> "srilanka"}, {name => "gayle", role => "batsman", country =>
> "westindies"} ],
>             },
> );
> 
> $Data::Dumper::Purity = 1;
> open my $fout, '>', 'gameinfo.perldata' or die "Cannot open file
> ($!)"; print $fout Data::Dumper->Dump([\%game], ['*game']);
> close $fout or die "Cannot open file ($!)";
> [/code]
> 
> [code]
> #Code for reading the data structure. Please note that, I have
> disabled 'strict' and 'warnings'
> 
> #use strict;
> #use warnings;
> use Data::Dumper;
> 
> open my $fin, '<', 'gameinfo.perldata' or die "Cannot open file ($!)";
> undef $/; #read in file all at once
> eval <$fin>;
> if($@) {
> die "Can't recreate game data from gameinfo.perldata $@";
> }
> close $fin or die "Cannot open file ($!)";
> 
> print "Name : ", $game{ipl}->{players}->[1]->{name}, "\n";
> print "Role : ", $game{ipl}->{players}->[1]->{role}, "\n";
> print "Country : ", $game{ipl}->{players}->[1]->{country}, "\n";
> [/code]
> 
> I have also tried using 'do' using strict but in vain
> 
> [code]
> #use strict;
> #use warnings;
> use Data::Dumper;
> 
> do "gameinfo.perldata" or die "Can't recreate gameinfo: $! $@";
> 
> print "Name : ", $game{ipl}->{players}->[1]->{name}, "\n";
> print "Role : ", $game{ipl}->{players}->[1]->{role}, "\n";
> print "Country : ", $game{ipl}->{players}->[1]->{country}, "\n";
> [/code]
> 
> [output]
> Name : muralidharan
> Role : batsman
> Country : srilanka
> [/output]
> 
> My question is, how to use the above program with strict enabled?
> 
> Thank you.
>  
> best,
> Shaji 
> ---
> Your talent is God's gift to you. What you do with it is your gift
> back to God.
> ---



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



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




Re: handeling multiple tcp connections

2013-09-04 Thread David Precious
On Wed, 4 Sep 2013 09:40:26 +
Marco van Kammen  wrote:

> Hi All,
> 
> I'm looking for some good documentation on how to handle multiple tcp
> connections at the same time. [...]
> Looked into IO::select and stuff but I'm just missing some good
> simple clean examples.
[...]

As Shlomi said, the event frameworks will be useful if you want to
handle multiple connections within the same process in a powerful and
efficient way.

However, for simplicity, I'd recommend a look at Net::Server, which
makes this kind of thing rather simple, and provides multiple
"personalities" depending on how you want your daemon to work - forking
a new process for each connection, pre-forking so some "worker"
processes are always available, multiplexing to handle multiple
connections within one process, etc.

It takes away a lot of the hard work for you.

https://metacpan.org/release/Net-Server



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



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




Re: Regular expression help pls !

2013-08-23 Thread David Precious
On Fri, 23 Aug 2013 17:06:41 +0100
jet speed  wrote:

> Chaps,
> 
> Please i need help on the regular expression, i have the sample code
> below. I only want to match the entries from the array to the file
> and print the matching line
> 
> for example if i only want to match fc3/23, in my code it prints both
> the lines fc3/2 and fc3/23. How to restrict to exact matches and
> print only ex: fc3/23

[...]
> if ($line = / ^(fc\d+\/\d+/) {

First off, you mean =~ to match against a regex - that's a fairly
meaningless assignment there.  Was that a typo or is that how it
actually is in your code?

Secondly, that regex shouldn't even compile - you've missed a closing
parenthesis.

Perl should have told you something like:

  Unmatched ( in regex; marked by  

You meant:

if ($line =~ /^(fc\d+\/\d+)/) {

Although I would write that a little more clearly - using qr lets you
pick delimiters that avoid you having to escape the slash in the
middle, and using the "x" modifier makes the regex whitespace
insensitive, so you can space things out for readability - and also,
assign the match to a meaningful var immediately:

if (my ($port) = $line =~ qr{^ (fc \d+ / \d+ ) }x) {
# matched, $port contains e.g. fc3/23
}


Also, iterating through the file once for each port you want to look
for is a rather inefficient approach - reverse the logic, and loop
through each line in the file, checking it against each of your ports
of interest within the loop - for example:

open my $fh, '<', $filename or die "Failed to open $filename - $!";
while (my $line = <$fh>) {
chomp $line;
if (my ($port) = $line =~ qr{^ (fc \d+ / \d+ ) }x) {
if (grep { $port eq $_ } @check) {
# $port matched one of the ones you wanted.
    print "$line\n";
}
}
}


Hope that helps?


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



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




Re: Need to login/logout for Domain Users

2013-08-14 Thread David Precious
On Wed, 14 Aug 2013 11:08:01 +0530
Frank Vino  wrote:

> Hi All,
> 
>Here is my requirement could you please help on this.
> 
>   I have Domain Controller for 100 users, and i have client PC
> (windows 7), i would like to login with multiple users for every 5
> minutes in a same client PC.

What have you tried so far?  What are you stuck on?

What is it you actually want to do, besides "log in"?


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




Re: Need to login/logout for Domain Users

2013-08-14 Thread David Precious
On Wed, 14 Aug 2013 13:47:37 +0800
Ken Peng  wrote:

> On 2013-8-14 13:38, Frank Vino wrote:
> > Here is my requirement could you please help on this.
> >
> > I have Domain Controller for 100 users, and i have client PC
> > (windows 7), i would like to login with multiple users for every 5
> > minutes in a same client PC.  
> 
> Do you mean you need to login into the registrar's control panel for 
> domains management?

I suspect the OP was talking about a Windows "Domain Controller" server
- nothing to do with domain name registrars.




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




Re: parsing html

2013-08-08 Thread David Precious
On Thu, 8 Aug 2013 22:42:01 +0530
Unknown User  wrote:

> What would be the best module available for parsing html in your
> opinion? My intention is to parse html that contains a table of 5
> columns and any number of rows

For parsing HTML tables, you want HTML::TableExtract, IMO.

https://metacpan.org/module/HTML::TableExtract

It makes life easy.


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




Re: Social network map

2013-07-15 Thread David Precious
On Mon, 15 Jul 2013 10:47:00 -0400
 wrote:

> Something like Facebook contacts, email contacts. I want to have a
> map of these contacts, which will include all type of interaction
> information .

Still a bit too vague - but I'm guessing you want something that shows
each "person" (Facebook account, email address) as a node on it, with
links between indicating friendships/emails exchanged between them as
connections.

One Facebook app that already does the Facebook side of what you're
suggesting, which I've used in the past, is:

https://apps.facebook.com/friendwheel/

One open-source option I've seen recommended in the past by people is
http://gephi.org/

In the Perl world, a quick search of CPAN turns up Graph::Easy::Edge,
which looks like it can do what you're looking for - define nodes and
paths between the nodes (where a node would be a person, and
the paths are friendships, say):

https://metacpan.org/module/Graph::Easy::Edge

I haven't personally tried it though, so I'm not recommending it from
experience of using it.

Anyway - your description of what you want to do is still very vague -
please try to narrow it down to a more succinct question - tell us what
you've tried, and what you're stumbling upon, etc.

Maybe worth a read:
http://www.bbc.co.uk/blogs/blogcollegeofjournalism/posts/How-to-map-your-social-network

Cheers

Dave P

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



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




Re: Who instantiated an object

2013-05-31 Thread David Precious
On Fri, 31 May 2013 13:13:29 +
"Weidner, Ron"  wrote:

> I have and object with a function called new. (seen below).  What I
> want to do is instead of simply calling die I want to know who called
> "new" without the required params.  (like a stacktrace)  I've tried
> confess, longmess, and caller and they did not produce the caller of
> new.

You want carp - add "use Carp;" to your script, then use "carp" instead
of "die", and it'll report the error as being where your code was
called from instead.

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




Re: help with some code

2013-05-23 Thread David Precious
On Thu, 23 May 2013 20:45:05 +0100
Rahim Fakir  wrote:

> Iam practicing from start and I have  a problem with this code:
> This is the comment of the console, i coulnd't give a round to it, pls
> help...
> 
> 
> Can't locate Glib.pm in @INC <@INC contains: C:/Dwimperl/perl/site/lib
> C:/Dwimperl/perl/vendor/lib
> C:/Dwimperl/perl/lib .> at janelahello.pl line 3.

This means that you need to install the module Glib.

There are various ways to install CPAN modules; the main "CPAN" client
(run "cpan"), cpanminus (cpanm), etc.

I'm not particularly familiar with dwimperl and I'm not a Windows user,
but I'd imagine running "cpan Glib" from a command prompt should help
get you on your way, assuming dwimperl configures CPAN.pm properly for
you.




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




Re: Spreadsheet::WriteExcel - multi-coloured text in cells

2013-05-15 Thread David Precious
On Wed, 15 May 2013 10:34:02 +0100
Gary Stainburn  wrote:

> Is it possible to write text cells where part of the string is
> highlighted in red? If so, how can I do it?

I'm fairly sure the format of a cell applies to the whole content of
that cell - can you do what you're asking for in Excel or similar
yourself?  If not, then I very much doubt you can do it via
Spreadsheet::WriteExcel.



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



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




Re: script dies when Net::DNS resolve fails

2013-05-13 Thread David Precious
On Mon, 13 May 2013 08:53:13 -0700
Noah  wrote:

> Hi list,
> 
> 
> When Net::DNS resolved name is not found my script dies.  How can I 
> allow for my script to continue on even if there is a failed DNS
> query?

The usual way to catch exceptions is with an eval block or Try::Tiny
etc.

Basic example:

my $source_address = eval { $res->query(); };

if ($@) {
# an error occurred - $@ will contain the message
# do something appropriate here
}

However, $resolver->query() should, according to the docs, just return
undef if no answers are found, and your example output suggests that
execution gets to your "query failed:" warning in the else block, so
something else is happening after that - something which we have no
idea, since we're not seeing the whole code.


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



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




Re: How to install non-most recent versions of the module "warnings"

2013-05-09 Thread David Precious
On Thu, 9 May 2013 14:13:27 -0500
Dariusz Dolecki  wrote:

> I am getting an error while running cpan>install warnings, I have the
> latest stable release of perl.

'warnings' isn't a CPAN module, it's a pragma, part of the core Perl
distribution, you should not need to attempt to install it.

The error the CPAN client was giving you was trying to explain that, to
get a newer version of 'warnings', you'd need to install a newer
version of perl itself - and it won't do that without being asked
specifically, because that's generally not what you'd want (if you'd
wanted to upgrade perl itself, you'd have said so).

What makes you think you needed to upgrade 'warnings' ?



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



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




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

2013-04-23 Thread David Precious
On Tue, 23 Apr 2013 16:56:55 +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.

Normal hash ordering is unpredictably random.

The Tie::IxHash module on CPAN can be used to obtain the behaviour
you're after, though.



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



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




Re: Perl Script for parsing log files.

2013-04-13 Thread David Precious
On Sat, 13 Apr 2013 21:20:30 +0530
Asad  wrote:

>I have completed O'Reily first book Learning Perl . Started
> writing small perl programs . However I basically do log file
> analysis , hence was thinking is there any perl code around in any of
> the book or if anyone already developed for the following
> requirement :
> 
> Nowadays I manually read the log files to read the errors which are
> either fatal or warning or ignorable . The purpose is I want to
> develop a web page as in I upload this log file and it searches for
> those keywords (fatal or warning or ignorable) and if found those
> keywords display the 10line before it and after it .
> 
> Please share your thoughts. Unable to start .

At what point are you stuck?

Learning Perl should cover the basics you're describing - opening a
file, looping over it line by line, and checking if a line matches a
regular expression should all be quite easy for you.

(I would recommend working this out as a standalone script you run
locally to start with, and worry about the handling file uploads etc
afterwards, rather than trying to tackle it all at once.)

When you're looping over the file, you can easily keep the last 5 lines
in a "buffer" (use an array, push() a line to it each time, and if it
contains > 5 lines, shift() the oldest off the front); when you find a
line that matches your pattern, print those five lines, then the line
you're processing, then the next 5.

What you're talking about sounds a lot like "grep -C5 filename",
though - possible wheel re-invention :)

Cheers

Dave P


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



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




Re: Any alternative for substr() function

2013-04-10 Thread David Precious
On Wed, 10 Apr 2013 08:45:40 -0700
Bob goolsby  wrote:

> Before you go off on a goose chase, how do you know that substr() is
> going to be a problem for you?  Have you bench-marked it?  If your
> file is as large as you say, I strongly suspect that your bottleneck
> is going to be I/O and any differences between unpack() and substr()
> will be lost in the noise band below 1%.

^^ This.

Kavita, I'd strongly advise profiling your script with Devel::NYTProf
to see exactly where the time is being spent; it can be a surprise
sometimes.

Once you've got some profiling data showing you where the most time is
spent, you know where to focus your efforts.


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




Re: Trying to parse a URL string

2013-03-24 Thread David Precious
On Sun, 24 Mar 2013 11:48:28 -0700
Angela Barone  wrote:

> Hello,
> 
>   I'm trying to do the following, but I'm not having much luck.
> 
> 
> use strict;
> use warnings;
> use 5.010;
> use CGI qw(param);
> 
> my %q;
> my $referrer =
> 'http://www.bing.com/search?q=stainless+wire+rope&t=';
> 
> my $q = CGI->new($referrer);
> say '$q = '. $q->param('q');  # 'q' is empty
> 
> 
>   How can I get the 'q' parameter from $referrer?  In case it
> matters, this variable is coming from a db.

URI and URI::QueryParam will do exactly what you need:

  use URI;
  use URI::QueryParam;

  my $uri = URI->new($referrer);
  my $q = $uri->query_param('q');




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



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




Re: First Mech script: entering a form value (another simple thing no doubt)

2013-03-14 Thread David Precious
On Thu, 14 Mar 2013 16:42:52 +
G M  wrote:

> 
> Hi,
> 
> Thanks for replying.  As far as I understand it will return undef if
> no form is found.  The form is there so it should do something I
> would've though, I tried adding in that line anyway using 1 to
> signify the first (and only) form on the page, still get the same
> error though :(

There are no forms on the page.

  DB<6> $mech = WWW::Mechanize->new;
  DB<7> $mech->get(
'http://www.easyjet.com/en/searchpod.mvc/showborderless?aclwidth=279');
  DB<8> x $mech->forms;
  empty array

See also:

[davidp@supernova:~]$ wget -q -O-
http://www.easyjet.com/en/searchpod.mvc/showborderless?aclwidth=279 |
grep -i 'http://www.easyjet.com/EN/Booking.mvc, so
take the field names from the source of the URL you gave before, and
POST them at that URL, and see what happens.

Failing that, you'll have to use the form in your browser while using
an addon like Firebug or something, or capturing HTTP traffic off the
wire, and see what happens, and make your script do the same.



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



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




Re: Email::Mime not able to send array in mesg body

2013-02-20 Thread David Precious
On Wed, 20 Feb 2013 10:41:51 -0800 (PST)
Rajeev Prasad  wrote:

> hello follwoing code is sending empty email body. I am trying to send
> HTML content in body of mesg. It works if I use a string instead of
> array

So, er, use a string?

Why are you trying to pass an array as the body?  What are you
expecting to happen?  The argument name even ends in _str - there's a
clue there as to what it's expecting, no?

You can use join() to turn all the elements of the array into a string
- e.g.

  body_str => join "\n", @array,

... that would give you all the elements in @array, with a newline
after each.


If, however, you're trying to send a multi-part message, and the array
contains the multiple parts, you want the 'parts' param, not 'body_str'.



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



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




Re: matching certain lines

2013-02-09 Thread David Precious
On Sat, 9 Feb 2013 11:06:46 -0600
Chris Stinemetz  wrote:

> I would like to only work with the data that has a line with |68| in
> it print that line and then print each subsequent lines in that match
> /\|7\|\d+\|\d+/ until #END is reached and then repeat for the rest of
> the input data.

OTTOMH,

perl -lne '/\|68\|/ .. /\#END/ && /\|7\|\d+\|\d+/ && print'

For an explanation, look up ".." in perldoc perlop - the flip-flop
operator - it evaluates to true once the first condition (in this case,
the current line matches the regex /\|68\|/ becomes true), and
continues to evaluate to a true value until the second condition is true
(in this case, the current line contains "#END"), at which point it goes
back to false again.  Combining that with a check for the line
containing what you want gets you most of the way there; I think it'll
skip the start & end lines though, so you'll probably want to modify
the last regex to include them.



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



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




Re: Database interface

2013-02-05 Thread David Precious
On Mon, 4 Feb 2013 22:39:18 +
Dermot  wrote:

> This will do what you want but alas I do not think it's really
> suitable for a beginner.
> http://search.cpan.org/~oliver/Catalyst-Plugin-AutoCRUD-2.123610/lib/Catalyst/Plugin/AutoCRUD.pm

And for Dancer, a similar option:

https://metacpan.org/module/Dancer::Plugin::SimpleCRUD

(Disclaimer: I wrote it, so consider me biased :) )


Both will allow you to fairly easily provide an interface to
list/search/create/edit/delete records in a database table via a web
interface.


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



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




Re: how to find item that happened once in an array

2013-01-20 Thread David Precious
On Sat, 19 Jan 2013 19:43:15 -0800
Jim Gibson  wrote:

> Use a hash to count the number of times each item appears in the
> array:
> 
> my %counts;
> $counts{$_}++ for @my_array;
> 
> Then, extract the keys the have a count of 1:
> 
> my @new_array = map { $counts{$_} == 1 } keys %counts;

Yep, that's how I'd do it, but you meant grep, not map :)


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



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




Re: reducing perl cpu usage

2013-01-14 Thread David Precious
On Mon, 14 Jan 2013 11:22:18 +0700
budi pearl  wrote:

> Is there any way to reduce the processor usage by perl?
> I am trying to use nice -19 but proc usage is still 100%

Depends what your code is doing.  If you're doing a continuous loop or
something, yes, you'll eat up CPU.

Devel::NYTProf can be awesome to see where your code is spending most
of its time, so you know where to focus.


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



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




Re: Hi

2013-01-10 Thread David Precious
On Fri, 11 Jan 2013 00:34:14 +0530
bhanu chaitanya abbaraju  wrote:

> Please help me how can I stop perl emails

"stop perl emails"?

Taking a guess that you mean you want to unsubscribe from this mailing
list, each post from the list contains the following header:

List-Unsubscribe: <mailto:beginners-unsubscr...@perl.org>

Drop a blank email to that email address, and it should do the trick.

Cheers

Dave P


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



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




Re: check processes accessing file system

2013-01-10 Thread David Precious
On Thu, 10 Jan 2013 05:01:38 -0800 (PST)
"Paul.G"  wrote:

> Linux/Unix you would use.
> 
> eg:
> fuser -c /tmp
> or
> lsof could also be used.
> 
> 
> I was wondering if there was a perl module that does the same thing.

See Unix::Lsof then, as per my reply - it should do what you need.


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




Re: File:: Find help

2013-01-10 Thread David Precious
On Thu, 10 Jan 2013 17:56:18 +0530
punit jain  wrote:

> I need to get all the subdirectories of test and then read the file
> usersettings under that later on to do some processing. I wrote code
> below :-
> #!/usr/bin/perl
> use strict;
> use warnings;
> use File::Basename qw(basename dirname);
> use File::Find qw(find);
> use File::Find::Rule;
[...]
> find( sub {
> print $File::Find::name;
> if ($File::Find::name =~ /Contacts/ && -s
> $File::Find::name > 0 ) {
> print "$File::Find::name";
> 
>   # do some processing
>   }
>  }, $dir);
> 
> }
> 
> However I get :-
> 
> Use of uninitialized value in print at new.pl line 21.
> Use of uninitialized value in pattern match (m//) at new.pl line 22.

I suspect the issue is likely to be that you're mixing File::Find and
File::Find::Rule - the latter also exports a find() method, so you may
be inadvertently calling File::Find::Rule's find() method, which
doesn't set $File::Find::name.

I would recommend changing your code to simply use File::Find::Rule
exclusively.  Alternatively, call File::Find::find() instead of just
find().


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



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




Re: check processes accessing file system

2013-01-10 Thread David Precious
On Wed, 9 Jan 2013 17:45:51 -0800 (PST)
"Paul.G"  wrote:

> Is there a quck way to test if processes are accessing file system in
> perl?

That's quite a vague requirement.

Find out what processes have a specific file open?

Find out what processes are doing IO at that specific moment?

What is it you actually need to find out?

How you do this may be somewhat OS-specific too, and you haven't
mentioned what OS you're using.

If you're using a Unixy system and want to know what processes have a
specific file open, take a look at Unix::Lsof - e.g.:

  use Unix::Lsof;
  my ($output,$error) = Unix::Lsof::lsof("afile.txt");
  my @pids = keys %$output;

(Checking for error is left as an exercise to the reader.)



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



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




Re: finding head and tail in data structure

2013-01-10 Thread David Precious
On Thu, 10 Jan 2013 17:01:43 +0700
budi perl  wrote:

> Hi,
> 
> I have this following hash:
> 
> #!/usr/bin/perl
> #
> use strict;
> use Data::Dumper;
> 
> my %MYROUTES = (
> "ROUTE-252" => {
> #  src => dest
>427 => "ABEP",
>"ABEP" => 441,
>441 => 427,
>427 => 444,
>444 => "MGWQ",
>"MGWQ" => "CDEF"
> },

You can't have the same hash key twice; you've duplicated 427 there.

Also, you don't need to quote the left side of a fat comma, so you can
just as easily say e.g. ABEP => 441.


> Expected results:
> 
> ROUTE-252: 427 - ABEP - 441 - 427 - 444 - MGWQ - CDEF
> ROUTE-432: Error: can not follow the route!
[...]
> Can someone shed some light how to find head then follow the path
> as above? Many thanks.

What have you tried so far?  What have you got stuck on?

How do you know where the start of the route is?  (Rememeber, hash keys
will come back in essentially random order; so, where do you start?)

If you need to start following from the first key you defined, you'll
need to either have an arrayref of hashrefs to iterate over, or store
the starting point as well as the routes.


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



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




Re: bool test return issues

2013-01-07 Thread David Precious
On Sat, 5 Jan 2013 09:01:26 -0700
Elim Qiu  wrote:

> perl shell is a good idear (I'm using ipython a lot. That's why I like
> to try perl shell), but it really has problems. See attached
> screenshot.

Ah.  Yeah, that's a different animal to Perl.

This list can help you with problems with Perl, but stuff specific to
psh would be better taken up with the psh project directly.

You could raise an issue on GitHub: https://github.com/gnp/psh/issues

Or find the psh mailing lists at:
http://sourceforge.net/mail/?group_id=475

Hope that helps a little?


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



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




Re: bool test return issues

2013-01-05 Thread David Precious
On Fri, 4 Jan 2013 15:43:41 -0700
Elim Qiu  wrote:

> It's on snow leopard, perl version 5.10.0
> 
> 
> 
> print 3 > 1, "\n";
> 
> prints 1 and ignored "\n"

Includes the newline for me.

If I had to hazard a guess, I'd say perhaps when you ran it you
accidentally mistyped "." instead of "," (so you concatenated, so the
code was effectively print 3 > "1\n")

Could that be the case?

Also, extra parenthesis never hurt; while not required in these
examples, they can help aid readability at times and make precedence
clearer to less experienced coders reading your code, removing any
doubt from their minds on what's going to happen.

If my guess above is right, if you'd used parenthesis it would have
still worked - e.g.:

  print 3 > 1 . "\n";

written instead as:

  print( ( 3 > 1 ) . "\n");

... would still have provided the result you were expecting.

(I do of course agree that excessive use of parenthesis where not
needed can instead hurt readability - just providing an example of
where they could have helped.)


The sections in perldoc perlop on precedence and associativity will be
worth a read.


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



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




Re: Array vs List output

2013-01-02 Thread David Precious
On Wed, 2 Jan 2013 16:05:25 +0530
Neeraj  wrote:

> Why do i have white-space between words when printing array while not
> getting white-space when printing individual element.

perlfaq explains this - from perlfaq5:

   Why do I get weird spaces when I print an array of lines?
   (contributed by brian d foy)

   If you are seeing spaces between the elements of your array when
   you print the array, you are probably interpolating the array in
   double quotes:

   my @animals = qw(camel llama alpaca vicuna);
   print "animals are: @animals\n";

   It's the double quotes, not the "print", doing this. Whenever
   you interpolate an array in a double quote context, Perl joins
   the elements with spaces (or whatever is in $", which is a space
   by default):

   animals are: camel llama alpaca vicuna

   This is different than printing the array without the
   interpolation:

   my @animals = qw(camel llama alpaca vicuna);
   print "animals are: ", @animals, "\n";

   Now the output doesn't have the spaces between the elements
   because the elements of @animals simply become part of the list
   to "print":

   animals are: camelllamaalpacavicuna

   You might notice this when each of the elements of @array end
   with a newline. You expect to print one element per line, but
   notice that every line after the first is indented:

   this is a line
this is another line
this is the third line

   That extra space comes from the interpolation of the array. If
   you don't want to put anything between your array elements,
   don't use the array in double quotes. You can send it to print
   without them:

   print @lines;



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



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




Re: Building a small web-app in Perl

2013-01-02 Thread David Precious
On Wed, 2 Jan 2013 10:32:56 +0530
Chankey Pathak  wrote:

> Hi guys,
> 
> I have to make a simple web app which should have the features
> mentioned below:
> 
> 1. User registration - Users can signup and login
> 2. New message creation: User can create and post new messages
> 3. Follow users: Users can follow other users
> 4. User Home Page - On the home page, show the messages of users that
> he follows.
> 
> For this particular task what should I chose - Catalyst, Dancer or
> Mojolicious?

Whichever you feel most comfortable with using, really; all three would
do the job perfectly well.


> I think Dancer would be a good choice for building small web-app in
> less time. What do you say?

As one of the Dancer dev team, I'm somewhat biased, so I won't bother
answering that point :)
 
> I made a small app using Catalyst some months ago but I don't have any
> experience in developing web-apps with Dancer or Mojolicious. So if
> you can add links of good tutorial which relates to my problem then
> that would be very appreciable.

Did you enjoy using Catalyst?  Did you find it easy to work with?  I'm
assuming, since you're asking the question, that you're feeling you'd
like to try something else.

If that's the case, look at the docs for both Dancer and Mojolicious,
and see which one seems to suit your style more; both will be perfectly
capable of what you need, so it's all down to which you'd prefer to
work with.

> PS: Happy New Year! :)

And to you!

Also, sorry you didn't get any reply on IRC; that was about 4am my
time :) 

Incidentally, to make user authentication easier, you might want to
take a look at:

https://metacpan.org/module/Dancer::Plugin::Auth::Extensible

... which is one of mine, so consider that recommendation biased of
course :) I'm fairly sure there's something similar for Mojolicious too.



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



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




Re: Regex help

2012-12-22 Thread David Precious
On Sat, 22 Dec 2012 16:45:21 +0530
punit jain  wrote:

> Hi,
> 
> I have a file like below : -

[snipped example - vcards with mail headers etc in between]


> My requirement is to get all text between BEGIN:VCARD and END:VCARD
> and all the instances. So o/p should be :-
[...]
> I am using below regex  :-
[...]
> Any suggestions ?

You've already had a reply indicating how to solve the problem you were
having with regexes, so I won't touch on that.

What I will advise, is that for any task you're trying to accomplish,
there's a pretty good chance someone has already solved that and made
code available on CPAN that will help you - so always check CPAN first,
to avoid unnecessarily reinventing the wheel each time (unless you're
doing so solely for a learning experience, of course).

In this case, parsing vcards is likely a common task - a quick look on
CPAN turns up Text::vCard::Addressbook:

https://metacpan.org/module/Text::vCard::Addressbook


From the synopsis:

  use Text::vCard::Addressbook;
 
  my $address_book = Text::vCard::Addressbook->new(
  { 'source_file' => '/path/to/address.vcf', } );
 
  foreach my $vcard ( $address_book->vcards() ) {
  print "Got card for " . $vcard->fullname() . "\n";
  }

It will ignore the non-vcard content in the example you provided, and
just provide you easy access to the data from each vcard.

That's a much nicer approach than extracting it yourself with regexes.

Cheers

Dave P


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



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




Re: Tutorial for a menu driven perl script

2012-12-21 Thread David Precious
On Fri, 21 Dec 2012 13:42:18 +1300
newbie01 perl  wrote:

> Hi,
> 
> Does anyone know of any great source/link for tutorials on menu
> driven Perl?

"Menu driven"?

You may need to be more specific.

Perhaps you mean terminal-based, using e.g. curses-based stuff?

Perhaps one of the following might help:

https://metacpan.org/module/Curses::Toolkit
https://metacpan.org/module/Tickit

Or do you mean straightforward text menus (present a list of options,
let the user enter their choice)?

Or do you mean presenting a GUI?

More details needed for anyone to really help you here, I think.



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



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




Re: hash help !

2012-11-15 Thread David Precious
On Thu, 15 Nov 2012 10:46:34 +
jet speed  wrote: 
>   I was able to build the hash, however the file contains approx
> 10,000 entries. but while i print the hash i get only approx 1300
> lines or key=>values.
> 
>  i guess its because, the file has duplicate entries. example below
[...] 
> How do i build all the entries into hash including the duplicates on
> both sides keys and values.

Yes, later lines with the same key would overwrite any previous value
for that key, that's how hashes work.

In your initial mail you didn't mention that there are duplicate keys.

What is it you want to achieve?  Store an arrayref of potentially
multiple values for each key?

If so, for each key & value pair you could say something like:

   push @{ $hash{$key} }, $value;

Then later, e.g.:

say "Values for 22:5a are: " . join ',', @{ $hash{'22:5a'} };

Is that what you're looking for?


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



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




Re: Date::Manip question

2012-11-08 Thread David Precious
On Thu, 8 Nov 2012 12:53:31 +0100
Rob Coops  wrote: 
> Something like the below would do perfectly fine...
> 
> my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
> localtime(time);
> 
> my @months = ( 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG',
> 'SEP', 'OCT', 'NOV', 'DEC' );
> 
> my $current_month = $months[$mon];
> my $current_mont_num = $mon;
> my $previous_month = $months[$mon -1];
> my $previous_month_num = $mon -1;

You'll have an interesting previous month in January :)



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




Re: Array elements in Hash

2012-10-04 Thread David Precious
On Thu, 4 Oct 2012 17:26:23 +0100
jet speed  wrote: 
> I am trying to find the array elements in hash, if it matches then
> print the hash value. Please help me to achieve this.
> 
> Note: array elements matches the first 2 characters of the hash keys.
> 
> 
> @match = ("6c7b00", "6d7b00", "6d9d8f", "6c6863", "6e6632");
> %abc = ('6c' => device1, '6d'=>device5, '6e'=>device3, '6g'=>device9);
> 
> 
> Appreciate your help with this.

What have you tried so far?  Where did you get stuck?

Hint: it should be pretty easy to loop over @match, use substr() to get
the first two chars, then look for that key in the hash.



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



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




Re: Module deleted from cpan

2012-09-29 Thread David Precious
On Sat, 29 Sep 2012 13:35:10 +0100
Ricardo Pais Oliveira  wrote:

> Hi!
> 
> I am using the module WWW::Vimeo::Simple::Video for this small
> personal project http://visualworlds.ricardopaisoliveira.com but it
> stopped working and when I looked on cpan the module was gone.
> 
> I'm new to this world and would like to understand better how this
> words: it's frequent to modules on cpan disappear? 

Not very common in my experience - old modules tend to more often be
left to fester than removed :)


> Is there a list of
> last deleted modules (explaining the reason)? When a module is
> deleted nothing remains? Not even some landing page where it previous
> was?.

Anything that was once on CPAN can usually be found on BackPAN.

In this case, the latest version on BackPAN is:

http://backpan.perl.org/authors/id/A/AL/ALEXBIO/WWW-Vimeo-Simple-0.14.tar.gz

The README for that dist shows the module as deprecated:

http://backpan.perl.org/authors/id/A/AL/ALEXBIO/WWW-Vimeo-Simple-0.14.readme

If you're curious about why it was removed from CPAN, you could try
contacting the author themselves.  I would imagine, though, that the
author decided there were better options out there, and no longer
wished to maintain their module.

Cheers

Dave P


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



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




Re: how to deal with columns in perl

2012-09-17 Thread David Precious
On Mon, 17 Sep 2012 18:29:34 +
"Wang, Li"  wrote:

> Dear List members
> 
> I have three columns of a table.  The example is as follows:
> 
> DBS R^2 genename
> 801 0.27807486057281494 POPTR_0002s00200
> 19031.0 POPTR_0002s00200
> 11030.25852271914482117 POPTR_0002s00200
> 32150.03134157508611679 POPTR_0002s00200
> 24150.010018552653491497POPTR_0002s00200
> 13130.03134157508611679 POPTR_0002s00200
> 34421.0 POPTR_0002s00200
> 26420.25852271914482117 POPTR_0002s00200
> 15401.0 POPTR_0002s00200
> 228 0.03134157508611679 POPTR_0002s00200
> 30990.026160990819334984POPTR_0002s00210
> 75550.80011920929   POPTR_0002s00210
> 44570.014814814552664757POPTR_0002s00210
> 75645.232862313278019E-4POPTR_0002s00210
> 44660.0018315018387511373   POPTR_0002s00210
> 10  0.0036630036775022745   POPTR_0002s00210
> 75655.232862313278019E-4POPTR_0002s00210
> 44670.0018315018387511373   POPTR_0002s00210
> 11  0.0036630036775022745   POPTR_0002s00210
> 2   1.0 POPTR_0002s00210
> 
> I would like to calculate the average value of column 2 while the
> content of column three is the same. In this case, I would like the
> output of my result be as follows: R^2 genename 0.3899163577
> POPTR_0002s00200 0.2314956035POPTR_0002s00210
> 
> I donot know how to deal with columns in Perl. I thought about using
> the idea of hash. But the key of a hash could not be the same.

You'd probably want to use a hash where the key is the genename in the
third column of your input, and the value is an arrayref of each value
you saw - so you can collate values against genenames, then calculate
the average at the end.

Something along the lines of:

use strict;
use List::Util;

# For each row, record the value against the genename in question:
my %values_by_genename;
while(my $line = <>) {
chomp $line;
next if $genename eq 'genename'; # skip header row
my ($dbs, $r2, $genename) = split /\s+/, $line;
push @{ $values_by_genename{$genename} }, $r2;
}

# Now, for each genename, calculate the average value
for my $genename (keys %values_by_genename) {
my $avg = List::Util::sum(
@{ $values_by_genename{$genename} }
) / scalar @{ $values_by_genename{$genename} };
print "$avg,$genename\n";
    }

Of course, you'd be better off parsing the input using Text::CSV, but
the above should give you something to start from.





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



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




Re: Clarification re Perl Script out of control

2012-09-14 Thread David Precious
On Thu, 13 Sep 2012 22:56:18 -0700 (PDT)
jmrhide-p...@yahoo.com wrote:
> 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? My hosting service runs Apache version 2.2.22. I can't
> tell what version of Perl they run. I had been relying on the idea
> that a mannerly Perl interpreter would automatically release
> resources when it runs out of statements to execute. Is it possible
> that I need to include some sort of END declaration? 

No - the interpreter will exit when it's done executing the script.

If it isn't exiting, that would suggest it's still executing it -
likely stuck within one of those while (1) loops.

If your host wanted to be particularly helpful, when they see a process
stuck using up CPU time, they could attach with strace and see what
syscalls it's making at the time, as that *might* give some kind of
clue.

Alternatively, you could sprinkle debugging log messages throughout,
hoping that will let you see by looking at the logs what's going on.

Or, you could see if you can install and use Runops::Recorder which can
trace and record the execution flow of your program.


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



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




Re: POD and Perldoc

2012-09-13 Thread David Precious
On Wed, 12 Sep 2012 12:01:31 -0400
"Weidner, Ron"  wrote:

> I'm looking at documenting perl code I'm working on.  I'm considering
> POD but I don't think it can do what I want.  Example of what I would
> like...
>  
> [code]
> 
> # here I would put POD synopsis etc.
> 
> # here I want to put POD that describes this function
> 
> sub my_echo
> { 
> my ($str) = @_;
> print $str;
> }
>  
> # here I want to put POD that describes this function
[...]

Of course you can; that's almost exactly how most POD is written.

Look at nearly any half-decent module on CPAN, or see perldoc perlpod -
it even has examples.

They key is that =cut ends POD, and POD directives restart it.

e.g.

=head1 NAME

Badger

=head1 SYNOPSIS

my $badger = Badger->new;
if ($badger->alive) {
warn "Can't provision on a live badger, that's cruel";
} else {
$badger->provision( os => 'Linux')
}

=head1 METHODS

=over

=item provision

Performs provisioning of this badger.

=cut

sub provision {
my ($self, %args) = @_;
...
}

=item alive

Checks whether this badger is alive.

=cut

sub alive {
my $self = shift;
return $self->has_pulse;
}

... etc.
 
(Hopefully you get the idea.)



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



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




Re: expect.pm not recognising match string

2012-09-06 Thread David Precious
On Wed, 5 Sep 2012 11:27:28 -0700 (PDT)
Rajeev Prasad  wrote:

> inside ssh tunnel (using Net::Openssh), a pty is opened.
> 
> then an exp object is init to this pty   my $exp=Expect->init($pty);
> 
> 
> using this object i am trying to run about quite a few commands on
> remote system. [...]
> I would like to send the command and then wait fo rit to finish
> (lookout for system prompt) and then execute next command in loop. 

Is there a reason you can't just let Net::OpenSSH do this for you?

From its docs, you can use the capture() / capture2() methods
(depending on whether you need to capture just STDOUT or STDOUT and
STDERR from the process).

For example:

for my $command (@commands) {
my $result = $ssh->capture($command);
}

I believe that should work, no?



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



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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-05 Thread David Precious
On Thu, 5 Jul 2012 09:30:01 -0400
Phil Pinkerton  wrote:
> ok using the example input file I got errors
> 
> Use of uninitialized value $current_resource in string eq
> at /Library/Perl/5.12/SVN/Access.pm line 70,  line 3.
[...]
> Invalid resource format in ! (format 'repo:/path')!

Ah, I recall now I had to uncomment the "#[groups]" line in the example
input you originally supplied before the script would work.


> but substituting a copy of our actual svn access file I get
> 
> Can't add new group cvs_cscda_owners: group already exists!
> 
> So I may have a format issue with the example inout file

Yeah, sounds like there's something potentially wrong with the input
file - have you perhaps defined the cvs_cscda_owners group twice?



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




Re: Something mojolicious

2012-07-05 Thread David Precious
On Thu, 5 Jul 2012 18:03:07 +0800
lina  wrote:

> But on my two webbrowers when I tried localhost:3000 it failed to
> open.

As in connection failed, or gave some other error?


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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-04 Thread David Precious
On Wed, 4 Jul 2012 17:01:35 -0400
Phil Pinkerton  wrote:

> Very nice to know about CPAN.

IMO, CPAN is one of Perl's strongest features :)

 
> got a syntax error though with your script
> 
> syntax error at ./getACLinfo.pl line 51, near "say join"
> Execution of ./getACLinfo.pl aborted due to compilation errors.

Whoops - as Chris pointed out, 'say' requires perl 5.10 or newer; I
should have included 'use 5.010' in the script to indicate that, but
forgot to (I have $PERL5OPT set to '-M5.010' on most boxes).

Chris's mail contains full instructions to work round that problem.


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



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




Re: Newbie Where to start perhaps a hash-table is what I need to learn about ?

2012-07-03 Thread David Precious
On Tue, 3 Jul 2012 09:47:00 -0400
Phil Pinkerton  wrote:

> I was given a project that seems to require Perl
> 
> I could use a sample just to extract a list of names associated with
> a group or repo and print them.
> 
> 1) Assigned a task to extract data fron a text file.
> 2) Output file needs to be very specific, and created monthly
> 3) tried doing in korn shell too complex
> 4) Figured Perl my best option, but don't know Perl
> 5) Got  the Modern Perl in PDF format
> 6) Installed Strawberry Perl
> 
> Output format is simple ??, but extracting the data and printing it
> I haven't a clue where to start. ( I am not trying to get someone to
> write this for me, just a simple extract and print example and point
> me in the right direction )

First pointer - a lot of things you want to do in Perl will be a lot
easier if you consult CPAN to see if someone has done this before and
shared reusable code which will make your life easier.

In this case, you've not described what the input data is, but to me it
looks very much like a Subversion access control file.  So, with that
in mind, I searched CPAN, and found SVN::Access:

https://metacpan.org/module/SVN::Access

Ahah - looks like it takes all the hard work out of things for you -
it'll take care of the parsing, and let you just get at the data.

So, glancing at its docs and whipping something up:

[code]
#!/usr/bin/perl

use strict;
use SVN::Access;
my $acl = SVN::Access->new( acl_file => 'data' );

# Walk through the resources in the ACL file:
for my $repopath ($acl->resources) {
# For this resource, find out what authorisations exist:
for my $auth ($repopath->authorized) {

# $repopath->authorized gives us hashrefs of group => perms
# TODO: I don't know if it might also give individual users, if
# had per-user grants.
my ($group, $perms) = each %$auth;

if ($group =~ s/^@//) {
# For each group, list out the members, along with the repo
# permissions

for my $user ($acl->group($group)->members) {
say join '~', $user, $repopath->name, $group, $perms;
}
} else {
die "Can't handle individual user permissions!";
}
}
}
[/code]

If you install SVN::Access, the above should work for you, and should
produce mostly what you need, e.g.:

ritched2~repo1:/~repo1_devs~r
appalas~repo1:/~repo1_devs~r

Handing the _admn groups as a special case is left as an exercise to
you, but should be reasonably easy given the above as a starting point.



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


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




Re: hash sort on value

2012-07-02 Thread David Precious
On Mon, 2 Jul 2012 11:42:44 -0500
Chris Stinemetz  wrote:

> I have a 3 deminsional hash that I would like to sort on the value in
> descending order. I can't quite seem to figure it out
>
> The portion of the program I can't get figured out:
> ## body
> foreach my $frameNo ( keys %RNC ) {
>   foreach my $ec ( keys %{$RNC{$frameNo}} ) {

The second foreach could become:

foreach my $ec (
sort  { $RNC{$frameNo}{$a} <=> $RNC{$frameNo}{$b} }
keys %{ $RNC{$frameNo} }
) {

}

(If you want to swap the sort order, just swap the $a and $b round.)


(For what it's worth, I tend to use "for" instead of "foreach" as I
think it reads better, but either way will work.)



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




  1   2   >