Newbie needs basic help - JW69


I feel like I have made some real progress on a web site that I have been working on, but now I am stuck with some basic stuff that both makes me feel stupid and pisses me off. If I ...
Posted On: Sunday 25th of November 2012 11:41:19 PM Total Views:  189
View Complete with Replies

RELATED TOPICS OF Perl PROGRAMMING LANGUAGE




Devel::SmallProf claims "return 1" needs much time !?

I'm trying to do some profiling on DBM:eep. I started with Devel:Prof and Devel::Profile but for some subs I had no idea *why* they took long, so I tried Devel::SmallProf for additional detail. Much of what I see makes sense but results like this puzzle me (these are the subs that do the low-level disc read and write): count wall tm cpu time line 0 0.00000 0.00000 103:sub print_at { 314000 0.96161 3.67000 104: my $self = shift; 314000 0.88398 3.34000 105: my $loc = shift; 0 0.00000 0.00000 106: 314000 1.51351 3.89000 107: local ($/,$\); 0 0.00000 0.00000 108: 314000 1.07561 3.94000 109: my $fh = $self->{fh}; 314000 4.39820 7.83000 110: if ( defined $loc ) { 0 0.00000 0.00000 111: seek( $fh, $loc + $self- 0 0.00000 0.00000 112: } 0 0.00000 0.00000 113: 314000 3.46850 6.32000 114: print( $fh @_ ) or die "Internal Error 0 0.00000 0.00000 115: 314000 6.65324 9.86000 116: return 1; 0 0.00000 0.00000 117:} 0 0.00000 0.00000 118: 0 0.00000 0.00000 119:sub read_at { 500507 1.43996 6.05000 120: my $self = shift; 500507 1.60207 5.78000 121: my ($loc, $size) = @_; 0 0.00000 0.00000 122: 500507 2.44265 7.01000 123: local ($/,$\); 0 0.00000 0.00000 124: 500507 1.43653 5.88000 125: my $fh = $self->{fh}; 500507 7.14111 11.73000 126: if ( defined $loc ) { 0 0.00000 0.00000 127: seek( $fh, $loc + $self- 0 0.00000 0.00000 128: } 0 0.00000 0.00000 129: 500507 1.41859 6.08000 130: my $buffer; 500507 4.36185 8.77000 131: read( $fh, $buffer, $size); 0 0.00000 0.00000 132: 500507 7.96107 12.04000 133: return $buffer; 0 0.00000 0.00000 134:} Much of the time is spent in "seek", "print" and "read" -- as expected (although I'm surprised it's only tenths of microseconds per call -- I suppose disc caches work their wonders...). But even more time is spent in the two "return" statements, and I don't know why! I thought that maybe they contain the overhead of function calling/returning but coudn't verify that in a simple testscript. Is this an artifact of the perl debugger Or what else is the cause for this
VIEWS ON THIS POST

93

Posted on:

Tuesday 16th October 2012
View Replies!

A good Perl book for newbies/beginners using Windows?

! I am looking for a good hardcopy Perl book for newbies/beginners (including myself) who use Windows (not UNIX/Linux) and that isn't so old. After talking to my Linux friends who use Perl, they suggested these two: 1. http://oreilly.com/catalog/9781565923249/ (1997 though and mentions Windows NT) 2. http://www.amazon.com/Programming-Pe...ref=pd_sim_b_1 (has UNIX references; from 2000). However, my boss said "They seem a bit out of date. Perl has evolved from 1997 and 2000. We may need something more current." Any suggestions on recent revisions for Perl newbies who use Windows (XP-Vista) Thank you in advance. -- "An ant can do more than an ox that is lying down." --unknown /\___/\ / /\ /\ \ Ant @ http://antfarm.home.dhs.org (Personal Web Site) | |o o| | Ant's Quality Foraged Links (AQFL): http://aqfl.net \ _ / Please remove ANT if replying by e-mail. ( )
VIEWS ON THIS POST

184

Posted on:

Tuesday 16th October 2012
View Replies!

A site for all ypur programming language needs

A complete tutorial for all your programming language needs such as Java, .Net and many more. A one shop site for all your programming language needs http://www.computenetwork.blogspot.com
VIEWS ON THIS POST

108

Posted on:

Tuesday 16th October 2012
View Replies!

Dumb newbie question: multiple dialogs

