more useful options PerlMonks

### The Monastery Gates

 ( #131=superdoc: print w/ replies, xml ) Need Help??

and Create a new user.

New Questions
 Given When Syntax 11 direct replies — Read more / Contribute by Deep_Plaid on Mar 15, 2014 at 11:34 I have run across a situation where I need a Switch statement. After reading up on this online, it is my understanding that there really is no native Switch statement for PERL, and for later versions you should use the given/when construct (I'm using PERL v5.18.1 for Windows). The problem is I can't seem to get the syntax right, even when using a dumbed down example I got from the web. Here's the code I'm trying to run: use strict; use warnings; sub test2 { my $var = 2; my$i; given ($var){ when(1) {$i = "One"; } when(2) { $i = "Two"; } when(3) {$i = "Three"; } default { $i = "Other"; } } print "$var is $i"; } test2(); [download] That gives me the following errors: syntax error at D:/Eclipse_std_kepler_ws/StudyBuildReport/TestGiven.pl + line 9, near ") {" Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 9. Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 10. Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 11. [download] If I can't get this to work, I'll be stuck using an if/elsif construct, illustrated below (this example works fine): use strict; use warnings; sub test1 { my $var1 = 2; my$b; if ($var1 == 1) {$b = "One"; } elsif ($var1 == 2) {$b = "Two"; } elsif ($var1 == 3) {$b = "Three"; } print "$var1 is$b"; } test1(); [download] Any advice you can give would be deeply appreciated. Thanks. What's the best way to use Any YAML (not YAML::Any) 2 direct replies — Read more / Contribute by djerius on Mar 14, 2014 at 17:30 My module doesn't care which YAML is around, just that it can Dump and Load. I'd like to make it YAML module agnostic, as there are several out there. There's YAML::Any, but that ships with YAML, and what if that's not installed? This works; but it ain't pretty: use List::Util qw[ first ]; my $class = first { eval "use$_; 1" } qw[ YAML::XS YAML::Syck YAML::T +iny YAML]; [download] Is there a more refined approach? Thanks! Update 1: fixed code to stop after the first successful load. Update 2: code is even better now Update 3: The code should be polite and use an already loaded module, if possible. use List::Util qw[ first ]; use Class::Load qw[ load_first_existing_class is_class_loaded ]; my @CLASSES = qw[ YAML::Tiny YAML::XS YAML::Syck YAML ]; my $class = ( first { is_class_loaded$_ } @CLASSES ) || ( load_first_existing_class @CLASSES ); [download] Undefined Value Error Message 3 direct replies — Read more / Contribute by Milti on Mar 14, 2014 at 13:50 I have the following code in a PERL cgi which calls a MySQL database. When calling the cgi I get a "Can't call method 'Prepare' on an undefined value". Sometimes one can't see the forest for the trees and vice versa. Can someone show me the error?my $reqpage= param('reqpage'); unless ($reqpage > 1) {$reqpage = "1";} my$pagesize = "5"; my $firstresult = (($reqpage - 1) * $pagesize) + 1; my$lastresult = $firstresult +$pagesize - 1; if ($lastresult >$result_count) { $lastresult =$result_count; } my $offset = (($reqpage-1) * $pagesize); my$limit = "5"; my $result; my @result; my$LastName=param('LastName'); my $n="3"; my$LastName= substr($LastName,0,$n); my $query="$LastName%"; my $sql = "Select AccountID,LastName,FirstName,Location,Specialty,Intr +oduction,View from members WHERE (Location = 'Phoenix, AZ USA' and La +stName LIKE ?) Limit$offset,$limit"; my$sth=$dbh->prepare("$sql"); $sth->execute($query) or die "Connection Error: $DBI::errstr\n"; [download] I will really appreciate any assistance provided!! Passing multiple user data GLADE 2 direct replies — Read more / Contribute by hakim-djz on Mar 14, 2014 at 13:43 Hi all I am making a gui with glade at the moment. Now I've got the gui setup the way I want, but all that's left is to run my scripts from the events. So I have four text entry boxes that are sitting in a container box. I want the values from them, however in glade you can only pass a single parameter as user data to the callback function. Is there any way, or how do you pass the multiple text entry boxes to the callback function? Here is my callback function: sub OK_clicked_cb { my ($widget, $entry) = @_; my$entry_text = $entry->get_text(); foreach (@entry){ my$entry_text = $entry[$i]->get_text(); print "$entry_text";$i++; } [download] Thanks! Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements) 4 direct replies — Read more / Contribute by JDoolin on Mar 14, 2014 at 12:06 I am trying to write a subroutine which will do a global substitution on the global variable $_, then report some debugging information. my ($nobrackets) = (qr/[^\{^\}]+/); &replace(qr/\{$nobrackets\}/,'$1') #This line is changed below. sub replace{ s/$_[1]/$_[2]/g } [download] The code does a nice job of finding the matches, but instead of removing the brackets, and keeping the text, it removes the brackets, and replaces them with the text with the literal '$1'. my ($nobrackets) = (qr/[^\{^\}]+/); &replace(qr/\{$nobrackets\}/,"$1") #Using qq// instead of q//. sub replace{ s/$_[1]/$_[2]/g } [download] The first snippet of code will never replace the $1 with the captured text. The second snippet replaces the$1 before it captures the text. Is there a way to make perl interpolate the $1 after the call, and during the substitution? =========================================================================== Update: 1:49 PM, March 15 (I have posted this clarification below, but I accidentally buried it in a subthread.) Here is the code that I have working. #!/usr/bin/perl$_='{\selectlanguage{english} \textcolor{black}{\ \ 10.\ \ Three resistors connected in series each carry currents labeled }\textit{\textcolor{black}{I}}\textcolor{black} +{\textsubscript{1}}\textcolor{black}{, }\textit{\textcolor{black}{I}}\textcolor{black}{\textsubscript{2}}\tex +tcolor{black}{and}\textit{\textcolor{black}{I}}\textcolor{black}{\tex +tsubscript{3}}\textcolor{black}{. Which of the following expresses the value of the total current }\textit{\textcolor{black}{I}}\textit{\textcolor{black}{\textsubscript +{T}}}\textcolor{black}{in the system made up of the three resistors i +n series?}}.';; $nobrackets = qr/[^\{}]+/; my$pass = 0; while(++$pass <=2){ s/\\textsuperscript\{($nobrackets)\}/ startsuperscript $1 endsuperscri +pt /g; s/\\textsubscript\{($nobrackets)\}/ startsubscript $1 endsubscript/g; s/\\textit\{($nobrackets)\}/ startitalic $1 enditalic/g; s/\\textcolor\{$nobrackets\}//g; s/\{($nobrackets)\}/($1)/g; print "Pass $pass:\n\n". qq{$_}."\n\n\n"; } [download] This produces output as follows: Pass 1: {\selectlanguage(english) (\ \ 10.\ \ Three resistors connected in series each carry currents labeled )\textit{(I)}( startsubscript 1 endsubscript)(, )\textit{(I)}( startsubscript 2 endsubscript)(and)\textit{(I)}( starts +ubscript 3 endsubscript)(. Which of the following expresses the value of the total current )\textit{(I)}\textit{( startsubscript T endsubscript)}(in the system m +ade up of the three resistors in series?)}. Pass 2: (\selectlanguage(english) (\ \ 10.\ \ Three resistors connected in series each carry currents labeled ) startitalic (I) enditalic( startsubscript 1 e +ndsubscrip t)(, ) startitalic (I) enditalic( startsubscript 2 endsubscript)(and) start +italic (I) enditalic( startsubscript 3 endsubscript)(. Which of the following expresses the value of the total current ) startitalic (I) enditalic startitalic ( startsubscript T endsubscrip +t) endital ic(in the system made up of the three resistors in series?)). [download] Notice on pass 1, it removes the inner curly-brackets, and on pass 2, it removes the outer curly-brackets, additional passes could remove more curly-brackets if necessary. What I want(ed) to change was to turn these s///g or s///eeg statements into subroutines, keeping the capture and replacement variables separate. The code works fine as is, but I'm still curious as to whether the variables could be passed to a subroutine. OpenSSH capture perl-command 1 direct reply — Read more / Contribute by Alasharin on Mar 14, 2014 at 12:01 Hi all, i'm searching for a solution / or any ideas to get a better working to the epochal mtime of remote files to compare them and after comparing copy only the youngest file to local-dir. Any ideas? I've tried like this ... but it doesn't work: Read more... (3 kB) .... This was what i want ... but...maybe... someone has an idea to get it work - perhaps with expect? ...And for completeness: here is my workaround ...but i hope there is a better way...: Read more... (3 kB) Thanks and regards Armin Net::SFTP::Foreign timeout value is inaccurate 2 direct replies — Read more / Contribute by wanderedinn on Mar 14, 2014 at 11:54 So, we have been using Net::SFTP::Foreign for some time now. It appeared to me that the timeout value was not accurate and I recently discovered an interesting issue. When I set a timeout value (which the docs indicate is seconds) my actual timeout is 4X the value I set. Thus, a timeout => 5 translates to 20 seconds. I've even tested timeout => 2.5 and sure enough, it times out in 10 seconds. Is this by design? I have googled and searched for references to this, but I don't see anyone with the same issue. my time testing method is pretty straight forward printf "before sftp: %s\n", scalar localtime(); my $ftp = Net::SFTP::Foreign->new(host=>"localhost", timeout => 2.5, port => 10022, user => "test", password => "test"); printf "after sftp: %s\n", scalar localtime(); print or die? 3 direct replies — Read more / Contribute by molecules on Mar 14, 2014 at 08:49 Several years ago I "learned" somehow that in Perl we should theoretically check all print statements, because there could be funny system problems like a full disk or other things that we otherwise wouldn't know about. But behold, I was told, autodie will take care of all of that print checking for you. THIS IS NOT TRUE. Just now I read in autodie's documentation on CPAN that it specifically does not check print. So, how important is checking print statements? I assume it would depend on the application and/or system. Once in a blue moon, one of the cluster environments that I use has funny file writing issues (which err on the side of not writing), so it is something I worry about. Post on FB (cron) 2 direct replies — Read more / Contribute by ultranerds on Mar 13, 2014 at 11:28 I'm going up the wall trying top get this going! I'm trying to write a script, which will run via cron - and make posts to my GROUP (not wall, and actual group I manage) So far I've got:  use Facebook::Graph; use LWP::Simple; my$access_token = 'xxx'; my $fb = Facebook::Graph->new( app_id => 378913472248618, secret => 'xxxx', postback => "https://www.facebook.com/connect/login_success.h +tml" );$fb->access_token( $access_token );$fb->add_post ->set_message('I like beer.') ->publish; [download] That works - and posts to MY wall... but how the heck do I get it to post on a group? I've been going round and round in circles with this for the last 2 hours.. and its doing my head in :/ TIA Andy Moose constructor help 2 direct replies — Read more / Contribute by Anonymous Monk on Mar 13, 2014 at 09:54 I'm trying to use and understand Moose better, currently I use it for simple classes but am now stuck. I have some code that reads a json file, parses it and then returns a scalar with the decoded contents. use JSON; open FH, "< myfile.json" or die $!; my$contents = ; close FH; my $results = decode_json($contents); [download] My problem is that I wish to include the defining of the $results value in the class constructor, or possibly as a class method.Unfortunately I can't work out how to do this within Moose.Can anyone help me? How to understand chapter 6 of Higher Order Perl? 5 direct replies — Read more / Contribute by Anonymous Monk on Mar 13, 2014 at 04:12 The first 5 chapters weren't bad at all, but I'm seriously stuck at chapter 6. I can't understand what calls what and where. I tried to follow the examples with the debugger, but it's a bit worthless for that kind of thing. It doesn't help that the examples are really boring and kind of obfuscated. For instance: sub promise(&) {$_[0] } [download] That's right, that thing does nothing. I find this really annoying. Does someone know where I can find better explanations of infinite streams or whatever they are called. Not nesessarily in Perl. Windows Background forking in Perl /HTTP Error Capture 1 direct reply — Read more / Contribute by suzun30 on Mar 12, 2014 at 17:53 Hi I am having a web page which accepts an input excel file and does some processing and presents the output to the user for download via a link. After it does this, it continues to do some more processing on that file in the background, which I am attempting to achieve by using exec(bgscript.pl args). I am running my webpage via IIS 6,on windows platform with activestate perl 5.8. The issue is the exec does not really seem to be happening after the display of the page to the user. The page still waits for the bg process to complete and then displays the output. While in most cases this is almost not noticeable since the processing time is minimal for both operations to complete, but in others its not and I get "page cannot be displayed" error. Is there a way to capture this error or make sure exec does not interfere with the page display. Appreciate any inputs. Thank you
New Meditations
 local::lib - installed by default 3 direct replies — Read more / Contribute by wmehl on Mar 11, 2014 at 11:52 Hello, I have a humble request - that the mod local::lib be installed by default at part of CPAN, and not as a separate install. FTP and HTTP are supported "out of the box", we are asking the same for local file. This would allow a local repo of CPAN to be used out of the box via filesystem. This may mean that local::lib no longer exists, it is rolled into the base CPAN code. It is a chicken/egg scenario, in order to install from fileshare, you have to have local::lib installed. This requires going to the net for CPAN install, which in our case is not allowed. There are work arounds (boot strap, rpm's, etc) but it would be nice to use a local repo native to CPAN out of the box. The use case, many servers (1000) in a commercial environment. We have no direct access to the internet, and we have to use a vetted source for CPAN on a local fileshare, not public repos via FTP or HTTP. We could bring up a local HTTP repo, but would prefer to use fileshare Thanks for your time an consideration. Something to meditate on -- the need for a trendy perl? 10 direct replies — Read more / Contribute by perl-diddler on Mar 11, 2014 at 11:19 New version of vim came out for windows... had x86 support -- went to look -- had support for running scripts in Lua, Ruby, Python 2 and Python 3 and no perl. I asked if it was an error, and 1 version of perl might not be needed vs. 2 for python. The reply I got: Dear Linda, It feels like Perl is not very trendy these days. Support for that man +y languages was added because certain popular plugins are written par +tly (or fully) in these languages rather than Vim Script. Python is p +robably the most popular language for extending Vim right now, perhap +s after Vim Script. Ruby goes next, then Lua. But I've never came acr +oss a single plugin which would rely on Perl to be honest. May I ask +why you think that Perl is so important for that Vim distribution? Regards, Alexander [download] I doubt benchmarks on multiple thread execution would be considered trendy. Ideas? Thoughts? Examples or counterpoints? Job With Fun meditation 2 direct replies — Read more / Contribute by project129 on Mar 09, 2014 at 13:34 Hello dear monks! to be clean in my speech: I just looking for my next telecommute contract with mojolicious and modern (sure depends on what you think modern perl are :)) perl. And can not find anything real during last 3 months... I am not too greedy (really my hourly rate are not huge), I am not (suppose) too lazy or (really suppose :) ) too stupid ... but all proposed projects was related to old cgi style or (not interested) catalist framework :( I just wanna my next project to be interesting and use tools that really interesting for me :( Am i dream too much? My wishes (work with fun) not real? Please share you experience: - are things are going really bad (for my expectations) in perl market? (support old/poor coded apps dominate) - any success story? - any mojo fans here? :) sorry for disturbing you with best regards Nick

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-03-16 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?