Donations gladly accepted
If you're new here please read PerlMonks FAQ and Create a new user.
|
New Questions
|
Finding repeat sequences.
4 direct replies — Read more / Contribute
|
by BrowserUk
on Jun 18, 2013 at 14:55
|
|
|
Given a string of arbitrary (long) length that is known to comprise of a number of repetitions of an unknown length substring, (thought the last repetition my be incomplete), how to find that repeat sequence?
Eg. Given 'abcdabcdabcdabcdab' find 'abcd'.
Complications:
- The first (shortest) repetition may not constitute the full repetition.
Eg. In 'abcdabcdabceabcdabcdabceab'; 'abcd' is a false rep; the required rep is 'abcdabcdabce';
- As mentioned, the last part of the string may be an incomplete repetition.
The number of characters 'ignored' at the end of the string should be less than the length of the rep. Is is possible to code that into a regex? I guess it could just be checked afterwards.
I'm assuming that a regex solution would be possible, but I cannot wrap my brain around it today?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|
References for ano subs fixed at compile time?
2 direct replies — Read more / Contribute
|
by LanX
on Jun 18, 2013 at 14:54
|
|
|
Hi
I'm pretty sure the answer is yes, but to be sure:
Are the references to anonymous code blocks in Perl always constant, because they are fixed at compile time?
DB<115> sub tst (&) { print "$_[0]\n" }
DB<116> tst {1,2,3,4} for 1..5
CODE(0xa396560)
CODE(0xa396560)
CODE(0xa396560)
CODE(0xa396560)
CODE(0xa396560)
Is it reliably so? I took a look at B::Concised opcode output and as far as could see it seemed so...
Please note that it is not the case for anonymous hashes
DB<118> sub tst (%) { print "$_[0]\n" }
DB<119> tst {1,2,3,4} for 1..5
HASH(0xa3963f0)
HASH(0xa396940)
HASH(0xa396720)
HASH(0xa3966c0)
HASH(0xa3963f0)
Cheers Rolf
( addicted to the Perl Programming Language)
|
DBI placeholders and like statement
1 direct reply — Read more / Contribute
|
by jfroebe
on Jun 18, 2013 at 12:52
|
|
|
AFAIK, you can't use a placeholder with a SQL like statement. A coworker says you can. Can anyone confirm or deny it?
$query = 'select name from my_table where name like ?%';
my $sth = $dbh->prepare($query);
$sth->execute('Jo');
Basically he is convinced that you can. Nothing I've shown him has convinced him otherwise. Hopefully some prince or princess of the Perl Monks can break this stalemate
UPDATE:
poj pointed out that I was incorrect. The percent sign belongs in the execute() not the prepare query.
|
DBD::Sybase query execute hangs randomly for Sybase IQ 15.4
3 direct replies — Read more / Contribute
|
by cmahajan
on Jun 18, 2013 at 09:36
|
|
|
I am running DBI version 1.616 along with DBD::Sybase version 1.12 to connect to the Sybase IQ 15.4 server through one of my Perl modules. The platform is IBM AIX 5.3.0.0 maintenance level 5300-12
The problem that I am facing is that one of my simple queries randomly hangs for no apparant reason. The query is to select some data from the system view SYS.SYSTABLE. The code runs fine as such but every once in a while the code (execute statement) freezes and appears to be doing noting for a long time and I have to terminate the process forcibly.
Below is a fragment of code that I'm running (all variables are properly defined):
AutoCommit is set to 1
RaiseError is set to 1
$dsn = "dbi:Sybase:serverType=IQ:loginTimeout=240:server=$db_server_if
+c";
my $dbh = DBI->connect($dsn,
$self->get_db_username(),
$self->get_db_password(),
\%connect_options );
my $sqlA =<< "END_SQLA";
SELECT table_id
FROM sys.systable
WHERE table_name like '${uc_table_name}%'
AND length(table_name) = $table_name_len
AND user_name(creator) = '$uc_schema'
END_SQLA
my $sthA = $dbh->prepare($sqlA);
$sthA->execute();
Can someone help me figure out what might be wrong with the code or if any of the modules versions that I am using are incompatible with Sybase IQ 15.4.
Another interesting thing to note is that the same code works fine most of the times, but the problem occurs randomly at random times. I have checked the active connections in my database when the code hangs, there are no deadlocks or blocked processes. I can also see that the connection to DB is being established successfully but I do not see any active executing statements at all for the DB handle.
|
Using __DATA__ from a package
4 direct replies — Read more / Contribute
|
by yoda54
on Jun 17, 2013 at 22:19
|
|
|
package Test;
sub run {
while(<Test::DATA>)
{
print "$_\n";
}
}
__DATA__
1
#!/usr/bin/perl
use strict;
use warnings;
use Test;
Test::run();
|
Unit testing question with Test::Deep
1 direct reply — Read more / Contribute
|
by willjones
on Jun 17, 2013 at 16:20
|
|
|
I have a structure I want to test for that comes back as a hash where one of the values is 'objects' which is an array of hash objects that have 2 fields that should always be present, but a 3rd field that may sometimes be present and sometimes not be present. When it is present I'd like to verify it with a re(...) comparison, but when it isn't present I'd like to prevent an error message telling me so... because it is optional! However if any other fields show up in there that aren't known optionals, I want to know about it. Does this make sense? How can I code this with Test::Deep?
I read through the doc on cpan and noticed the superhash stuff, but I wasn't sure how to apply it with this array situation and also in such a way that met my specific needs. Any counsel would be appreciated... thanks.
my $myObjs = {
alpha => re('^alpha(.*)'),
beta => re('\d+'),
#myOptionalField => re('^opt(.*)'), #sometimes there is a "myOpti
+onalField", but sometimes there is not. What to do?
};
cmp_deeply(
$response,
{
field1 => re('\d+'),
field2 => 2,
field3 => 1,
objects => array_each($myObjs)
},
'testing for valid response structure'
);
|
Smart matching is experimental/depreciated in 5.18 - recommendations?
7 direct replies — Read more / Contribute
|
by coolmichael
on Jun 17, 2013 at 15:39
|
|
|
I've been reading the Perl 5.18 delta, and now that smartmatch is both experimental and depreciated I'm in a bit of a muck. I have a big project that uses given/when quite heavily (28 givens, 85 whens).
What is the recommended way to fix this? I know I can turn off the warning, but I'd rather fix the code. The documentation says that given/when is subject to big changes and might even go away, so disabling the warning is only a temporary fix. An automatic fix would be nice, but I don't mind doing all the work by hand. I have a good test suite, so I'm not concerned about breaking things.
Mostly the given/whens look like this:
given($foo) {
when('abc') {...}
when(['foo', 'bar']) {...}
default {...}
};
|
Making Coro::Socket Override IO::Socket::INET
1 direct reply — Read more / Contribute
|
by jabowery
on Jun 17, 2013 at 12:40
|
|
|
|
|
How can I configure the perl App::Cmd module to complain about incorrect options
2 direct replies — Read more / Contribute
|
by ascobie
on Jun 17, 2013 at 11:31
|
|
|
I'm attempting to use the perl App::Cmd module and a simple test program works fine.
However, if I run the program with an --option that I haven't configured in the opt_spec function (of the sub command I'm invoking), it doesn't complain about an invalid option. I would expect it to do so. Instead it just quietly ignores that option.
I can't see anyway of configuring App::Cmd to complain about invalid options.
Is this possible, or is each sub-command expected to do the checking itself?
Thanks
|
log parser
5 direct replies — Read more / Contribute
|
by alex_fatu
on Jun 17, 2013 at 03:36
|
|
|
hi there, monks!
i am new to Perl.
i am a Java, C/C++/C# programmer, recently i also administrate some servers and i need to make a log parser.
let's say my logs size its aprox 2Go. after some test of parsing a simple text i have the next results:
soft powergrep(windows7) - aprox 5 mins
powershell script made by me (new to powershell also) - aprox 5 min
Perl script - aprox 1 min (maximum) - i think..if i will use regexp it will go to 40 sec..maybe.. (today is the first day using Perl, Powershell, scripts)
what i need from this script. Let's say that my log have errors..like DB error ORA-xxxx etc. If i find ORA-xxxx ..maybe print this in a new log and ..the most important: send an e-mail and a text message on my phone.
I need some ideas, please..best practices..i'm sure that i'm not the only one who needs this script.. maybe one of you have something like this...
any info is very useful
btw..this is the script with 1 min search result:
#!/usr/bin/perl -w
use strict;
use IO::File;
use constant FILE => 'D:\ps-scripts\DirectX.log';
use constant FIND => 'eroare';
IO::File->input_record_separator(FIND);
my $fh = IO::File->new(FILE, O_RDONLY)
or die 'Could not open file ', FILE, ": $!";
$fh->getline; #fast forward to the first match
#print each occurence in the file
print IO::File->input_record_separator
while $fh->getline;
$fh->close;
|
Failing to use SDL
2 direct replies — Read more / Contribute
|
by grondilu
on Jun 17, 2013 at 01:27
|
|
|
Hi monks,
I was reading SDL::Manual, and after having followed instructions for installation (with cpan), I tried the first examples.
Creating a window works. I get a black window with this basic code:
use strict;
use warnings;
use SDL;
use SDLx::App;
my $app = SDLx::App->new(w => 400, h => 400);
sleep(4);
But if I try to draw something, such as a line (as in the book):
use strict;
use warnings;
use SDL;
use SDLx::App;
my $app = SDLx::App->new(w => 400, h => 400, d => 32);
$app->draw_line( [200, 20], [20, 200], [255, 255, 0, 255] );
$app->update();
sleep(4);
Then the screen stays totally black. No error message, no warning. I guess something is wrong with the installation of the library. Any idea on how I could fix that?
UPDATE: It seems that I can make things appear when I call
$app->run();
It's not what is in the manual, though. And when I call this, I can't stop the processus anymore, not even with Ctrl-C :/
|
substr outside of string? (Updated: Fixed in later builds.)
3 direct replies — Read more / Contribute
|
by BrowserUk
on Jun 16, 2013 at 11:11
|
|
|
Update: I spent so long tracking this down that when I discovered the cause I posted before doing the proper checks which show it is fixed in later builds. My apologies.
Any explanations for this?
C:\test>perl
$a = chr(0); $a x= 2**31;
substr( $a, 0, 2**16 ) =~ tr[\0][\1];
substr( $a, 2**16, 2**16 ) =~ tr[\0][\1];
^Z
substr outside of string at - line 2.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|
|
|
New Meditations
|
Main routines, unit tests, and sugar
6 direct replies — Read more / Contribute
|
by stephen
on Jun 13, 2013 at 00:43
|
|
|
Recently, I've been thinking about a really, really minor perl issue: what's the best way to format your script's main routine? I'd also always wondered how you were supposed to unit-test the main routine in your script. I recently came up with an idea (inspired by a brian_d_foy article) that answers both questions for me.
When I first started coding, I just put everything in my main routine at top level-- in global scope. My scripts looked something like this (translated from perl 4):
#!/usr/bin/env perl
use strict;
use warnings;
# Script variables
our $Foo = 'bar';
# Main routine
my ($name, $greeted) = @ARGV;
$name //= 'Horace!';
$greeted //= 'world';
say_hello($greeted);
# Subroutines
sub say_hello {
my ($name) = @_;
print "Hello $name\n";
}
|
RFC: Lexcically scoped methods, a toy example
No replies — Read more | Post response
|
by dcmertens
on Jun 11, 2013 at 17:25
|
|
|
PDL is a very general datatype. To one person, a 2D piddle may represent a collection of samples from a song while to another person a 2D piddle may represent an image. However, when you say use PDL::Image2D, various methods get installed into the PDL package, for all piddles to use. This naturally leads to the careful selection of long-winded names in a defensive approach to not getting your toes stepped on (or not stepping on somebody else's toes).
It occurred to me a few days ago that we could manage this with lexically scoped methods. I don't mean lexically scoped subroutines (i.e. Lexical::Sub) because I really like PDL's method-chaining and want to keep that. Nor do I mean lexically scoped methods that get called on ALL objects, regardless of type (i.e. Method::Lexical) because I only want to (ultimately) modify the PDL method resolution.
|
|
|
New Cool Uses for Perl
|
RSS of Twitter search results after 11 June 2013
1 direct reply — Read more / Contribute
|
by ciderpunx
on Jun 17, 2013 at 10:41
|
|
|
Twitter recently got rid of the ability to get search results as an RSS as part of their API update of 11 June 2013.
I found those feeds rather useful, so I made a little screen scraper that reimplements the functionality without needing to auth against their API (it just pulls the results out of the web search page). I guess this will be good for a while longer, like enough time to switch to statusnet, identica, or whatever.
It might be of use to some others in the monastry and illustrates the power of HTML::TreeBuilder::XPath.
|
|
|
|