Background: I'm a very experienced Perl programmer and have dealt with real time/event driven programs for decades, so little of that aspect of Tk is a problem for me. What I'm having trouble getting my head around is how to do a "multiple screen" application. When it starts, it'll have a window with some buttons and menus on it, but when you select one of the actions, that dialog box should go away and by one appropriate to the action you've selected. I'm fiddling with Tk-izing a program that current uses CGI [*solely* so I could do forms so I could get buttons and drop-down lists, and text input boxes, etc )]. CGI made it easy when you clicked on something I could make the resulting screen look like whatever was appropriate for the action [e.g., one set of actions was a fairly complicated database-update function [involving making coordinated changes in a bunch of joined tables]. So on the first, main, screen there was an button in a form and when you clicked it, you got a screen with info about what you were changing, input-text boxes for various new values, some checkboxes for optional things... then you clicked "update", got a new screen with nothing but some confirmation text on it and an "OK" button, then you went back to the main screen. Here's my best guess for how to organize this with Tk and I'd appreciate some advice if this is moving in the right direction and whether I'm thinking about it properly: For each of the various "areas" of the program, I'll create a TopLevel object (probably in the program's before MainLoop initialization), pack it with whatever is appropriate for that part of the program, and then "withdraw" it (dunno: maybe I can create it withdrawn \t). When the user clicks on, say, the "go do THAT function" button in the main window, I'll "withdraw" the main window and then "raise" the TopLevel for the function, and then vice versa, re-raising the Main Window when the user completes the action and it is time to go back and, in essence, ask "what's next". That reasonable\t
VIEWS ON THIS POST

107

Posted on:

Wednesday 17th October 2012
View Replies!

[newbie]How to call stored procedure using DBIx?

all! I've stored procedures defined in my database schema, and I need to call them in my code. The arbitrary-sql approach won't help since it writes sql statements in source code to act as stored procedures. So could I do that in DBIx Thank you! , On Fri, 28 Dec 2007 22:58:25 +0800, Tony Winslow wrote: > The env: Catalyst, DBIx::Class, MySQL > If I can get a $dbh from what I already have from DBIx::Class, the > problem can be solved. Yet I can figure out a way to get it. See DBIx::Class::Storage:BI: $schema->storage->dbh -- Peter Scott http://www.perlmedic.com/ http://www.perldebugged.com/
VIEWS ON THIS POST

97

Posted on:

Saturday 20th October 2012
View Replies!

perl newbie question...................

Whats wrong with code excerpt below I put file test operators in an array list (arg) and 2 filenames in a 2nd array list (fname). I do a test if ( ("$arg $filename") ne 0) { print "yes\n" } else { print "yes\n" } ---------------------------------------------- #!/bin/perl -w @arg=('-e', '-d', '-r', '-w', '-x', '-S'); @is=("exists ", "directory ", "readable ", "writeable ", "executable ", "socket "); @fname=($0, "perl.exe"); foreach $filename(@fname) { $ndx=0; foreach $argument(@arg) { print "is $filename a ", $is[$ndx++]; if ( ("$argument $filename" ) ne 0) { print "yes\n"; } else { print "no\n"; } } }
VIEWS ON THIS POST

223

Posted on:

Saturday 20th October 2012
View Replies!

Use of "return" in place of "last" (newbie question)?

I am new to perl, and I am wondering whether it is a bad thing to jump out of a foreach loop with return. I have a function which searches for a value in one of several arrays. If the value is found in any of them, then the whole function may as well terminate. At the moment I am setting a flag when the value is found, which stops execution of the later loops. Would it be safe / stylistically sound to do away with the flag by using "return" rather than "last". Example code follows. TIA sub example{ my $found; $found = 0; foreach $item (@list) { if ($value eq $item) { print ("found $value\n"); $found = 1; last; } } .... several other foreach loops follow in which the only thing which differs is the name of the array which is "@list" above, and the test for the flag. return }
VIEWS ON THIS POST

82

Posted on:

Monday 5th November 2012
View Replies!

perl newbie File manipulation

