Keep It Simple, Stupid PerlMonks

### Seekers of Perl Wisdom

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

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
 Passing multiple user data GLADE 1 direct reply — 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(); How can I reduce this with a REGEX? 6 direct replies — Read more / Contribute by misterperl on Mar 14, 2014 at 11:30 I'm not too shabby at REGEXes but this one isn't behaving as I'd expect. I want to remove all redundant numbers and their separators in a string (the numbers are always 2 digits) like: 12-12 -> 12 12-12-12 -> 12 12-13-12-13 -> 12-13 12-12-13-13- -> 12-13 12-13-13-14 -> 12-13-14 and so on. I figured this was a good start: s/(\d\d)(-\d\d)*?(-\1)/$1$2/g; but although it DOES remove a number, it (to me, unexpectedly) only removes one. I read that as: FIND A NUMBER LOOK FOR other seperator / numbers, miminally find a seperator and matching number to the first one replace it with the initial number and the minuimally matched stuff repeat I get this: 12-14-12-12 -> 12-14-12 I realize I could put this in some sort of while () loop until it no longer matches but that seems like it should be unnecessary. I know I need a non-consumiung match on the (-\d\d)*? part so I tried: ?=(-\d\d)*? instead, but then it never matched ANYTHING.... Wise ones please enlighten me? Perhaps there is a switch that tells the regex to go back to ^ after each substitution? Thank-You. use telnet to router with expect 1 direct reply — Read more / Contribute by fionbarr on Mar 14, 2014 at 08:56  use strict; use warnings; use Expect; my $command = Expect->spawn("telnet 000.000.000.000"); print "looking for 'login:'\n"; unless($command->expect(10, "login:")) { die "timed out"; } print $command "\n"; unless($command->expect(60, -re=> 'assword: ?')) { die "timed out"; } print $command "\n"; unless($command->expect(60, -re=> 'cr.*')) { die "timed out"; } print "------------------\n"; print $command "show inventory\n";$command->hard_close(); [download] I am using the above to run the command 'show inventory' on a router I am logging into. The command does not run, should it work? Am I going about this wrong? Device::USB ubuntu 13.10 and bulk read write problem 4 direct replies — Read more / Contribute by AlexSH on Mar 14, 2014 at 08:51 Hello I'm install Device::USB from apt-get on my Ubuntu machine. I have usb device. With wireshark i get protocol from anower software. The protocol used to communicate bulktransfer but when i try implement it with Device::USB i have no luck #!/usr/bin/perl -w use strict; use Device::USB; my $usb = Device::USB->new(); my$dev = $usb->find_device( 0x2642,0x0669); printf "Device: %04X:%04X\n",$dev->idVendor(), $dev->idProduct();$dev->open (); $dev->claim_interface();$dev->set_configuration (0); my $cmd =$dev->{config}->[0]->{interfaces}->[0]->[0]->{endpoints}->[0 +]->{bEndpointAddress}; my $string =$dev->{config}->[0]->{interfaces}->[0]->[0]->{endpoints}- +>[1]->{bEndpointAddress}; my $data =$dev->{config}->[0]->{interfaces}->[0]->[0]->{endpoints}->[ +3]->{bEndpointAddress}; my $send =$dev->bulk_write($cmd,hex('dd'),1000); my$data; my $read =$dev->bulk_read($string,$data,64,1000); $read =$dev->bulk_read($string,$data,64,1000); [download] I can't see any output in wireshark. And i have $send =-1 and read='-1' What i'm missing ? PS sorry for English 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. system() and output redirect 2 direct replies — Read more / Contribute by llancet on Mar 14, 2014 at 03:32 Perl's system() function has two modes: if you give one argument, it will have this string parsed by a "shell"; if you give an array, it will use the first one as program name, and directly pass the following things to the program without the process by a "shell" We know that the second mode is more safe, as it avoids the arguments with spaces being splitted into multiple arguments. So we should prefer the second mode. However, I don't know how to use ">file" in the second mode. If you provide it, it will be transferred into the program, not performing an output redirection. For example: # This works system("cat foo >bar"); # ">bar" being passed to cat as an argument # and there's no file named ">bar" system('cat', 'foo', '>bar'); [download] Thanks! How to pass arguments in perl in linux system through system command 3 direct replies — Read more / Contribute by sumandas on Mar 14, 2014 at 01:55 I am running script in linux environment but it is not passing the argument.I am calling the system command from one perl script(script1.pl) to call the second one(script2.pl) by passing argument my$configfile=$ARGV[0]; system('path of script in linux/perl script2.pl',$configfile); [download]

text here (a paragraph)

and:  code here to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• Outside of code tags, you may need to use entities for some characters:
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2014-03-16 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?