This is how I would do that:CODE$file = $ARGV[0];open(INPUTFILE,$file)||die"couldn't open the file!";while(){\tlast if/Processing table/;}while(){\tlast if/99: VolumeTable/;#\t\t\t\t\tmanipulate the data } Francohttp://www.xcalcs.com : Online engineering calculationshttp://www.megamag.it : Magnetic brakes for fun rideshttp://www.levitans.com : Air bearing pads
VIEWS ON THIS POST

231

Posted on:

Wednesday 7th November 2012
View Replies!

logic help for newbie

Need a little point in right direction.. I have a list of filenames stored in a txt filethat I want to copy from 1 machine to another and check that they copied based on doing keeping track by file id and then a comparison of the filesize to make sure they match afte the copy.\tCould someone just give me a general idea on this\ti'm not sure where to even begin as far as a best method.I figured I'd open the file list then stick them in an array ( or would a hash be more ideal )this is where a lot of my confusion begins with when is the best tiem to use arrays or hash to parsethe files are in the flat file by a file id, size, filename. F1 size file1F2\tsize file
VIEWS ON THIS POST

103

Posted on:

Wednesday 7th November 2012
View Replies!

newbie question - handling email addresses

I'm 100% new to Perl but I've been given a project with it anyway, so I have some pretty basic questions. The script is used to process a form submitted in PHP. I have it working fine, but I need to add an email address field now. i was told that Perl doesn't like the "@" character (which makes sense), and I know that I could use the backslash, but does Perl treat the submitted field as an object, or will I have to add some functionality which adds the backslashes before the form is submitted to the Perl Basically, do I need to even worry about the @ symbol if I'm working with the submitted form fields in Perl, or will the @ still crash the script
VIEWS ON THIS POST

245

Posted on:

Wednesday 7th November 2012
View Replies!

Newbie needs help with IO::Socket (maybe IO::Select)

Hi all I'm playing around with IO::Socket and try to build a small Client/Server App. The Serverpart is working as it's supposed to, but I have some problems with the Clientpart. So I have a couple of Questions: 1. How can a Client react to a Servermessage (Server prints to Socket), when it's not expecting a Servermessage (For example another client connects to the server and kicks the first one or the server is beeing shutdown). If I use the IO::Select approach like in the Serverpart, the Client is blocked and the User can't interact with it anymore. 2. Why does $socket->connected still return the peer-address, eventhough the server is shutdown I'm sure there is a solution for this (probably by using IO::Select), but I can't find any examples of Clientcode using this. Here's what I have (simplified... the "real" Client is using Tk): ------ Star Serverpart ------- #!perl -w use strict; use IO::Socket; use IO::Select; use Data:umper; our $socket; my %users; my $port = 9901; #Socket erstellen my $main_socket = IO::Socket::INET->new(LocalPort => $port, Type => SOCK_STREAM, Reuse => 1, Listen => 1) or die "Kann kein TCP-Server an Port $port sein: @!\n"; my $lesbar = new IO::Select(); $lesbar->add($main_socket); #Pruefen ob ein Client connectet while(1) { my ($neu_lesbare) = IO::Select->select($lesbar, undef, undef, undef); foreach $socket(@{$neu_lesbare}) { if ($socket == $main_socket) { #Neue Verbindung kommt rein... my $neues_socket = $socket->accept(); $lesbar->add($neues_socket); } else { # Hier drin passiert Zeug mit dem Client my $buf = ; if ($buf) { chomp($buf); my @args = split(',', $buf); if ($args[0] eq "AUTH") { auth($args[1], $args[2]); } else { print "Nicht unterstuetzter Command\n"; } } else { # print "Client hat Socket geschlossen\n"; $lesbar->remove($socket); close ($socket); foreach (keys %users) { if ($socket eq $users{$_}{socket}) { print localtime().": User [$_] disconnected\n"; delete $users{$_}; } } } } } } #---- Subroutinen ---# sub write2socket { my $socket = shift; my $text = shift; my $length = sprintf("%04d", length($text)); my $newstring = $length.$text; print $socket "$newstring"; } sub auth { my $usr = shift; my $pass = shift; #Na wer ists denn... my $iaddr = inet_ntoa($socket->peeraddr()); my $rem_host = gethostbyaddr($socket->peeraddr(), AF_INET); my $rem_port = $socket->peerport(); print localtime().": Neue Verbindung von Host: $rem_host [$iaddr:$rem_port]\n"; if (($usr eq "boerni") or ($usr eq "admin")) { if ($users{$usr}) { write2socket ($socket,"User $usr ist bereits verbunden ($users{$usr}{host}:$users{$usr}{port})"); } else { write2socket ($socket,"Verbindung mit User $usr akzeptiert"); $users{$usr}{socket} = $socket; $users{$usr}{host} = $rem_host; $users{$usr}{port} = $rem_port; } } else { write2socket ($socket, "Verbindung abgelehnt"); $lesbar->remove($socket); $socket->shutdown(2); close ($socket); } #print Dumper \%users; #print Dumper \%sockets; foreach (keys %users) { my $handle = $users{$_}{socket}; write2socket($handle, "User $usr hat sich verbunden"); } } ------ End Serverpart ------- ------ Star Clientpart ------- #!perl -w use strict; use IO::Socket; use IO::Select; my $socket; my $usr = "admin"; my $pw = "pass"; my $remote_host = 'localhost'; my $remote_port = 9901; my $svrmsg; my $lesbar = new IO::Select(); $lesbar->add($socket); while () { chomp; if ($_ eq "connect") { connect2server(); } elsif ($_ eq "man_move") { man_move("up"); } elsif ($_ eq "check") { check_status(); } else { print "Nicht unterstuetzt\n"; } } #---- Subroutinen ----# sub connect2server { #---- Socket aufbauen ----# $socket = IO::Socket::INET->new(PeerAddr => $remote_host, PeerPort => $remote_port, Proto => "tcp", Type => SOCK_STREAM) or die "Konnte Verbindung zu $remote_host:$remote_port nicht herstellen: @!\n"; print $socket "AUTH,$usr,$pw\n"; $svrmsg = read_from_sock($socket); print "FROM SERVER: [$svrmsg]\n"; } sub man_move { return if ! $socket; my $direction = shift; print $socket "man_move,$direction\n"; $svrmsg = read_from_sock($socket); print "FROM SERVER: $svrmsg\n"; } sub read_from_sock { my $socket = shift; my $length = 0; my $data; $socket->read($length, 4); $socket->read($data,$length); return $data; } sub check_status { if (! $socket) { print "CHECK: No socket\n"; } elsif ($socket->connected()) { print "CHECK: Connected with ".inet_ntoa($socket->peeraddr())."\n"; } else { print "CHECK: Not connected"; } } ------ End Clientpart -------
VIEWS ON THIS POST

106

Posted on:

Wednesday 7th November 2012
View Replies!

Good intro to perl to a "newbie to programming"

I am looking for a book on Perl that introduces it to a person who is not a programmer, and offers a track along with exercises, etc. That would be for someone who does not know any programming, so it ...
VIEWS ON THIS POST

94

Posted on:

Sunday 11th November 2012
View Replies!

CSV to ~XML (total newbie)

, Never written a line of Perl until now but I think it might be perfect for this little program I need. I have some CSV files that are given to me and I need to spit them in ...
VIEWS ON THIS POST

99

Posted on:

Sunday 11th November 2012
View Replies!

module needs to know its own path

, within a perl module, I need to access content included with this module, but stored in separate files (WSDL definitions in my case). If my module lies in /usr/lib/perl5/xxx/MyModule.pm, the WDSL files could be stored in /usr/lib/perl5/xxx/MyModule/WSDLs/*.wsdl or similar. ...
VIEWS ON THIS POST

105

Posted on:

Sunday 11th November 2012
View Replies!

a newbie encounters difficulties in using module

I'm testing the codes obtained from internet: use Imager; use Imager::Plot; $plot = Imager::Plot->new(Width => 550, Height => 350, GlobalFont => 'ImUgly.ttf'); my @X = 0..100; my @Y = map { sin($_/10) } @X; my @Z = map { 1+cos($_/10) ...
VIEWS ON THIS POST

170

Posted on:

Sunday 11th November 2012
View Replies!

like greping tails - newbie

We work with a system that has hundreds of very large files we have to search through for text strings. At certain times during the day this is a significant enough read/write operation on the disk that we've had to ...
VIEWS ON THIS POST

81

Posted on:

Thursday 15th November 2012
View Replies!

newbie q on grid

I am new to using Tk in perl and having trouble understanding/using the grid layout manager. I am trying to create a 3x3 grid matrix and store values in them. I figure that I would create a main window, followed ...
VIEWS ON THIS POST

107

Posted on:

Monday 19th November 2012
View Replies!

unix perler needs win32 API perl snippet

dear perl experts---I hope this is a request for a one-liner. I usually run linux or OSX, but I now am in need of automating some OCR on Windows. (The OCR vendors only sell affordable software on Windows in their ...
VIEWS ON THIS POST

176

Posted on:

Sunday 25th November 2012
View Replies!

[newbie]Tk::Button

I want to build two button which shut call the same function but depending on a "parameter". In the book Mastering Perl/Tk.I found somthing like this: # Use the same sub for many Buttons $b = $mw->Button(-text => 'Red', ...
VIEWS ON THIS POST

102

Posted on:

Sunday 25th November 2012
View Replies!

newbie to perl

This may seem to be a bit complex for a newbie, but I'm looking for some ideas as to how to go about this. Here's my "pseudo code" Open a file read a line While not at EOF do { ...
VIEWS ON THIS POST

97

Posted on:

Sunday 25th November 2012
View Replies!