Re: A Philosophical Point To Be Made: The Truth
https://www.youtube.com/watch?v=xhAoN7g-xLs That is another attempt to reach you. Please try again if I haven't yet. I'm disappointed there has been no feedback yet. I know that it's off-topic, but at least yell at me for that, geez. :) Regards, -- Brandon McCaig < bamb...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <https://www.bambams.ca/ <http://www.bambams.ca/>> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' On Sun, Oct 17, 2021 at 1:15 AM Brandon McCaig wrote: > Hello All, > > It has been a while. I haven't been active on the list in years. > Something much bigger has been stealing my focus. And now I think > I might be ready to share it with the world. > > Do be warned, I make best efforts to be inclusive of all, but I > only know what I know. But if I'm right I have the answer to all > of this for all of us. > > https://castopulence.org/ > > To summarize, I think I know what the "correction" to > insensitivity is. A better social network. Based upon the truth. > > I wonder too if the Perl developers themselves might be > interested in this discussion because of its relationship to > linguistics and software. Though I dare not try to send it their > way directly without a push (and maybe a direction). > > Feedback, constructive or otherwise, is welcomed and encouraged. > > Regards, > > > > Brandon McCaig < > bamb...@castopulence.org> > Castopulence Software <https://www.castopulence.org/> > Blog <https://www.bambams.ca/ <http://www.bambams.ca/>> > perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. > q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; > tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' >
A Philosophical Point To Be Made: The Truth
Hello All, It has been a while. I haven't been active on the list in years. Something much bigger has been stealing my focus. And now I think I might be ready to share it with the world. Do be warned, I make best efforts to be inclusive of all, but I only know what I know. But if I'm right I have the answer to all of this for all of us. https://castopulence.org/ To summarize, I think I know what the "correction" to insensitivity is. A better social network. Based upon the truth. I wonder too if the Perl developers themselves might be interested in this discussion because of its relationship to linguistics and software. Though I dare not try to send it their way directly without a push (and maybe a direction). Feedback, constructive or otherwise, is welcomed and encouraged. Regards, Brandon McCaig < bamb...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <https://www.bambams.ca/ <http://www.bambams.ca/>> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
Re: data structure for Template Toolkit
Sorry for the noise. This reply was intended for on-list, but apparently I'm a bit rusty and hit the wrong key. On Tue, Oct 30, 2018 at 02:41:46PM -0400, Brandon McCaig wrote: > On Tue, Oct 30, 2018 at 07:34:11AM -0500, Rick T wrote: > > Still, if anyone can point me to a “beginners guide” to using > > CPAN, I’ll take a look at it. > > The easy button for CPAN, or at least the one I'm most familiar > with, is cpanm AKA App::cpanminus. The cpan command is very > low-level and prompts the user repeatedly, making it basically > unusable by a human user. If it is available to you, or you're > able to get it installed, cpanm takes away all of this pain and > automatically fetches, builds, tests, and installs your desired > package and all of its dependencies automatically.. You'd just > invoke: > > $ cpanm Template::Simple > > And with minimal output it should install for you. If not, you > might have to get your hands dirty reading logs to figure out > what is wrong, and what to do about it, but for well maintained > packages in a Unix-like environment that is rare in my > experience.. > > You can read about cpanm and how to install it from the Git repo > readme: > https://github.com/miyagawa/cpanminus/tree/devel/App-cpanminus > > I have not used hosting environments for Perl deployment so I > cannot say whether this is likely to be an option for you. > > On another note, a nice way to control your Perl environment is > by using perlbrew, which allows you to install an up-to-date perl > distribution within your home directory to use, and to likewise > install your CPAN modules within your home directory too. It is > also capable of managing many different perl environments > side-by-side which probably isn't needed, but doesn't hurt to > know about.. > > I'm not sure how difficult it would be to use a perlbrew > environment for your hosting environment, but nevertheless it is > a useful resource to know about. Note that if you do use perlbrew > it has a command to install cpanm too so it might kill two birds > with one stone. Regards, -- Brandon McCaig Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: PGP signature
Re: local and auto flush
On Tue, May 15, 2018 at 06:02:40PM +0100, Andrew Solomon wrote: > $| is a global* variable so you use local to isolate this > change to only where you need it. Just to expand on this idea, this is called "dynamic scope"[1] (as opposed to global or local scope). Effectively a new scope is allocated when you use local and all code within the current *and nested* scopes will see the new value, but as soon as you leave the current scope (by any means, including an "exception"!) it will revert back to its previous value. [1] https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scoping To illustrate it's best to show nested calls: #!/usr/bin/env perl use feature 'say'; use strict; use warnings; our $foo = 1; f2();# => 1 f1(); # ... p(); # => 1 sub p { say $foo; } sub f1 { local $foo = 2; p(); # => 2 f2(); # => 2 } sub f2 { p(); } __END__ Compare that to a lexical variable. You could have defined a lexical variable $foo in `f1`, and within `f1` it would have evaluated `$foo` as 2, but within `f2` inside of `f1` it would have still seen the package variable at 1. And compare that to the package variable itself. If you reassigned the package variable it would remain at 2 forever until some other piece of the program modifed it again. Dynamic scope gives you an isolated, temporary modification of some "global"/package scope variable without affecting other parts of the program. To achieve this in other languages you often have to copy the value, and execute the action in the equivalent of a try...finally construct to restore the original value before leaving the scope, which is far more error-prone and far more difficult to read. As mentioned, unless you limit the scope of `local` there isn't much point. If you use local from the package scope of `main` you're effectively changing it for the entire program. As a result, you'll often see it used withn a do {} statement in such a context to allocate a limited scope for it: my $contents = do { local $/ = undef; # Temporarily set read record separator <$file_handle>; # to undefined to slurp the whole file. }; Regards, -- Brandon McCaig Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: PGP signature
Re: Write a plugin for an IRC bot
On Wed, Mar 16, 2016 at 02:44:21AM -0300, pablo báez wrote: > Hi, Hello, > I'd like to write a plugin for an IRC bot (which is based on > POE::Component::IRC) using POE::Component::IRC::Plugin. I want > it to do the following (this is a simplified version of what I > actually would like it to do): > > When the nick 'foo' sends to a channel > > !greet 20 > > the bot must reply > > Hi foo! > > after 20 seconds. How could I do that? I don't know how to > trigger an event after a specified amount of time. I'm a POE newbie myself, but the POE::Kernel SYNOPSIS contains an example that recursively triggers an event after a second (except for the initial one, I imagine): > POE::Session->create( > inline_states => { > _start => sub { $_[KERNEL]->yield("next") }, > next => sub { > print "tick...\n"; > $_[KERNEL]->delay(next => 1); # *** > }, > }); It appears that delay() is a standard method that can trigger an event after some amount of time. Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamb...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Perl executable problem...
Rui: On Thu, Nov 12, 2015 at 8:53 AM, Rui Fernandes <rui.kep...@gmail.com> wrote: > Hi, Hello: > I'm having a problem compiling a perl script to run in my windows 7 32 bits > environment. It compiles with Perl Dev Kit from active state, but when I run > it (in the command line of dos) it just freezes - no anwer. The compilation > gives no error. Does anyone knows why this might be happening? Ordinarily Perl programs are compiled just before they are run. How are you attempting to run it? I am not familiar with ActiveState's "Perl Dev Kit" (and I imagine many of us wouldn't be). I imagine ActiveState has a support channel though so you could try directing your problem to them. Normally to run a Perl program you would just pass the path of the perl script to the perl executable: C:\Users\bambams\src> perl myscript.pl Hello World! C:\Users\bambams\src> In *nix environments you can use a shebang line (the #! line at the beginning of a script) combined with an executable bit to execute the script directly and have the system execute the interpreter for you: bambams@home ~/src$ ./myscript.pl Hello World! bambams@home ~/src$ In Windows, it might work similarly if there is a file association setup for the .pl file extension. To be safe and consistent it is easiest to just invoke the perl executable manually as in the first example. Does any of this help you to understand the problem? Is ActiveState's "dev kit" actually attempting to assemble a self-contained .exe with your Perl program and the perl interpreter (ignore this suggestion entirely if it's gibberish to you)? I support the suggestion to give Strawberry Perl a try. It's assembled with free (as in speech) software and contains everything you need to build and install most Windows-compatible CPAN modules. Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamb...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Multi line file handling
On Mon, Oct 19, 2015 at 07:48:06PM +0530, Prashant Thorat wrote: > Hi All, Hello, > I have a file with multiple lines ,I want to assign all content to variable > & then want to work on it. > Like want to match pattern /window/ from it. > how can it is possible ? > for example - > > open ( F1 ,"file.txt") || die "$!"; You should always use strict and use warnings. They will catch a lot of gotchas that Perl will normally silently ignore. use strict; use warnings; You should prefer the 3-argument open() as a best practice because it's safer. Also, you should prefer lexical file handles. Note that there's no need to quote a variable in Perl (Perl is not a shell language; variable quoting is not necessary). die $! would suffice. That said, you should probably add some context to that error so that you know what failed. For example: open my $fh, '<', 'file.txt' or die "open: $!"; > $a=; You should avoid using $a or $b as variable names because they have special meaning (i.e., see perldoc -f sort). See `perldoc perlvar' for $/, the input record separator (AKA $INPUT_RECORD_SEPARATOR with use English). If set to undef then you can slurp in the whole file with one read. my $contents = do { local $/ = undef; <$fh>; }; > if (/window/i){print > "it is present\n"; > } It's best to avoid using the default variable, $_, because it can very easily be overwritten from a distance. You originally planned to put the contents into $a, but as noted above that's a bad practice. Instead, I read into $contents. We can match the contents of the file by binding the regex to the contents: if ($contents =~ /window/i) { print "Contents of file.txt matched window.\n"; } Even though a lexical file handle will automatically be closed when it goes out of scope it's still a good practice to manually close it and check for errors. close $fh or warn "close: $!"; Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamb...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Sockets getting stuck
On Tue, Oct 13, 2015 at 6:34 AM, David Emanuel da Costa Santiago <deman...@gmail.com> wrote: > Hi! Hello, > THanks for your suggestion. I assume this was off-list because I didn't get it. Also consider bottom-posting or interleaved posting for easier reading. > I changed my code to: > > sub _read_from_socket{ > my ($select) = @_; > > my ($output, $buffer) = ('', ''); > my $socket = undef; > do { > $socket = ($select->can_read(0.1))[0]; > } while (!defined $socket); You're essentially manually blocking your code here. Until you can read the program will be stuck in that loop, blocked/hung. It's no different than just reading without IO::Select (which probably wastes fewer CPU cycles too). In general it would depend on the protocol you're using. _Should_ there always be data when you go to read or could the remote end have nothing to send you? You can try to rearrange your program to handle the case when there is no data. Instead of looping until can_read(), try printing or logging a diagnostic message and either moving on to other work, putting the program to sleep to try again after some period of time, or exit 1. Perhaps your troubles aren't code and are instead network stability. If your network isn't stable enough to receive the packets then there isn't much you can do within software to fix it. Unless you need socket-level access consider using a higher-level network programming API to save yourself such low-level headaches. The code to deal with sockets robustly is complicated and that wheel already exists in several forms. Check CPAN for something more appropriate. Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: reading from socket
On Thu, Sep 17, 2015 at 10:55:58PM +0200, Chris Knipe wrote: > Hi All, Hello, > Running under Net::Server, I have: Unfortunately, you seem to be neglecting to give us full programs to test with which requires us to do guess work. The best way to get help is to do your best to reproduce the problem with as little code as possible, and post a *complete* program that we can debug without having to do any extra research or writing ourselves. > sub process_request { > while () { You should assign the line to a variable instead of using $_. That will protect you against $_ being implicitly overwritten or being overwritten from a distance. > $_ =~ s/\t//; # Remove any instance of \t from the received > line This will only remove the first occurrence of tab in the string. It won't remove _any_ occurrences. > $_ =~ s/[^[:ascii:]]//g; > my @Command = split(/\ /, $_); There's no need to escape a space character in a regular expression. That would be fine as just / /. Or if you wanted the special awk-like behavior of splitting on multiple white-space characters and trimming the start then you should use " ". > switch($Command['0']) { It doesn't make any sense to quote the 0 here. The string gets converted to an integer index. It should be written as just plain $Command[0]. Additionally, Switch.pm is a hack of a module that uses source filtering to accomplish its goals. It has various limitations and should generally be avoided in production code. I think the preferred mechanism is to simply use an elsif-ladder. I'm not sure if it could be, but it would be interesting if that was the culprit all along. > case m/^\bPOST\b/i { > print $sock "340 Ok, send message\r\n"; > > my $filename = '/tmp/test.txt'; > open(OUTFILE, ">", $filename); > binmode(OUTFILE); > binmode(STDIN); > my $size=0; > while (read(*STDIN, local $_, 4)) { Again you'd be better off using a different variable than $_. $_ can be easily changed on you without being obvious that it has changed. > $self->log(0,Dumper($_)); > print OUTFILE $_; > $size=$size+length($_); I'm assuming all of the lines on the left margin are debugging lines because that's what I do too. I'm ignoring that. > if ($_ =~ /^\.\r\n$/) { This looks suspicious to me, but maybe that's just my ignorance with the protocol. You appear to be reading 4-bytes at a time, but then you're comparing with an extact 3-byte terminator. How do you know that the last 3 bytes will align themselves this way? > Can someone *PLEASE* just help me to get this sorted out? I am > even willing to go off list and pay someone to fix this for me. > It's now a serious, serious issue that I need to get resolved > asap, and more than likely either something very trivial that > I'm overlooking, or a bug somewhere in perl / Net::Server. That's a reasonable route to take. Be sure to let us know if you do get private help so that we know to stop. :) I'll attempt to look at the files from your latter E-mail, but without having a complete program to work with there's no guarantee I'll even be looking in the right ball park. Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: reading from socket
On Wed, Aug 12, 2015 at 12:40:29AM +0200, Chris Knipe wrote: On Wed, Aug 12, 2015 at 12:16 AM, Chris Knipe sav...@savage.za.org wrote: Looking at Data::Dumper - it actually looks like latin1 to me *seriously confused* $VAR1 = L\\xAF\\xF3\\xDDWJL\\x94\\xEB\\xB2\\xF8\\xE1\\xACZ\\x89Ul;\\xB7\\xDE}1 '\\xCA\\xC2\\xE3uZ\\xFF7\\xD2=}\\xA8q:;\@,%:_\x{286}-\\xD8\\xF8\\xF9E\x{2c1}5\\xD6j+Z?\\xA6\\x9F\\xEC7\\xEC\\xBB4'CNld\\xBE\\xE6=J\\xFDL\x{7d4}\\xF9nOz(w\\x83\\xEA\\xD6UHi\\xD5p+\\xBFa\\xADvG\\xD4*a\\xEA\\xBBC\\xD0\x{9631}=J\\x96 \\xFD\\xF9MxD\\x89g\\x9EA\\xBD5\\xB4R\\xA0\\xE5sp\\xF3\\xD2x\\x84\xEC\\x92y\\x91\\\x{7c1}\\xB1\\xA1\\x9C\\xB73BE\\xA9)\\xCAf\\xC8\\xEE+\\xA1b\\x8E\\xFCM!\@\\xBE^\\xB5d; This snippet of data looks like what I perceive the yEncoding to be. If this is the beginning of the message then perhaps the entire stream *is* yEncoded? In that case, decoding the yEnc before processing may be helpful. I don't know what protocol this is... I don't see anything in there that could be mistaken for \r or \n, though I'm not sure how yEnc works. How did you modify the loop to log this? Can you show us the updated program? And yes - there's numerous characters in the the above that actually didn't even make it to the email... http://www.savage.za.org/webpics/perl-encode.png I don't remember mention of an E-mail before. What relationship does that E-mail message have to this program? Note also that you appear to be sending HTML E-mails (see the Internet for why that's evil), which could also potentially mean that what I'm reading in plain text isn't exactly what you intended to send... It all depends on your MUA. You may gain some comfort knowing what you send is what you want to send by switching to plain-text E-mails. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: reading from socket
has to travel through to arrive at your machine. Lots can happen on the network. Packets can be dropped, routed wrong, etc. The packets get reassembled in order once they arrive on your machine and only then can you read the data from them. There's no way to ensure that all of the data is there when you go to read. All you can do is read and hope. There's no guarantee the data will ever get to you. A robust program will be prepared to deal with that. One thing to note is that when you read you're throwing away partial reads of data. If the stream contains more than 512 bytes of data then you'll lose the first n - 512 bytes. If you want to slurp up the entire thing into memory and process it all at once then perhaps at a -1 argument to read() indicating that it should store the read data at the end of the buffer instead of overwriting it. I realize my problem here is the really whacky way in which the data stream is encoded (and that is completely out of my control). But there must be a adequate and proper way to handle this data. I don't think the data stream is encoded overly whacky. I think the problem is just that writing computer software is *hard*. It takes a bit of experience with something knew to wrap your head around it, and until then even things that you know how to do can go wrong. It would be helpful if you could show us a complete program that we could test for ourselves. If you can't share the whole thing then try creating a minimal example. We will figure this out...! Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: reading from socket
Chris: On Tue, Aug 11, 2015 at 02:24:13PM +0200, Chris Knipe wrote: Hi All, Hello, I'm reading binary from a socket, and just like a normal email message on the SMTP protocol (for example), the data is terminated by \r\n.\r\n I'm saying binary because the data stream does include yEnc data (or character codes 127) I'm having issues to exit my read loop when I receive the termination characters... my $numBytesToRead = 512; my $buffer; while ($bytesRead = read($TCPSocket, $buffer, $numBytesToRead)) { if ($buffer =~ m/\r\n\.\r\n$/) { print $buffer; last; } } I'm obviously doing this wrong :( Can anyone perhaps show me the light? I can think of a couple of problems. Firstly, if the handle isn't being read with binmode set then perhaps the \r\n are being converted to \n (if this is Windows)? How are you creating/initializing the socket? You used to be able to set binary mode on a file handle using binmode() like this: binmode $fh; Similarly, the character encoding of the data on the socket could matter. You said there are character codes above 127. Does that mean the encoding is 8-bit such as [extended] ASCII or latin1, or do you mean the character codes are WAY above 127? Character encoding could be another culprit if the \r and \n characters are encoded differently in the stream than you (and Perl) expects. Using the IO layers or the explicit Encode module you should be able to decode the stream into a Perl string that Perl understands properly. You can attach an IO layer to the file handle by passing an additional argument to binmode: binmode $fh, ':encoding(UTF-8)'; Lastly, you're reading from a socket so there's no guarantee that the buffer string is going to necessarily end at the termination boundary. Perhaps the protocol guarantees that, but the socket surely doesn't. You may need to look for that terminating sequence in the middle of the buffer. You could use Data::Dumper and/or various poor-man's debugging techniques to try to identify the problem here. You could write what you read to a file and inspect that file with a hex editor afterward. Alternatively, you could invoke perl's debugger with the -d flag and inspect the program state in real-time. Does any of that help? Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: self Introduction
On Mon, Aug 10, 2015 at 10:41:12AM -0400, Brandon McCaig wrote: I'm afraid that I'm not familiar with OWP so please forgive my ignorance. Errr, OPW*. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: self Introduction
Angela: Hello, On Mon, Aug 10, 2015 at 01:18:12PM +0100, Angela L wrote: Hello, I am a student from cameroon looking foward in doing the opw internship. I am new to perl and i wish to have some directives on how to get a grip of the code base and also contribute to the perl community. I'm afraid that I'm not familiar with OWP so please forgive my ignorance. I cannot help much if you will be working with the perl internals, but if you're learning the Perl language then I might be able to point you in a useful direction. I found the perldoc documentation shipped with Perl to be very useful. It reads almost like a very verbose tutorial. If you have the patience to read through it then it is quite useful. At least, if you have existing programming experience with other languages. You should be able to access it with the perldoc command from a command line interface. perldoc perl I'd suggests going through the Overview and the first few Reference Manual sections as a base... At least, that's what I did. I'm not sure how up-to-date the tutorials there are (Shlomi Fish has already suggested some alternative resources for tutorials that are probably better). You should probably install an up-to-date distribution of perl before doing this so the perldoc documentation will also be up-to-date (see below). Finally, one of the best ways to learn is to become active on this mailing list, ask questions, and try to help others as you learn. That's a good way to help yourself learn and also help others learn with you. And a good starting place to participate in the community. The first step is probably to install perl. On *nix you probably already have a perl installed, though it is likely quite old. An easy way to get an up-to-date perl is perlbrew (http://perlbrew.pl). That is my preferred distribution. On Windows, my preferred distribution is Strawberry Perl (http://strawberryperl.com/). It's a free (libre and gratis) distribution that includes the necessary toolchain for installing CPAN modules (third party Perl libraries, which are central to Perl's power and flexibility). I'm not familiar with the OS X ecosystem, though the *nix approach may work. I recommend using App::cpanminus AKA cpanm to install CPAN modules. It's much more user-friendly than the basic cpan command. P.S., Consider configuring your mail client to send E-mails in plain-text format. This helps to preserve formatting of messages and especially source code, even for those of us that are reading from text-based interfaces. It's also a generally good netiquette and preferred practice on many mailing lists such as this. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: convert linear array to nested structure
On Sat, Aug 1, 2015 at 12:39 PM, Simon Reinhardt si...@keinstein.org wrote: Am 30.07.2015 um 20:42 schrieb Brandon McCaig: I'll give you my 2 cents for whatever that's worth. :) Thanks for your many comments. The updated full project code is below. Feedback is appreciated. (you also find this at github.com/simon0x5b/bookmark-djvu) I can't help thinking that the entire recursive subroutine could be simplified and tidied up a bit by changing the direction of it could you elaborate on this? Generally I meant that your solution was short and simple enough that it probably wasn't worth the extra effort of trying to improve it [unless the code was going to be maintained for a long time]. This is what I came up with in a few minutes. It appears to work, but I reserve the right to be missing something. #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @input = ( { title = 'title 1', level = 1, page = 1, }, { title = 'title 1.1', level = 2, page = 2, }, { title = 'title 1.2', level = 2, page = 3, }, { title = 'title 1.2.1', level = 3, page = 4, }, { title = 'title 2', level = 1, page = 5, }, ); my @output = inflate_outline_tree(\@input); print Dumper \@output; sub inflate_outline_tree { my ($input) = @_; my @output; my %prev = (1 = { kids = \@output }); for my $flat_node (@$input) { my ($level, $page, $title) = @{$flat_node}{qw/level page title/}; my $node = {page = $page, title = $title}; push @{$prev{$level}{kids}}, $node; $prev{$level + 1} = $node; } return \@output; } __END__ Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: convert linear array to nested structure
On Tue, Aug 4, 2015 at 5:06 PM, Brandon McCaig bamcc...@gmail.com wrote: my %prev = (1 = { kids = \@output }); Perhaps a more adequate identifier for this hash would have been %parents. *shrugs* Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: convert linear array to nested structure
= {title = $input_array-[$index]{title}, page = $input_array-[$index]{page}}; With an alias like above you'd get: my $outline_item = {title = $node-{title}, page = $node-{page}}; Alternatively, you could take advantage of slices and get all of the elements of the hash at once: my ($next_level, $title, $page) = @{$node}{qw/level title page/}; ... my $outline_item = {title = $title, page = $page}; if ($index + 1 = $last $input_array-[$index + 1]{level} $level) { ($outline_item-{kids}, $index) = get_recursive_array ($input_array, $index + 1); } More personal preferences: It's a good idea to indent continuation lines (wrapped lines) differently than the nested levels so that it's easier to tell them apart. I also prefer operators on the end of the line because I find that it's easier to read that way. if($index + 1 = $last $input_array-[$index + 1]{level} $level) { ($outline_item-{kids}, $index) = get_recursive_array ($input_array, $index + 1); } else { ++$index; } You could also store index + 1 into a variable. This could become (shortening the length of the condition and making the entire thing easier to read): my $next_index = $index + 1; my $next_node = $next_index = $last ? $input_array-{$next_index} : undef; if(defined $next_node $next_node-{$level} $level) { ($outline_item-{kids}, $index) = get_recursive_array($input_array, $next_index); } else { ++$index; } It's a matter of personal preference, but I find it easier to read this way. push @$result_array, $outline_item; } return ($result_array, $index); } I can't help thinking that the entire recursive subroutine could be simplified and tidied up a bit by changing the direction of it, but it looks like it should work and it's concise enough that it's probably not worth the trouble. sub djvused_unescape { # see djvused(1): DJVUSED FILE FORMATS - Strings # and perlrebackslash my $string = shift; # octal escape sequences $string =~ s/\\([0-7]{3})/pack 'U', oct($1)/ge; $string =~ s/\\(.)/ if ($1 eq \\ or $1 eq \) { Consider using single quotes here for at least the double quote. Also be careful substituting || and or because they do have a different meaning: it makes no difference here, but you may surprise yourself if you don't keep it in mind so it's probably a bad habit to get into using it where it doesn't belong. if ($1 eq '\\' || $1 eq '') { $1; } elsif ($1 eq a) { \a; } elsif ($1 eq b) { \b; } elsif ($1 eq t) { \t; } elsif ($1 eq n) { \n; } elsif ($1 eq v) { \x{0b}; } elsif ($1 eq f) { \f; } elsif ($1 eq r) { \r; } else { die unknown escape sequence in '$string'; } /ge; You should probably indent the replacement body here so that it stands out from the surrounding code. You could also lose some vertical space by joining } and elsif ... lines. Again, it's a style thing, but if you're already going half-way towards sharing a line I don't see the harm in going all the way. ;) $string =~ s/\\(.)/ if ($1 eq \\ or $1 eq \) { $1; } elsif ($1 eq a) { \a; } elsif ($1 eq b) { \b; } elsif ($1 eq t) { \t; } elsif ($1 eq n) { \n; } elsif ($1 eq v) { \x{0b}; } elsif ($1 eq f) { \f; } elsif ($1 eq r) { \r; } else { die unknown escape sequence in '$string'; } /ge; Additionally, consider putting it into a separate subroutine: $string =~ s/\\(.)/expand_escape_sequence($string)/ge; sub expand_escape_sequence { my ($string) = @_; if ($1 eq \\ || $1 eq \) { return $1; } elsif ($1 eq a) { return \a; } elsif ($1 eq b) { return \b; } elsif ($1 eq t) { return \t; } elsif ($1 eq n) { return \n; } elsif ($1 eq v) { return \x{0b}; } elsif ($1 eq f) { return \f; } elsif ($1 eq r) { return \r; } else { die Unknown escape sequence in '$string'; } } return $string } All of my suggestions are untested so copy-pasta chefs beware. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa
Re: To use signatures or not to use?
On Wed, Jul 22, 2015 at 3:11 PM, Alex Becker asb.c...@gmail.com wrote: Dear all, Perl now has subroutine signatures. However, they are marked as experimental feature. As I really like it, I'm always tempted to use it. On the other hand, I don't want to wast efforts to something I have to roll back. So, does anyone know the tendency if signatures are going to stay? I'm not looking for the standard do if you want to do it because TIMTOWTDI answer, it will not help me. I'm looking for rumors, or eventually an assessment from the people who did the feature? This is either the first I've heard of this or I just drink too much. One thing that I notice is that the Signatures section seems quite early and long in the perlsub documentation for such an experimental and new feature. I know that when I first learned Perl (which was 5.x) I did so largely by reading the core perldocs almost like a tutorial. Reading the perlsub now I might quickly forget that signatures are experimental after glossing over the warning and relating the sizable signatures documentation to other languages. A beginner to Perl might consider that the norm or recommended option (after their nightly beer erases the whole experimental disclaimer). It seems to me that Signatures should be towards the end (I mean, do experienced Perl programmers go to these core documents to discover new features or hear about them from mailing lists and the like and then look them up explicitly as I did!?). As for rumors, this is the first that I've heard of it, but it sounds like a relatively safe feature to use assuming they haven't overlooked anything critical in terms of parsing the signature from the language. And I can't imagine anything that would break it. It's probably safe assuming you can require such a modern perl. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Display a hash in the order of an array
Vincent: On Fri, Jul 17, 2015 at 9:11 AM, Vincent Lequertier s...@riseup.net wrote: Hi, Hello, I have the following structure : $hash{$date} = { 'ip' = $ip, 'action' = $action, }; witch produce data like : $VAR1 = '[15/Jul/2015:10:30:03 +0200]'; $VAR2 = { 'ip' = 'xxx.xxx.xxx.xxx', 'action' = 'GET xxx' }; and an array of ip addresses, say @ip My question is how can I display the content of %hash in the order of @ip, assuming %hash has the same length as @ip ? Unfortunately this data structure isn't ideal for doing this. You'll have to search the hash structure for the matching IP addresses. You may wish to transform the data structure first (or simply store it differently in the first place if this is all you need it for). If the hash keys were IP addresses then you could simply loop over the elements of @ip (some would argue arrays and hashes should have plural names) and use the current IP address to address the current hash element from '%hash' (note: hash is usually a poor name for a variable). use strict; use warnings; use Data::Dumper; sub cmp_ip_addresses { my @parts = map { split /\./, $_ } $a, $b; for my $i (0..4) { my $result = $parts[$i] = $parts[$i + 4]; if ($result) { return $result; } } return 0; } # The resulting structure will be inverted so the keys are IPs # and the values are references to hashes containing a date and # action (you could go further and store actions by date, or # dates by action; depends how you want the data shaped). sub transform_date_logs_to_ip_logs { my ($logs) = @_; my %logs; while(my ($date, $entry) = each(%{$logs})) { my ($ip, $action) = @{$entry}{qw/ip action/}; push @{$logs{$ip}}, { date = $date, action = $action}; } return \%logs; } my @ip_addresses = qw( 192.168.200.220 1.0.0.127 192.168.90.221 ); my %date_logs = ( '15/Jul/2015:10:30:01 +0200' = { ip = '1.0.0.127', action = 'GET http://1.0.0.127/foo', }, '15/Jul/2015:10:30:04 +0200' = { ip = '192.168.90.221', action = 'GET http://192.168.90.221/bar', }, '15/Jul/2015:10:30:05 +0200' = { ip = '192.168.200.220', action = 'GET http://192.168.200.220/baz', }, '15/Jul/2015:10:30:03 +0200' = { ip = '1.0.0.127', action = 'GET http://1.0.0.127/foo/bar', }, ); my $ip_logs = transform_date_logs_to_ip_logs(\%date_logs); for my $ip (sort { cmp_ip_addresses() } @ip_addresses) { my $entries = $ip_logs-{$ip}; for my $entry (@{$entries}) { my ($date, $action) = @{$entry}{qw/date action/}; print Date: $date, IP Address: $ip, Action: $action\n; } } __END__ Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Display a hash in the order of an array
On Fri, Jul 17, 2015 at 10:40 AM, Brandon McCaig bamcc...@gmail.com wrote: for my $i (0..4) { Errr, 0..3. _ Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Use of uninitialized value $j in numeric eq (==) at myfile line 125
On Fri, Jul 3, 2015 at 11:10 AM, Nagy Tamas (TVI-GmbH) tamas.n...@tvi-gmbh.de wrote: Hi, Hello, My code: It's evident that this is not actually your code, let alone the full code... Use strict; Use warnings; Those must be 'use', not 'Use'. Obviously you aren't actually using them or at least this isn't actually the code. :P my $j = 0; my $doread = 0; my ($buf, $data, $n); $n = read FILE, $data, 1; $buf = $data; while($n != 0) { $doread = 0; # there has to be a begin tag before the end tag # so we can check for begin tag first if($begintagfound == 0) { If you actually were using warnings and strict you'd be getting an error here for the undefined variable $begintagfound. if($buf eq chr hex $xhtmlbegin[$j + 1]) { $doread = 1; $j = $j + 1; } else { if($j == 0) {... line 125 There are not 125 lines in your entire message. … Says use of uninitialized value $j in numeric eq == at line 125 Does anyone has any idea why does it tell $j as uninitialized? I initialized it at the first line to 0. We can't have any idea what's wrong because we don't have the actual code. If you want actual help with the code then paste a full example that demonstrates the problem. I suspect the issue here is lexical scope combined with the lack of strict, but it could be any number of things. It also appears as though you're trying to write a square HTML parser of sorts. I'd suggest using one of the CPAN modules that already exist for this purpose. Don't roll your own unless you really want square wheels. Feel welcome to post a real example if you want some real help with the real warning. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: problem with regex qr operator
Mike: On Thu, Jun 25, 2015 at 5:42 AM, Mike Martin m...@redtux.org.uk wrote: Hi I am currently getting issues with regexes that use the qr operator. The results with qr are different than without. This is a small sample program to illustrate it use strict; my $str='Database Administrator'; my $pattern= '(?=^(?:(?!(?:datab|network|system)).)*$).*(?:Adm(?:in(?:i?strat(?:ors?|ion|ive)?)?)?|Clerk\b|Clerical|office)(?:.*?ass.*?|supp|temp|staff|officer?|).*?' ; my $pattern2=qr/$pattern/; $str=~/($pattern)/i; print 'no qr',\t, $1,\n; $str=~/($pattern2)/i; print 'with qr',\t, $1,\n; print 'Pattern without qr',\t,$pattern,\n; print 'Pattern without qr',\t,$pattern2,\n; - - - 8 - - - *snip* - - - 8 - - - The only difference I can see is the addition of a non-capturing group around the expression (?^:) Anyone any idea what is happening here If you check out the documentation on (?adlupimsx-imsx) then you will know that it allows activating and deactivating modifiers from within the regex. For example, (?i) is equivalent to /i for the remainder of the pattern. A caret after the ? has an implied meaning: perldoc perlre (Strawberry Perl 5.20.2) said: Starting in Perl 5.14, a ^ (caret or circumflex accent) immediately after the ? is a shorthand equivalent to d-imsx. Flags (except d) may follow the caret to override it. But a minus sign is not legal with it. Directly beneath this is the explanation for (?:pattern), (?adluimsx-imsx:pattern), and (?adluimsx-imsx:pattern). These are for clustering. They apply the specified modifiers to pattern. You can look at `perldoc perlre' for the specifics of what this means, but the '-' means to deactivate and the i means case-insensitive matching as per usual. I believe this is the problem. If you notice your usage of qr// above you aren't using the /i modifier with the qr//. You should be since that is what you meant, and that is where you meant it. If you do include the /i modifier on the qr// then the resulting pattern changes thusly: use strict; use warnings; my $pattern = 'foo'; my $regex_with = qr/$pattern/i; my $regex_without = qr/$pattern/; print with: , $regex_with, \n; print without: , $regex_without, \n; __END__ Output: with: (?^i:foo) without (?^:foo) With the modifier in qr// the i is added back to the pattern. Without it is left off. The modifiers on the m// won't have any affect to the enclosed pattern. An alternative solution is to include the modifier directly in the regex by prefixing it with (?i). For example, if the patterns are dynamic (user-supplied). One can conclude that it works this way so that what you specified in the qr// is preserved regardless of what m// says (if your qr// is case-sensitive then so is the pattern embedded into m//, regardless of what applies to m// as a whole). Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: perl function for byte to MB conversion
Hello, On Mon, May 25, 2015 at 08:20:08PM +0530, Sunita Pradhan wrote: I get following error while loading this module : Can't locate Format/Human/Bytes.pm in @INC (@INC contains: /usr/software/lib/perl5/5.8.8/x86_64-linux-thread-multi /usr/software/lib/perl5/5.8.8 /usr/software/lib/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/software/lib/perl5/site_perl/5.8.8 /usr/software/lib/perl5/site_perl .) at test.pl line 3.BEGIN failed--compilation aborted at test.pl line 3. You need to download and install CPAN modules that aren't included with Perl. There are various command-line clients to do this with varying degrees of automation. I prefer cpanm (App::cpanminus) for being largely automatic and quiet. Once cpanm is installed (installation instructions will depend on your platform) you can install the Format::Human::Bytes module and all of its dependencies with the following command: cpanm Format::Human::Bytes You can also download the module yourself and manually install it by following its installation instructions (this will require repeating this process for each unsatisfied recursive dependeny). That said, from the description of Format::Human::Bytes I'm not sure that it really solves your problem. It seems to be meant for friendly display of byte counts. It seems to automatically choose which unit to display. If that is what you want then it will work for you, but if you actually wanted something to do unit conversions then I didn't have much success searching for a module that contained the logic for that. That doesn't mean one doesn't exist, but I failed to find it. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: last statement in a package
On Thu, May 21, 2015 at 3:41 AM, Vincent Lequertier s...@riseup.net wrote: From http://perldoc.perl.org/perlmod.html#Perl-Modules, 1; is used to end with a true value There is a bit better explanation here: http://www.perlmonks.org/?node_id=781340 In short, `use' is like syntactic sugar around `require' (plus some stuff), which is like syntactic sugar around `do' (plus some stuff). require is the one checking the return value of the module code and raising an exception on falsy values. perldoc -f use perldoc -f require perldoc -f do Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Removing text
On Tue, May 19, 2015 at 1:10 PM, Brandon McCaig bamcc...@gmail.com wrote: 1234567890Richard: Sorry, that should have been just Richard:. I will blame my flaky browser-based user interface. I should have switched to mutt to write that. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Removing text
1234567890Richard: On Mon, May 18, 2015 at 12:27 PM, Richard Taubo o...@bergersen.no wrote: Hi, Hello, The full (bash) script with perl parts looks like this: [$] top_return=$(top -n1 | head -5) [$] io_return=$(printf %s\n $top_return | grep ^Cpu(s)) [$] io_all=$(printf %s $io_return | awk '{ printf $9 }' | perl -pe 's/\%st//') [$] printf %s\n $io_all Since you're using Perl here I wonder why you're bothering with bash and shell commands. Since the output of those commands will vary by system (I can't be sure what you're trying based on my results) it's impossible for us to debug your problem. However, we can demonstrate a more straightforward, Perl-only solution using output from our own system. use autodie; use strict; use warnings; use Data::Dumper; $Data::Dumper::Useqq = 1; main(); exit(0); sub main { # On my system top outputs control characters for colors and such. # This is a simple way to convince it to do nothing special. my $term = $ENV{TERM}; $ENV{TERM} = 'dumb'; my $cpu_stats = parse_cpu_stats(); $ENV{TERM} = $term; print Dumper $cpu_stats; } sub parse_cpu_stats { # Use a pipe to top(1) to read the output directly. open my $fh, '-|', qw/top -n1/; while (my $line = $fh) { chomp($line); # Look for the Cpu line. if ($line =~ /Cpu/) { return parse_pairs($line); } } } sub parse_pairs { my ($line) = @_; # Results in (0.5, 'us', 0.1, 'sy', 0.0, 'ni', 99.1, 'id', # 0.3, 'wa', 0.0, 'hi', 0.0, 'si', 0.0, 'st'); my @pairs = $line =~ /([0-9]+\.[0-9]+)\s+(..)/gi; # Results in (us = 0.5, sy = 0.1 ni = 99.1 id = 0.3, # hi = 0.0, si = 0.0, st = 0.0) my %fields = reverse @pairs; return \%fields; } __END__ This was hacked against the Debian Linux top(1) implementation. Example output is: %Cpu(s): 0.5 us, 0.1 sy, 0.0 ni, 99.1 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st Output from the above program is: $VAR1 = { wa = 0.3, hi = 0.0, ni = 0.0, st = 0.0, us = 0.5, id = 99.1, si = 0.0, sy = 0.1 }; I'm not saying this is a better or more robust solution than hacking it up in bash, but at least by using Perl you have the full power of Perl without switching between bash, grep, awk, and perl. The Interwebz suggests that checking /proc would be a better solution, though that would vary by the *nix system. In general, you should prefer CPAN modules for this since the solution is flaky, and hopefully somebody else has already done any heavy lifting and ugly work for you... I didn't immediately see a module that looked like a drop-in replacement, but I would be surprised if nothing exists yet. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Inhomogeneous data in a database
Alex: On Wed, Apr 15, 2015 at 11:27:41PM +0200, Alex Becker wrote: Hi! Hello, Thank you for your feedback. I also asked some colleagues about it. We agreed that the view approach would be the nice thing. However, I do not know if I did it the right way / if I understood t correctly. Given the table definition from my last mail, I added a view as follows: CREATE VIEW v_navigation AS SELECT content_types.content_type_id as target_type_id, fancy_scripts.script_id as target_id FROM content_types JOIN fancy_scripts ON content_types.ident = script UNION SELECT content_types.content_type_id as target_type_id, html_contents.content_id as target_id FROM content_types JOIN html_contents ON content_types.ident = page A table for the content types was also added. content_types is a set of: {1,page,2,script} I think that what Tommi had in mind was something more like this (excuse the T-SQL, but it's what I'm most familiar with): navigation 1 1 /\ 0|10|1 html_navigation script_navigation 1 1 | | 1 1 html_contents fancy_scripts CREATE TABLE navigation( navigation_id INT NOT NULL PRIMARY KEY, position INT NOT NULL UNIQUE, link_text VARCHAR(45) NOT NULL, ); CREATE TABLE html_navigation( navigation_id INT NOT NULL PRIMARY KEY CONSTRAINT FK_html_navigation_navigation FOREIGN KEY REFERENCES navigation(navigation_id), content_id INT NOT NULL CONSTRAINT FK_html_navigation_html_contents FOREIGN KEY REFERENCES html_contents(content_id) ); CREATE TABLE script_navigation( navigation_id INT NOT NULL PRIMARY KEY CONSTRAINT FK_html_navigation_navigation FOREIGN KEY REFERENCES navigation(navigation_id), script_id INT NOT NULL CONSTRAINT FK_html_navigation_fancy_scripts FOREIGN KEY REFERENCES fancy_scripts(script_id) ); CREATE TABLE target_type( target_type_id INT NOT NULL PRIMARY KEY, name VARCHAR(10) NOT NULL UNIQUE ); CREATE VIEW v_navigation AS SELECT a.navigation_id, a.position, a.link_text, /*or use foreign key if you wish*/ content AS target_type, c.content_id AS target_id FROM navigation a, target_type b INNER JOIN html_navigation c ON a.navigation_id = c.navigation_id WHERE b.name = 'content' UNION SELECT a.navigation_id, a.position, a.link_text, b.target_type_id AS target_type, c.script_id AS target_id FROM navigation a, target_type b INNER JOIN html_navigation c ON a.navigation_id = c.navigation_id WHERE b.name = 'script'; Untested. Meant purely to illustrate the idea. Let me know if you have any questions. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Inhomogeneous data in a database
On Thu, Apr 16, 2015 at 10:56:22AM -0400, Brandon McCaig wrote: /*or use foreign key if you wish*/ content AS target_type, Doh! That should have been the same as the second part of the union: b.target_type_id AS target_type, Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Need Advice on Perl learning
Manpreet Singh: On Sun, Apr 12, 2015 at 8:05 AM, Manpreet Singh manpreet@gmail.com wrote: Hello All, Hello, Any other skill building advice for a beginner related to Perl is welcome. You've come to the right place. A great way to learn is to join this mailing list and try to help out with questions yourself. You'll learn a lot about Perl by practicing it and this is a good place since people regularly post programs they're trying to solve. You can also get feedback from experienced Perl programmers on bad things and good things in your proposed solutions. For more advanced topics people have already proposed good directions to go. We rarely venture into the higher-level Perl here (not that it's unwelcome either), though there are many other Perl-based mailing lists to consider that may go into more advanced usage. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split regex
Danny Wong: Hello, On Thu, Mar 19, 2015 at 09:25:02PM +, Danny Wong (dannwong) wrote: Hi Perl GURU, I have a string like this: 'Baseline: (_bMgvUBQ_EeKsP6DECdq0Lg) 1 Initial Baseline Initial Baseline of Component NGP-Diagnostics Sivakumar Subas Oct 12, 2012 12:35:41 AM'; I’m trying to perform a split via \s (spaces) delimiter, but this give me a partial return of Initial Baseline” (returns Initial). Is there a regex where I can split using space as the delimiter, but get all the words inside a quote string? It seems that you have figured this out by yourself, but a few tips for future reference: You may wish to configure your mail user agent (client) to send plain text E-mails instead of HTML. I believe that many of us prefer it. You should also learn how to properly send plain text E-mails and generally proper netiquette for mailing list usage (it varies list to list, and I don't know that this is a hard rule here, but IMO it would help). On that same note, avoid using rich text styling to communicate ideas because not all readers will be able to see it. It appears that you tried to color parts of the text in red to emphasis them. I didn't see this from mutt where I normally read mailing list posts. I noticed it when I happened to open the same thread from the Gmail Web interface. Lastly, if you're having trouble with Perl code it is always a good idea to post the code you're struggling with. It's a little bit difficult to understand what you were having trouble with, but if you had posted code then there would have been several anxious persons able to spot the problem and rushing to be the first to reply. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: catching utf8 errors
On Sat, Mar 14, 2015 at 03:20:06AM -0700, Charles DeRykus wrote: open my $fh, ':encoding ) or die ... { open( local *STDERR,'',\my $err); my $string = $fh; if ($err =~ /does not map to Unicode/) { # take action. } } Here is an alternative approach.. use strict; use warnings; use Encode qw/decode encode/; my $file_name = './somefile'; sub create_file { open my $fh, '', $file_name or die open: $!; print $fh ABC\n, chr(0x90), \nDEF\n; close $fh or warn close: $!; } sub read_or_die { my ($fh) = @_; my $bytes = $fh; # Emphasis on the Encode::FB_CROAK. my $string = decode('UTF-8', $bytes, Encode::FB_CROAK()); return $string; } sub write_or_die { my ($fh, $string) = @_; # Emphasis on the Encode::FB_CROAK. my $bytes = encode('UTF-8', $string, Encode::FB_CROAK()); print $fh $bytes; } create_file(); print STDERR Created file. Attempting to open it...\n; open my $fh, '', $file_name or die open: $!; print STDERR Attempting to read it...\n; eval { while(my $line = read_or_die($fh)) { write_or_die(\*STDOUT, $line); } }; if ($@) { print CAUGHT ERROR: $@; } close $fh or warn close: $!; __END__ Output: Created file. Attempting to open it... Attempting to read it... ABC CAUGHT ERROR: utf8 \x90 does not map to Unicode at /home/bambams/perl5/perlbrew/perls/stable/lib/5.18.0/x86_64-linux/Encode.pm line 176, $fh line 2. It is a bit more painful having to call decode and encode ourselves, but I couldn't figure out any way to control the CHECK parameter used by PerlIO. The weird thing is that the documentation appears to assert that PerlIO is supposed to be using Encode::FB_CROAK() (1) too, but alas I guess that isn't the case if it doesn't croak().. That or the PerlIO layers are catching and handling the error differently... Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Use vs Require with version number
On Wed, Mar 04, 2015 at 06:26:41PM -0800, SSC_perl wrote: So there's only really a difference for loading modules, not for setting the minimum version number? There could be a difference if code with side effects is done first. By being done at compile-time, Uri means that use is effectively requiring the module with a BEGIN block. That means that it will execute before any other code that isn't in a BEGIN block. That could matter in rare, silly cases. In most cases, it wouldn't really matter (usually we require modules and assert versions at the beginning of a program or module before anything else is actually done). A silly, contrived program might be: use strict; use warnings; use My::Stuff; my $stuff = My::Stuff-new(); $stuff-do_consequences(); require v5.018; $stuff-more_consequences(); __END__ If your entire program requires 5.018 to work properly, but perhaps My::Stuff::do_consequences will do something *bad* if run under a previous version of perl then this program would be *dangerous*. The require() is going to happen at run-time *after* the do_consequences method is called. If I had used `use' instead then it would have happened at compile-time, within a BEGIN block, and do_consequences never would have been called. Here is that program again using `use' for the version assertion: use strict; use warnings; use My::Stuff; my $stuff = My::Stuff-new(); $stuff-do_consequences(); use v5.018; ### *** $stuff-more_consequences(); __END__ This is sort of like: BEGIN { require strict; } BEGIN { require warnings; } BEGIN { require My::Stuff; My::Stuff-import(); } my $stuff = My::Stuff-new(); $stuff-do_consequences(); BEGIN { require v5.018; } $stuff-more_consequences(); __END__ BEGIN blocks are always executed before surrounding code so you can think of this program as: require strict; require warnings; require My::Stuff; My::Stuff-import(); require v5.018; ### *** my $stuff = My::Stuff-new(); $stuff-do_consequences(); $stuff-more_consequences(); __END__ This is an over-simplification. Hell, I probably don't fully understand the mechanism either, but the idea is that the BEGIN stuff happens first. Even though within my code I had the do_consequences method call before the v5.018 assertion the actual result is that the Perl version is checked first, and if dissatisfied the program will die() right then and there before any consequences can be carried out. I think that generally you should be using `use' unless you have a specific need to use require directly. `use' will call require() under the surface when needed so to you it's basically the same, but it has added benefits that make sense generally. If you want the action to happen at run-time then require() may be more appropriate, but those cases should be rare. Similarly, require is sort of like a wrapper over `do FILENAME' so there may also be cases where you'd want to use `do' instead, but most of the time use is more appropriate, and less often require is more appropriate. For the detailed explanation take the time to read through the following: perldoc perlmod (search for BEGIN) perldoc -f use perldoc -f require perldoc -f do Then use `use' until you know you need something else. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: CONFIG_FILE
Nicolas: On Fri, Feb 20, 2015 at 10:29 AM, nicolas nicolas...@mail.com wrote: package vowCons2; use strict; use lib::avt; my $self = {}; sub new { my $this = shift; my ($WORD) = @_; my $voyConsDef = readConfFile($CONFIG_FILE/vowConsDef); $self-{'V'} = $$voyConsDef{'V'}; $self-{'C'} = $$voyConsDef{'C'}; I change my $voyConsDef = readConfFile($CONFIG_FILE/vowConsDef); to : my $voyConsDef = readConfFile($CONFIG_FILE/vowConsDef); The double quotes are necessary there because you're interpolating the values of that variable into the string. Either that or you could explicitly concatenate them like so: my $voyConsDef = $CONFIG_FILE . /vowConsDef; The original form is probably preferred most of the time. I get this message Global symbol $CONFIG_FILE requires explicit package name at vowCons2.pm line 18. Compilation failed in require at sufpref.pm line 10. BEGIN failed--compilation aborted at sufpref.pm line 10. Compilation failed in require at ./base.pl line 32. BEGIN failed--compilation aborted at ./base.pl line 32. Should I create CONFIG_FILE directory ? If I recall correctly you said that you are not an experienced programmer, and I got the impression that this code was not written by you. If you are the author, or maintainer, then by all means follow the others' advice to try to fix it. If you didn't write it, and don't understand it, and just want it to work then I would try to ask the author/programmer to look at it. This looks like a clear programming error so there's little a user can do to make the code work. We don't even know it will work, or how it's supposed to work. Perhaps the $CONFIG_FILE variable was supposed to be exported from lib::avt? We don't know. Declaring it explicitly in vowCons2.pm file might be the very wrong thing to do... Let us know your role in this and that will help us to give you better advice... If there's a programmer that you work with that is responsible for this then bring him coffee or beer and he'll gladly fix you up. Or worst case, if the guy responsible is long gone perhaps you could attach a zip file or tarball of all of the code that you have and a kind, bored soul can attempt to make sense of it... Or somebody might offer their services in private for a fair rate if needed (I'm not sure how much of that is tolerated on the list). Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: can locate pm
Nicolas: On Wed, Feb 18, 2015 at 6:14 PM, nicolas nicolas...@mail.com wrote: Hello, Hello, I used to work with a perl script that contains (use vt.pm). (vt.pm are code that complement the script) I remember that I have to copy the vt.pm file in perl somewhere. Since I am not very familar with programming my question is where to excaclly copy this *.pm so I can use my script again. Thanks Can't locate lib/vt.pm in @INC (you may need to install the lib::vt module) (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at sequence.pm line 8. Thanks for you help It sounds like your program is looking for lib::vt. That means the file should have a relative path of lib/vt.pm, and it should be located in one of the paths in @INC as listed in the error output. E.g., /usr/local/lib/site_perl/lib/vt.pm. You should be able to pick any of those base paths to put the relative path of lib/vt.pm into. There may be better places to put it, but if you just want it to work then that should suffice. Alternatively, you could look into lib and local::lib to use it to customize the @INC array at run-time to specify a project-specific path instead. See also `perldoc perlvar' for @INC, `perldoc lib' for manipulating @INC at run-time, and search cpan.org or metacpan.org for local::lib which is a similar idea. Or just copy the file to one of the paths already listed and get on with your day. :) Hope that helps. Let us know if it works. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: simple module install via CPAN not working.
Gary; On Mon, Feb 16, 2015 at 8:45 AM, Gary Stainburn gary.stainb...@ringways.co.uk wrote: I'm still looking to get WWW::Mechanize::Firefox installed. It looks like I've got a more pressing issue. Even simple module installs aren't working properly. I'm working through the dependancy list and looking at Test::Tester and Test::NoWarnings. Installing Test::Tester worked fine, but when I try to install Test::NoWarning it fails saying that Test::Tester is missing: cpan[2] install Test::NoWarnings *snip* The cpan(1) client is rather low-level. It's a real pain to use. I highly recommend you use a more advanced tool that requires a lot less configuration and user interaction. Personally I use cpanm(1) (App::cpanminus). It handles fetching, building, and testing all dependencies without any prompts and hides all of the ugly output from you by default. If the build fails, you will still need to sort through the build output, which will be logged to a file, but if everything works fine then it saves you a lot of trouble. If you're using the system perl then you can probably install cpanm using your distribution package software. I use perlbrew to install both up-to-date perl and cpanm. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: question of what can respond to die [...]
Harry: On Tue, Jan 27, 2015 at 9:37 AM, Harry Putnam rea...@newsguy.com wrote: In the following code I hoped to use `use Cwd abs_path;' to not only produce the absolute path for a named directory but to die if it could not... thereby getting around having to test the named dir with -d ... to make sure it was really a directory in the fs. It does not have that effect when a bogus directory is passed in, and, in fact, goes on to the chdir to find out its bogus. Why is that? --- 8 snip -- 8 snip -- 8snip --- #!/usr/local/src/test/bin/perl use strict; use warnings; use Cwd 'abs_path'; my $tdir = shift; $tdir = abs_path($tdir) or die Can't find $tdir: $!; chdir $tdir or die Can't chdir $tdir: $!; print Hello from $tdir\n; It would not be very pleasant if most things die() on error because errors are normal and usually we don't want robust programs to crash when something innocent fails, nor do we want to have to wrap every error that we can recover from with eval blocks (i.e., similar to try...catch in other languages) to handle errors. In general the documentation for a function or module should be consulted to learn of how it handles errors. Most often die() implies that something in the program state is invalid and the program cannot continue. This typically implies an impossible or exceptional situation. When the simple fact that an operation failed is sufficient it is often good enough to return an error code (e.g., undef or an empty list, or perhaps a two-item list with an error as the second item) and let the caller decide if this is a serious error or not. Often the $! variable is set to an appropriate error code that is more descriptive of the error. That works well for many errors. Often die() is called directly by you. When die() is called (and its string doesn't end in a newline) it reports the line where it was called as the source of the error. There is a module, Carp, that is often used to die() from within modules by pointing at your invocation of the module (e.g., where you called the module) as the source of the error instead of the module itself being wrong. Modules will generally tell you if they die() or use Carp, and often that behavior can be toggled on or off with flags. There is also an 'autodie' pragma that allows many system functions to die() automatically. chdir() is supported by it if you're interested in that. I do not believe that the intent of abs_path() is to tell you whether that path exists. It is merely there to resolve a relative path into an absolute path. You might do so in order to create a new file with that path, and then tell the user what that full path is so there is no ambiguity. If it were to die() when that path didn't exist it would be impossible to resolve non-existent paths, which would limit the function's usefulness. I hope that helps. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Looping regexes against a list
On Tue, Jan 20, 2015 at 10:21 AM, Brandon McCaig bamcc...@gmail.com wrote: perldoc -f qr// I was sure that worked in my up-to-date perlbrew environments, but it isn't working in Cygwin running perl 5.14.2 so in the event that it doesn't work for you look at `perldoc -f qr' and `perldoc perlop' (then search for qr/STRING or something to that effect). Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Looping regexes against a list
On Tue, Jan 20, 2015 at 9:51 AM, Andrew Solomon and...@geekuni.com wrote: Aside from this lengthy rant^H^H^H^H discussion:) about where you put your regex, have you made any progress on the performance problem you put forward at the outset? I'm not quite sure that I understand what the OP is doing still, but a relatively simple thing to do to improve performance if he's not doing it yet might be to compile the regular expressions ahead of time (assuming they are loaded from text streams into strings and reused over and over again): perldoc -f qr//. If the hash is currently storing them as strings they will probably end up getting compiled over and over again, whereas if they are compiled beforehand that should speed things up a little bit. It might not be enough to improve performance completely, but it's certainly easy enough to do to be worth trying before approaching more in-depth refactoring. Beyond that, I'd look into alternative ways to structure the data for more efficient processing. Look at the look ups that need to be done and try to think of efficient ways to structure the data so they aren't needed. Alternatively, the OP could post a more complete program that we can play with... I wonder about the no warnings 'uninitialized' too. Why is that needed? I wonder if checking that things are defined first would be more efficient than attempting an operation on undef and maybe going through overhead to trigger and silence warnings about it or what not. I could see it being negligible, but I could see the opposite being true as well. You could probably even exclude those elements when loading the hashes so you never need to skip them later on. Just another simple thing to consider. Other than the regular expression's being text I think the only other solution is restructuring the program... You can look into using Benchmark.pm to compare the performance of different approaches. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: should this old forumulation still work?
Andy: On Tue, Jan 20, 2015 at 05:56:26PM -0600, Andy Bach wrote: my $contents = do { local $/; map { chomp } HANDLE }; sorry, untested. chomp() returns a number representing the number of removed characters unfortunately. Your block would need to return $_ separately. (I find this rather annoying, but surely a CPAN module has fixed this with an altenative that returns the string...) my $contents = do { local $/; map { chomp;$_ } $fh }; * * * Uri Guttman, the author of File::Slurp, insists that it performs much better than the standard Perl solution of setting $/ to undef. I don't have benchmarks to prove it, but I'm willing to trust his expertise on this. I imagine if you inspect the code or distribution you'll be able to make an educated decision for yourself. I've never cared to. At the very least, I'm sure it doesn't hurt, and it allows you to replace a rather cryptic syntax (albeit, one that any Perl programmer should understand) with something a bit more readable. Consider using File::Slurp despite being able to code it up yourself in pure Perl. If you're OK with getting the results as a LIST then File::Slurp even supports a chomp option. Otherwise, I suppose there's always s///, though I can't specdulate on the performance of such an operation. For a file like .bashrc I'm sure any of these differences are negligible. I might prefer File::Slurp with the chomp option and joining the lines afterward though. I suppose there's always Benchmark.pm to give you a hint about what works better if it matters. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Looping regexes against a list
Mike: On Mon, Jan 19, 2015 at 01:25:56PM +, Mike Martin wrote: Hi Hello, I am looking for the most performant way to achieve this I have a big list of text (47+ lines in a hash) I then run a hash ref consisting of replacement text - pattern to search - optional 3rd param for grouping matches So I loop through the text and then loop the regex hash against each record This works but takes about 20 minutes Any suggestions It might help to post some actual code. I wonder how those 500k lines are getting loaded into the first hash in the first place, what keys are being used, and then how the second hash is related to it. This sounds a bit like an XY problem[1], but you'll likely want to solve the problem with a more ideal algorithm and that might require a more ideal data structure. Not that I'm any authority on this problem. :\ [1] http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Parsing multi column files
Mike: On Sun, Jan 18, 2015 at 07:00:05PM -0500, Mike wrote: So I've got a text file in a multi column format (three columns), each column is separated by a single space. Here is a snippet for reference: artless base-court apple-john bawdy bat-fowling baggage beslubbering beef-witted barnacle I want to be able to randomly select a word from the first column, then from the second column, then from the third column, and concatenate them to together into a single string. Here's the code I currently have, but I'm stuck: First a little code review. Be sure to always use strict and warnings to help identify bugs. Add this to the top of your programs. use strict; use warnings; Perhaps you're already using it. At the very least, your snippet is strict and warnings approved. :) That said, it is best to include a full example of what you'e trying, including pragmas. It saves us the trouble of emphasizing best practices that you're already following and omitting them might also alter the behavior of code and confuse us and you. So hopefully you're already using strict and warnings, but next time include them in your post too! :) sub gen_ins { open(FH, '', 'insults2.txt') or die [-] ERROR: Can't find insult list.; It is better to use lexical file handles. open(my $fh, '', 'insults2.txt') or die [-] ERROR: Can't find insult list.; Good job using 3-arg open and handling failure. Often you should include the output of $! in your error message because it will contain information about what exactly went wrong. For example, perhaps insults2.txt exists, but the user doesn't have the necessary permissions to read it. Take that into account also when you're writing your error messages to avoid confusing the user (which may be yourself). open(my $fh, '', 'insults2.txt') or die [-] ERROR opening insults list: $!; my @cols = split ( , FH); print $cols[0]; close FH; } gen_ins(); When currently run, gen_ins() will print out the first word of the given column: $cols[0], $cols[1], or $cols[2]. How can I access the rest of the words below the first? Others have demonstrated good solutions to this problem already. Here's an alternative take emphasizing modularity and reuse. use strict; use warnings; use Readonly; exit(main(@ARGV)); sub main { Readonly::Scalar my $Filename = 'insults2.txt'; my ($max) = (@_, 1); my $insults = load_insults($Filename); for (1..$max) { my @chain = random_insult_chain($insults); local $, = ' '; print @chain, \n; } return 0; } sub get_random_index($) { my ($length) = @_; # Note: rand() is not meant to be particular good at picking # randomly. Look to CPAN for alternative options if you need # something *good*. return int rand $length; } sub load_insults { my ($filename) = @_; open my $fh, '', $filename or die open $filename: $!; my @insults; # Load the insults into an array of arrays. while ($fh) { my ($first, $second, $third) = split; push @{$insults[0]}, $first; push @{$insults[1]}, $second; push @{$insults[2]}, $third; } close $fh or warn close $filename: $!; return \@insults; } sub random_insult_chain { my ($insults) = @_; my @words = map random_insult_word($_), @$insults; return @words; } sub random_insult_word { my ($insult_column) = @_; my $i = get_random_index @$insult_column; my $word = $insult_column-[$i]; return $word; } __END__ Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Parsing multi column files
On Sun, Jan 18, 2015 at 07:24:21PM -0500, Shawn H Corey wrote: You would need an array for each column: my $MAX = 10; my @first = (); my @second = (); my @third = (); sub get_columns { my $file = shift @_; open my $fh, '', $file or die could not open $file: $!\n; while( $fh ){ my @items = split; push @first, $items[0]; push @second, $items[0]; push @third, $items[0]; Minor typo there. I believe that Shawn meant: push @first, $items[0]; push @second, $items[1]; push @third, $items[2]; Alternatively, eliminate the @items array entirely and assign to a list of 3 lexical variables. For example: my ($first, $second, $third) = split; push @first, $first; push @second, $second; push @third, $third; } close $fh or die could not close $file: $!\n; } get_columns( 'insults2.txt' ); for ( 1 .. $MAX ){ my @line = ( @first[rand(@first)], @second[rand(@second)], @third[rand(@third)], ); print @line\n; } Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Parsing multi column files
On Sun, Jan 18, 2015 at 07:24:21PM -0500, Shawn H Corey wrote: my @line = ( @first[rand(@first)], @second[rand(@second)], @third[rand(@third)], ); Sorry to beat on you, Shawn, but I missed this the first time around. Those should probably be: my @line = ( $first[rand @first)], $second[rand @second)], $third[rand @third)]] ); The use of the @ sigil for dereferencing an array is meant to grab a slice of the array, not a single element. The warnings pragma will warn you about this: perl -Mwarnings -e 'my @x = (1..10); print @x[4], \n;' said: Scalar value @x[4] better written as $x[4] at -e line 1. 5 And with that, it should be emphasized to always use the strict and warnings pragmas. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: How does opendir/readdir process files
On Thu, Jan 15, 2015 at 05:27:03PM -0500, Harry Putnam wrote: Now I really have to ask what that even means. When Brandan said that to me... other than the RTFM it sailed right over my head... so what does tl;dr mean? http://www.urbandictionary.com/define.php?term=tl%3Bdr Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: How does opendir/readdir process files
Charles: On Fri, Jan 09, 2015 at 11:02:28AM -0800, Charles DeRykus wrote: Definitely needs a 'use 5.014' if you want to dabble. You appear to be correct. Hmmm, I didn't figure that from the documentation. Either I read it wrong or it's not documented well. Apparently undocumented that you don't need a reference either since while(($key,$value) = each @array works as well as 'each \@array'. I think the normal and original behavior is no reference. I think they added the reference in 5.14 too. Perhaps the documentation just fails to mention that support for arrays was added in 5.14 along with references? Hopefully I got that right this time. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: How does opendir/readdir process files
as a block which against aliases $_ to the elements of the list. Again we have list context and it also takes the filtered list that grep returned. While grep is used to filter the list, map is used to transform it. Another way to say it would be: sub my_filter { my $is_dot_file = $_ =~ /^\./; my $is_reg_file = -f $dir/$_; return ! $is_dot_file $is_reg_file; } sub my_mapper { my @stats = stat($dir/$_); my $mtime = $stats[9]; my $tuple = [$_, $mtime]; return $tuple; } my @files = readdir($dh); my @reg_files = grep \my_filter, @files; map(\my_mapper, @reg_files); The code block passed to map is returning a tuple (a pair), represented with an anonymous array, containing the name of the file in its first element and its last modified timestamp (mtime) in the its second element. A reference to the array representing the tuple for each regular file is returned for each element in the list. A list of these tuples or array references is what map returns. sort { $a cmp $b } Next up we have sort(). Just like grep and map it is being called without parenthesis, is accepting code as a block, and is accepting the list of previously filtered and mapped files to work on. Sort uses the code block to compare two elements within the list at a time. It does this to determine the order in which they should be sorted. The elements are aliased to $a and $b within the block (so you should never use those names in your own code). If the block returns a negative number then it indicates that $a comes before $b. If the block returns a positive number then it indicates that $a comes after $b (in other words, $b comes before $a). If it returns zero then it indicates that $a and $b are considered equal for the purposes of the sort. Two core operators that do this are = and cmp. = works with numbers and cmp works with strings. So this sort is sorting stringwise in ascending order (strings that are considered less come before strings that are considered more). A stringwise sort is typically based on a position comparison between the character ordinals in the string. That leaves us with this: sub my_filter { my $is_dot_file = $_ =~ /^\./; my $is_reg_file = -f $dir/$_; return ! $is_dot_file $is_reg_file; } sub my_mapper { my @stats = stat($dir/$_); my $mtime = $stats[9]; my $tuple = [$_, $mtime]; return $tuple; } sub compare_files { return $a = $b; } my @files = readdir($dh); my @reg_files = grep \my_filter, @files; my @file_tuples = map(\my_mapper, @reg_files); sort \compare_files, @file_tuples; That won't quite work as we intended because we'd be comparing the array references of our tuples. Dermot probably meant to compare the mtime within the tuples. We can fix that easily enough: sub compare_mtimes { my $lhs = $a-[1]; my $rhs = $b-[1]; return $lhs = $rhs; } sort \compare_mtimes, @file_tuples; Finally we have: my @files = map { $_-[0] } Here we have another invocation of map() which again is getting the results from the previous function call chain of grep, map, and sort. We've already gone over what map does so we should be able to figure out what this is doing already. Taking our sorted list of regular file tuples and mapping them back to a list of file names. sub my_filter { my $is_dot_file = $_ =~ /^\./; my $is_reg_file = -f $dir/$_; return ! $is_dot_file $is_reg_file; } sub my_mapper { my @stats = stat($dir/$_); my $mtime = $stats[9]; my $tuple = [$_, $mtime]; return $tuple; } sub compare_mtimes { my $lhs = $a-[1]; my $rhs = $b-[1]; return $lhs = $rhs; } sub extract_filename { my $filename = $_-[0]; return $filename; } my @files = readdir($dh); my @reg_files = grep \my_filter, @files; my @file_tuples = map(\my_mapper, @reg_files); my @sorted_tuples = sort \compare_mtimes, @file_tuples; my @sorted_files = map \extract_filenames, @sorted_tuples; And there you have it. A very verbose way to write this: my @files = map $_-[0], sort { $a-[1] = $b-[1] } map [$_, (stat($dir/$_))[9]], grep !/^\./ -f $dir/$_, readdir($dh); Or put another way, this is a very terse way to write that. I hope this wall of text helps. For the most part, you just need to spend some quality time with The Revelant Manuals (TFM). Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: How does opendir/readdir process files
On Tue, Jan 13, 2015 at 10:40:46PM -0500, Brandon McCaig wrote: to spend some quality time with The Revelant Manuals (TFM). I cite this as proof that I am TFT (too fucking tired). I'm not going to confess how proud I was of this word play before I realized that it was completely nonsensical. -to spend some quality time with The Revelant Manuals (TFM). +to spend some quality time with The Functional Manuals (TFM). Sort of kind of salvaged the situation, but maybe not. I should go sleep. Sorry. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: How does opendir/readdir process files
Charles: On Fri, Jan 9, 2015 at 12:46 PM, Charles DeRykus dery...@gmail.com wrote: On Fri, Jan 9, 2015 at 3:39 AM, Dermot paik...@gmail.com wrote: I think John has answered your immediate question. ... for (0..$#files) { print $_) $files[$_]\n; } Alternatively (at least since 5.14) : say $k) $v while ($k,$v) = each @files; perldoc -f each said: *snip* When called in list context, returns a 2-element list consisting of the key and value for the next element of a hash, or the index and value for the next element of an array, so that you can iterate over it. *snip* Starting with Perl 5.14, each can take a scalar EXPR, which must hold reference to an unblessed hash or array. The argument will be dereferenced automatically. This aspect of each is considered highly experimental. The exact behaviour may change in a future version of Perl. while (($key,$value) = each $hashref) { ... } See also keys, values, and sort. Your usage should not depend on 5.14. Starting with 5.14, the EXPR passed to each() can be a reference to an array or hash instead of the data structure themselves, which will be automatically dereferenced. I believe that is the only bit of the functionality that is dependent on Perl 5.14 (and as of 5.14, the automatic dereference was considered experimental; my Cygwin environment is still back on 5.14..). Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Get return value when running cmd like this
Harry: On Mon, Jan 05, 2015 at 06:20:06PM -0500, Harry Putnam wrote: When running shell commands from perl script with an open() instead of system() or exec how is the return value snagged. I guess, if it doesn't die, it worked but how to snag that information and at what point? --- 8 snip -- 8 snip -- 8snip --- #!/usr/local/bin/perl use strict; use warnings; my $log = '/home/harry/some.log'; my $cmd = 'rsnapshot -c rsnap_Home.conf hourly'; open my $fh, '', $log or die Can't open $log: $!; open my $ch, '-|', $cmd or die Can't run $cmd: $!; while ($ch) { print; print $fh; } close $fh; close $ch; __END__ You can check the exit status of a pipe after closing it. You should also be checking the return value of the close because it might indicate a problem closing the pipe which might affect the exit status, for example. perldoc -f close If the filehandle came from a piped open, close returns false if one of the other syscalls involved fails or if its program exits with non-zero status. If the only problem was that the program exited non-zero, $! will be set to 0. Closing a pipe also waits for the process executing on the pipe to exit--in case you wish to look at the output of the pipe afterwards--and implicitly puts the exit status value of that command into $? and ${^CHILD_ERROR_NATIVE}. So for example... unless (close $pipe) { my ($errno, $child_error) = ($!, $?); if ($errno == 0) { # Closing the pipe succeeded, but the exit status was # non-zero. my $signal = $child_error 127; my $status = $child_error 8; handle_pipe_exit($signal, $status); } else { # One or more system calls failed when attempting to # close the pipe. $! should be set appropriately. I don't # know if you can rely on the process having exited/died # or on the exit status being set. handle_pipe_exit_error($errno); } }; Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: say,
On Mon, Jan 05, 2015 at 02:25:15PM +0100, Hans Ginzel wrote: Hello! Hello, Is correct that say() uses n? Why it does not use $\ (http://perldoc.perl.org/perlvar.htm#$OUTPUT_RECORD_SEPARATOR)? See http://perldoc.perl.org/functions/say.html. Consider Perl6::Say - http://rpm.pbone.net/index.php3/stat/45/idpl/1146707/numer/3/nazwa/Perl6::Say#lbAE. What about say() on Mac or Windows? According to perldoc -f say: Just like print, but implicitly appends a newline. say LIST is simply an abbreviation for { local $\ = \n; print LIST }. print() already uses $\ so if that's all say() did it would have no effect. Instead, say() temporarily sets $\ to \n before printing so that the record ends with a newline. If you want to use $\ instead of \n then you can just use print normally. use strict; use warnings; $\ = ---; print for qw/foo bar baz/; __END__ Output: foo---bar---baz--- Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: say,
Andy: On Mon, Jan 05, 2015 at 01:22:34PM -0600, Andy Bach wrote: And Perl should be noting which OS you're on and 'tranlating' \n into the appropriate sequence. Though I get the same output for: $ perl -e '{ local $\ = \n; print hi }' | perl -ne print( ord($_), qq{ }) foreach split(//) 104 105 10 on, Mac, windows and Linux. I think in most programming languages the \n escape sequence typically becomes a literal LF character in memory. It is only when you write to a text file stream that it gets translated (if then). Obviously for UNIXes (e.g., GNU+Linux, BSDs, MAC OS X) there is no translation since the newline character in UNIX is LF. In Windows, it would turn LF into CRLF on the way out, but only for text streams. If you write with binmode set then it should be literal again. You can still end up with CRLF in memory in Windows if you read from a binary stream, which I think includes non-native encoded streams (e.g., UTF-8 via IO layer). I think, but I'm not sure, that a plain text stream in Windows would convert CRLF to LF when reading too, but I don't have a Windows machine to test on right now (that feels good). If so that would explain why you get the same output on each platform. In Windows the first perl writes h i CR LF to the pipe and the second perl reads it back into memory as h i LF. I'm not even sure if that's something that perl does or something that the C runtime or OS is doing for us. In Windows you should be able to experiment with tee(1) in the middle and/or binmode on either or both ends of the pipe anyway. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: IO::Socket
Mike: On Mon, Dec 29, 2014 at 10:23:00AM -0600, Mike wrote: Can anyone point me in the direction of more thorough documentation for the IO::Socket module? Seems like the documentation on perl.org is pretty limited. At least, it doesn't feel very thorough to me. http://perldoc.perl.org/IO/Socket.html For example, what exactly does timeout affect? This documentation also doesn't explain what values the methods accept or how to use them. I believe that IO::Socket is intended to be a Perl OO wrapper around the raw UNIX socket concept (and WinSock's concept of an Internet socket copied from Berkeley's implementation) and its raw Perl wrapper (e.g., Socket.pm). I guess the documentation relies on knowledge of this underlying API and/or reference to its documentation. There appears to be a send timeout and a receive timeout option for UNIX sockets, though the documentation says that not all implementations support it. Since Perl's OO wrapper only seems to have one I can only assume that the timeout applies to both read and write operations on the socket. At least shy of accessing the source code to see for myself. ...read read read... It appears that IO::Socket's timeout is unrelated to the afforementioned send and receive timeout options (though those could potentially still be accessed using the raw /(get|set)sockopt/ functions).. The IO::Socket source appears to construct an IO::Select object with the timeout value and then waits until you can either read or write, depending on what's applicable, or the timeout is elapsed. This appeared to only affect connect() and accept() methods. Though the documentation cited this by saying that it's horribly inefficient to do this since the IO::Select object is constructed just for this one purpose and that using select() or non-blocking IO is preferred. Truly understanding the implenetation details may require dipping into IO::Select's source too and following it all back to C. I can't be bothered. I think that to understand what's going on here you need experience programming raw sockets in C. While raw socket access is a useful skill (mostly for a C programmer), I think that older and wiser Perl hackers would encourage the use of higher-level, event-based wrappers for networking in Perl to save yourself the trouble of reinventing the square wheel. YMMV. Raw socket access or experience shouldn't really be necessary if using one of the higher-level wrappers so you can spare yourself the nightmares if all you need is networking. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Script for auto conf
On Tue, Dec 30, 2014 at 12:47:13AM -0300, Alan Glait wrote: Hi ! Hello, I have the Idea to make a perl script (better than in bash) to make some configuration on linux. I think to have some files like param.txt with some lines like: param_one = ZZZ XX VV param_two = Z param_three = X so need to check if param_one is in some_file.conf and if it is there test the value .. if it is not .. add to some_file.conf ... I think it should be easy ... right ?? There are many ways to accomplish this, and Perl is known for its TIMTOWTDI (There is more than one way to do it). We could propose solutions for you, but it isn't our job to write your code for you for free. If you have tried to come up with a solution to this yourself and are having trouble getting it to work then post the code that you've tried and we can help you to get it working. Perl is a perfectly valid choice for this project. It supports many good existing technologies that can accomplish this well. Only you know what it is that you're trying to do so go ahead and try to do it. You will learn best by trying and you'll get a lot more help from us by showing us something. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Anonymous functions and safe compartments
David: On Sun, Dec 28, 2014 at 07:35:14PM +0100, David Emanuel da Costa Santiago wrote: Hello! Hello, How do i run and pass arguments to an anonymous subroutine that i shared on a safe compartment? My goal is to have a function that perform dangerous operations, that must be available to untrusted code, however this function needs some parameters that are only available when the code is executing, and the untrusted code doesn't know them (hence the anonymous subroutine). Example (35 lines): ### Example.pl file use strict; use warnings; use Safe; use 5.020; my $UNTRUSTED_SCRIPT=untrusted_script.pl; sub run_me{ my $sandbox = Safe-new(Sandbox); $sandbox-permit(qw(:base_core :base_loop :base_mem :base_io :load :base_orig)); $sandbox-deny(qw(die exit)); my $operation = Dangerous; my $anon_func = sub{ my $param = shift; print $operation $param operation!\n; }; $sandbox-share($anon_func); $sandbox-rdo($UNTRUSTED_SCRIPT); $sandbox-reval('func1()'); } run_me(); ### untrusted_script.pl use 5.020; use warnings; use utf8; sub func1{ say About to run shared function!; $anon_func-(func1); } ## The output of the Example.pl script is: Error while executing the sandbox: Global symbol $anon_func requires explicit package name at untrusted_script.pl line 5. I was expecting of the output of the Example.pl script to be Dangerous func1 operation!. What am i doing wrong? I am not familiar with Safe.pm so I don't know anything about that, but $anon_func is a lexically scoped variable in Example.pl. That variable is not available within the untrusted_script.pl file. I also don't see a subroutine or anything at all named func1 defined so I can't imagine how $sandbox-reval('func1()') is supposed to work. I think that the first part that you're having trouble with is sharing data (or code) with the compartment. I think that if you want for $anon_func to be available to the Safe compartment, and therefore the untrusted_script.pl when executed with the Safe compartment, you need to pass the name of the anonymous function variable instead of the variable itself. $sandbox-share('$anon_func'); It also appears that you cannot share lexical variables with these compartments (see perldoc Safe) so you need to make $anon_func a package variable (or copy it to one). A named function would work too. So either: our $anon_func = sub { ... }; ... $sandbox-share('$anon_func'); Or else: sub shared_func = sub { ... }; ... $sandbox-share('shared_func'); A bareword passed to Safe::share is apparently assumed to be a named function too. You can also use Safe::share_from to share a subroutine from a different package than the caller. I worked out a little contrived example to figure this out myself: ./program: #!/usr/bin/env perl use strict; use warnings; use Safe; my $sandbox = Safe-new(); $sandbox-permit(qw/print/); # Shared variables cannot be lexicals! our $get_id = do { my $id = 0; sub { my ($base) = @_; my $result = ${base}${id}; ++$id; return $result; }; }; $sandbox-share('$get_id'); $sandbox-rdo('unsafe') or die $!; __END__ ./unsafe: my $id = $get_id-(foo_); print Unsafe script got id $id!\n; __END__ Output should be: $ ./program Unsafe script got id foo_0! Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Anonymous functions and safe compartments
A few corrections on my part... On Sun, Dec 28, 2014 at 04:35:49PM -0500, Brandon McCaig wrote: sub func1{ say About to run shared function!; $anon_func-(func1); } Oh, duh, there is func1 right there. :\ OK, that part makes sense. :) Or else: sub shared_func = sub { ... }; That obviously should have been: sub shared_func { ... } I should have had coffee (or beer) before posting. It looks like David figured it out anyway. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: parsing string output that isn't CSV, but similar; what module would parse these kind of key-value pairs?
On Tue, Dec 16, 2014 at 4:29 PM, Kenneth Wolcott kennethwolc...@gmail.com wrote: Well, I had to look at http://perldoc.perl.org/perlrequick.html#Extracting-matches to find out how much I had forgotten about extracting matches from a regex :-) This might be inefficient, but it seems to work: $shared_folder =~ m|\'([-A-Za-z/_]+)\'.+'([-A-Za-z/_]+)\'|; print $2 . \n; Would be good to come up with something better, hopefully... In general, parsing with regex is difficult. It depends on how complex the data is. If no single-quotes can appear inside of those values then it should suffice to match on single quotes globally. Note that the m// operator returns a list of matches in list context. Instead of doing what you did above, you can try something like this: my @shared_folders = $example_string =~ /'([^']*)'/g; (Untested) See documentation on m// and /g. If you want something to actually do parsing I have heard that Text::Balanced https://metacpan.org/search?q=Text::Balanced is a good direction to go, but I can't say for sure whether it will solve this problem more precisely. Hope that helps. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Can't find EOCD signature Archive::Zip
Mike: On Thu, Dec 4, 2014 at 7:15 AM, Mike Flannigan mikef...@att.net wrote: This is a very old problem that apparently still persists. Some or all gzip files created on Unix can't be unzipped by Archive::Zip (1.38). There are tons of old posts from 2005 that mention this problem, but apparently it is not an easy fix. Here is one link: http://www.perlmonks.org/?node_id=445999 The gzip files open fine on my win7 machine when I double click them (probably uses Winzip), but Archive::Zip cannot open them. Does this happen for all people on all OS, or does it work for some people and some situations? I think that you are just confused about gzip vs zip. These are not the same format at all, and are not compatible. See http://wikipedia.org/wiki/Gzip and http://en.wikipedia.org/wiki/Zip_(file_format). I think that they use the same compression method internally, but the structure of the files is different. WinZip is just capable of opening different types of archive files (it isn't limited to zip files). BTW, 7-Zip is a free, libre, open source alternative for Windows that can also open up most kinds of archive (I believe it is also available for *nixes, but there are other free software tools on those platforms already). I am unfamiliar with Archive::Zip, but I'm guessing that it is designed to only work with .zip files. In order to open a gzip file (typically suffixed .gz) you will need a different module. Your perlmonks link suggests Archive::Tar. That said, tar is yet another file format used to create a single-file archive of a set of many files. The gzip format itself can only compress a single file. It cannot acrhive many files into a single file so tar and gzip are combined. The zip format by comparison does the archiving and compression in one format. Tar archives, typically called tarballs, are usually combined with a compression mechanism to produce a compressed tarball. Modern tar utilities usually have support built in to invoke appropriate compression tools (gzip, bzip2, and xz are common compression methods in *nix). See http://en.wikipedia.org/wiki/Tar_(computing). Note that the gzip format is only used to compress a single file. If the contents of your gzipped file represents many files then it likely is a tarball (.tar) wrapped in gzip (.gz). These files are often suffixed with .tar.gz or .tgz, but that isn't necessary. If the file that you are trying to decompress is not actually a tarball then perhaps you will want to use a module that isn't meant for tarballs, and is used only to decompress the gzip format. I'm not sure which one will suit you best, but a good starting point would be searching metacpan.org for gzip: https://metacpan.org/search?q=gzip. There appears to be several modules that support the format (which isn't surprising since gzip is originally a Unix-ism as is Perl). There is also an Archive::Any that claims to work as a magical dispatcher to automatically do the right thing for supported formats. You could try that, though again I don't know if it would support raw gzip files that don't contain tarballs. If you still need help let us know what you're trying to do and we'll do our best to guide you to a solution. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Unable to install Net::IP
On Tue, Dec 2, 2014 at 5:17 PM, z...@psoftulsa.com wrote: Hi, Hello, I'm unable to find information on this issue. Whenever I try to install Net::IP (to do a simple DHCP configuration file that will make IP address reservations DNS files that will do forward and reverse lookups) it gives me the following results: root@SERVER:~# cpan -i Net::IP Reading '/root/.cpan/Metadata' Database was generated on Tue, 02 Dec 2014 10:53:28 GMT Running install for module 'Net::IP' Running make for M/MA/MANU/Net-IP-1.26.tar.gz Checksum for /root/.cpan/sources/authors/id/M/MA/MANU/Net-IP-1.26.tar.gz ok CPAN.pm: Building M/MA/MANU/Net-IP-1.26.tar.gz Checking if your kit is complete... Looks good Writing Makefile for Net::IP Writing MYMETA.yml and MYMETA.json MANU/Net-IP-1.26.tar.gz make -- NOT OK Running make test Can't test without successful make Running make install Make had returned bad status, install seems impossible *snip* Perl 5.18: root@SERVER:~# perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi (with 41 registered patches, see perl -V for more detail) *snip* It has been a while since I invoked cpan(1) directly. I recommend you upgrade to cpanm if you can. Regardless, there should be a build log somewhere (try searching ~/.cpan) that will contain the errors encountered while trying to `make'. Worst case, download the module yourself (I think cpan(1) can help with this if you ask it politely), extract its contents, and attempt to build yourself. One you know what errors you are getting that will help to hint to the ultimate problem. If you still need help post up the build output (or wait for somebody that knows here to find it for cpan(1)). I'm running always as root for everything. It makes my server management much easier by saving me from typing sudo for everything I do. That's not a very good idea. Of course, having global sudo access isn't ideal either (Ubuntu is silly in that regard). Look into perlbrew to install a localized perl installation that you can manage yourself without root privileges. Running always as root (or sudo'ing without due care) is asking for trouble. Typically the system perl (e.g., /usr) is asking for trouble or at least pains too simply because you either need root (see above) or need to do magic to not need to, and typically it is out-of-date (though Ubuntu may keep it more up-to-date than other distros, I believe that is still several releases back). Any help would be greatly appreciated! Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: why is if (! @ARGV) skipped
On Fri, Nov 28, 2014 at 9:40 AM, Brandon McCaig bamcc...@gmail.com wrote: # Protip: I'm not sure which is better, interpolating $status # into the format string, or passing it as an argument. I'm # sure it's negligible in this case. printf %-60s $status\n, $abs_path; I lied. To be safe $status should absolutely be a parameter just in case it contains patterns that look like format string placeholders. printf %-60s %s\n, $abs_path, $status; Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: why is if (! @ARGV) skipped
) = @_; # Protip: I'm not sure which is better, interpolating $status # into the format string, or passing it as an argument. I'm # sure it's negligible in this case. printf %-60s $status\n, $abs_path; } sub unlink_file { my ($abs_path) = @_; unlink $abs_path or die unlink $abs_path: $!; report($abs_path, 'unlinked'); } sub wanted { return unless -f; my $abs_path = $File::Find::name; my $filename = $_; unless(is_editor_garbage($filename)) { return; } unless($force || confirm_unlink($abs_path)) { report($abs_path, 'skipped'); return; } unlink_file($abs_path); } } } }, @trgs ); I'd like to see white-space here. White-space is practially free and spares your eyes and brain a lot of unneeded strain trying to figure out where one idea ends and another begins. Use lots of white-space. sub usage{ print Purpose: Recurse thru list of directories and search out all editor leavings (some~ .some.swp some.bak etc), but also includes things like #some# or the like. Usage:`$myscript dir1 dir2 ... dirN' ; Consider using a HEREDOC in this case. I might also recommend the use of printf in a large block of text with a single hidden variable in it because it's easy to miss. printf EOF, $myscript; Purpose: Recursve through list of directories and search out all editor leavings (some~ .some.swp some.bak etc.), but also includes things like #some# or the like. Usage: %s dir1 dir2 ... dirN EOF (Untested) } Hope that helps. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: copy unicode (UCS-2) file
Hans: On Wed, Nov 26, 2014 at 8:49 AM, Hans Ginzel h...@matfyz.cz wrote: Hello! Hi, Consider a small perl code below. It should copy text file with removing leading and trailing spaces. while () { s/^s+//; s/s+$//; print $_, n; # say; } I run it with shell redirection perl copy.pl src.txt dst.txt It works well for Windows ansi and utf-8 text files. But when I have tried an unicode (ucs-2le) source file containing anb this is FF FE 61 00 0D 00 0A 00 62 00 0D 00 0A 00 in hex (with Byte Order Mark) I get characters in hex FF FE 61 00 0D 00 0D 0A 00 62 00 0D 00 0D 0A 00 0D 0A . I have attached these files but I am not sure what Mail Agents do with them. Variable PERL_UNICODE is not set. I have tried add -CS to the command line, but got info about Malformed UTF-8 character. I have tried adding each of these pragmas to the beginning use open ':encoding(UCS-2LE)'; use open IO = ':encoding(UCS-2LE)'; use open ':std' = ':encoding(UCS-2LE)'; but without desired goal. I tried to combine the pragma with -CS option. I have tried use feature qw/say/; say; instaed of print $_, n; but without correct results. perl --version This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x86-multi-thread-64int What is the correct way to set stdin/out to UCS-2LE, please? What is the correct way to print encoding independent new line character, please? What is the correct way to say that s should match the UCS-2LE way, please? You can generally pass an :encoding() LAYER to binmode to specify the text encoding (see perldoc -f binmode). For file handles that you are creating yourself with open you can pass these in directly into the MODE argument (see perldoc -f open). You should of course be using the 3 or more argument version of open regardless, but this matters especially if you intend to use tainted (perldoc perlsec) data to alter the IO layers. I wasn't familiar with the open pragma, but according to perldoc open, in order for it to affect the standard streams (as opposed to new file handles that you create yourself) you must include a :std layer. It looks like you tried that, but were probably doing it wrong. I would guess the incantation you need is: use open ':encoding(UCS-2LE) :std'; Or variations thereof, or thereabouts. I recommend you familiarize yourself with open(), binmode(), and the Encode module to rid yourself of text encoding doubts. I guess since you're using the open pragma you should also familiarize yourself with that, but since there isn't much to the perldoc I'm guessing you already tried to do that. It's actually made pretty easy in Perl, but you do need to have a basic understanding of the system to use it properly. You can also look into PerlIO which provides the mechanisms for doing this automatically on a stream. In addition, is there a standardised way to auto-detect input encoding (legacy(8bit)/utf-8/ucs-2), please? Unfortunately there's no perfect way to detect character encodings. There are many encodings that use all of the same code points and have no identifying features. In general, the encoding needs to be written in the headers or content of a stream (if being sent by machine) or in user-specified options (if being sent by humans). For example, there are ways to specify text encoding for HTTP messages in the headers. There are ways to specify text encoding in an HTML or XML document. Of course, if the content is written in the encoding itself, how are you supposed to read the specified encoding? I'm not sure. I guess you can try to guess until the text makes sense, and hobble along until you find the encoding, and then reinterpret the text properly. Search metacpan.org for guess encoding. I know that there are several modules that attempt to solve this problem. Note that they aren't flawless because they can't be. It's not a problem with a perfect solution. The best option is to have the machine or user that is giving you data also tell you what the format/text encoding of it is. A simple way to do this is to implement a command-line option in your program (e.g., see Getopt::Long) that overrides a sane default encoding (e.g., UTF-8). If you control the software you can also define that the data *must* be in a particular encoding (e.g., UTF-8) and just assume that it is. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: UTF8, UTF-8, utf8, Utf8 encoding blues
Chris: On Sat, Nov 8, 2014 at 12:05 PM, Chris Knipe sav...@savage.za.org wrote: I agree with you - and it also explains what we are seeing in terms of that certain data comes through clean, and others doesn't. I too expect that the *entire stream* is not encoded with UTF8 (even though it should be). In terms of removing the last three characters, that is not what is causing the issue. Even if I remove the substr and pass literally $Body = $Response, the data is still corrupted once it goes out via STDOUT. What is also VERY strange to me is that for some reason when I just do something simple like put a use Encoding into the script, everything works fine. Then half an hour later, or a day or two later, and it stops working and starts becoming corrupt again. And THIS is what has my mind completely baffled. I am the only one with access to the servers (and code), and I am not even logged in on the machines when this sudden change in behaviour happens. It looks like this thread is quiet for 2 days so does that mean that you have solved your problem off-list, given up/failed, or it is still in the works? It doesn't really look like anybody skimmed through the basics of UTF-8 support in Perl in this thread. Whether or not you have already done that yourself appears to be uncertain, but it seems clear from the OP that you aren't comfortable with it and aren't confident in your understanding of it. I'm not an expert at Unicode programming, or Perl, or Unicode programming in Perl, but I have done a little bit of basic UTF-8 handling and for the most part understand the basics of the API. A few basics: # The utf8 pragma is only useful if you want to write Perl source # code directly in UTF-8. Doesn't affect how you read or write to # streams. It affects how the source code itself, including # string literals, are understood. use utf8; # The Encode module contains the core API for dealing with # various encodings. use Encode; # This assumes that $foo is a UTF-8 encoded byte string. To Perl # $foo is just an 8-bit string. $bar becomes a proper Perl # character-based string with all of the Unicode characters being # understood by Perl. my $bar = Encode::decode('utf8', $foo); # This is obviously the reverse of the above. $bar is considered # a Perl string where Perl properly understands each character. # $foo becomes a binary byte-string containing UTF-8 encoded # characters as sequences of bytes. my $foo = Encode::encode('utf8', $bar); __END__ Perl has this concept of its strings being flagged as UTF-8. In short, strings are normally not flagged as containing UTF-8 data, and are assumed to be an 8-bit encoding (e.g., US_ASCII or Latin1). When a string is decoded in Perl (i.e., Encode::decode) Perl will decide if the string it is decoding needs to be internally represented using a multi-byte encoding and automatically does so. It is transparent to the programmer. In such a case, that string would be internally flagged as being in UTF-8, and future character-based operations on that string would automatically take this into account for you. Visa-versa, when a character-based string is encoded you are turning these magic strings back into a fixed byte-based string of raw data (these bytes may or may not be characters). The thing that we programmers need to understand is that Perl doesn't know what the encoding of data coming from the outside is so we have to tell it how to interpret (i.e., decode) it. After that it does everything automatically. Similarly, Perl doesn't know what encoding the outside world can handle so we need to tell it how to encode data whenever we share data with the outside world (i.e., outside of our application). Note also that the encoding 'utf8' is magic in the sense that it is not strict in its interpretation of UTF-8. As far as I know, invalid characters will be silently ignored or perhaps just silently included as is or something along those lines. Check the documentation to be sure. I prefer to enforce UTF-8 strictness. If you know that your data is supposed to be *valid* UTF-8 then I would consider changing those 'utf8' encodings to literally 'UTF-8' (case-sensitive, IIRC). The 'UTF-8' encoding is the strict mode that will signal errors when invalid characters are read. Perhaps that will shed some light on the issue. Or not... What you really need is a specification for the data that you're reading. If you don't know what you're reading then it's basically impossible to properly read it. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: (Possibly a terrible lamer) is `open' format wrong
Harry: (New to thread, reading bottom-up..) On Tue, Nov 4, 2014 at 1:12 PM, Harry Putnam rea...@newsguy.com wrote: Shlomi Fish shlo...@shlomifish.org writes: 1. No need to wrap «$cmd» in double quotes here: http://perl-begin.org/tutorials/bad-elements/#vars_in_quotes 2. You should use the list form: my @cmd = qw(ls /); open my $ch, '-|', @cmd or die Can't open @cmd: $!; (Untested!). Ok, not so hard to do I guess. But why is this important? #1 is explained by Shlomi's URL so I'll assume you are asking about #2. The list form of opening a pipe will properly pass arguments on to the command without worrying about your shell interpreting special characters and doing something unexpected. In this case it doesn't matter because you don't have any special characters, and your command is a literal string without variable interpolation/concatenation, but to future-proof the code and also keep with best practices using the list form is generally a good idea (note that it isn't supported on every platform: I believe that native Windows implementations may not support it). use strict; use warnings; my $user_input = STDIN; # E.g., ';echo rm -fR /;echo ' my $cmd = ls '$user_input'; my @cmd = ('ls', $user_input); # Depending on what the user enters this could do # something wrong, or worse, destructive. open my $fh1, '|-', $cmd or die open pipe: $!; # This should be safe because Perl will take care to # make sure that $user_input is passed as a single # argument instead of being interpreted by your shell. open my $fh2, '|-', @cmd or die open pipe: $!; print STDERR First pipe:\n; print $fh1; print STDERR Second pipe:\n; print $fh2; __END__ (Horribly contrived example...) Look at the output with this example session (first line is just a command line, second line is user input): bash$ perl example ';echo Sneak attack!;echo ' First pipe: Second pipe: ls: cannot access ';echo Sneak attack!;echo ' : No such file or directory ls: cannot access : No such file or directory Sneak attack! WHOA, what is that last line of output all about?! That, good sir, is very dangerous. Avoid destructive words while testing this. The documentation is good. Please read the entirety of `perldoc -f open', but in particular look for the LIST form of opening pipes. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bambams.ca/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Make a sub routine return
appends line information by default so you can leave that second bit off entirely: die 'You must be root to run with this flag (-o) effectively.'; } } As Rob indicated, give us a more specific error if you still need help with your program (it's hard to speculate what is going wrong). If you want more code review then post a more complete program. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Creating a Perl web site, how should I build this
On Wed, Jul 03, 2013 at 10:13:19AM -0700, Robert Freiberger wrote: Hello everyone, Hi, I'm working on a work project where we are moving a few Perl scripts from a command line to a web page that will allow more users to access the tool. Basically it's a very simple script that takes an updated CSV feed, runs a internal test, then reports back the numbers from the test. Ideally, we would like to have this run from a web page instead of from the command line as more people would like to access the tool but don't want to give everyone shell access. But the question is how should we build out this web page? In college I did some CGI Perl writing but it looks like (I could be wrong) that this is no longer the standard, and more people recommend going with Dancer, Mojolicious or Catalyst. If anyone could provide suggestions to get started that would be great. As an ASP Web application developer I am very interested in learning Perl's Web frameworks as much better alternatives. That said, I have been mostly unsuccessful in doing so. Perhaps mostly due to time constraints and lack of motivation. I have made some minor progress with Catalyst though that may help you move forward (or at least evaluate forward). Once you install the Catalyst developer module (e.g., cpanm Catalyst::Devel[1]) you should have a program installed called catalyst.pl which will allow you to create a new application skeleton (try the perldoc or --help for guidance with that tool). Once you have that skeleton you can run it using either the generated script/${app_name}_server.pl program within the application skeleton, or by using an external server, such as Starman (to the best of my knowledge, this is sort of the defacto standard production server to use; it has a more verbose name too, but Starman is indeed easier to remember ;). Starman isn't the only server option available, and you could probably even wire it up with more tradictional Web servers if you care to, but using Starman is probably the simpler option. You will have to evaluate its worthiness in production and overhead in administration (I haven't come close to this point yet). [1] I'm sort of winging this as it has been several months since I have touched anything to do with Catalyst... Time and motivation.. x( To run an instance of your skeleton application using the Starman server you must first install it (e.g., cpanm Starman). Then run it, passing a path to a .psgi file (which can I guess be thought of as the entry point for the PSGI i.e., Catalyst application). E.g., starman ${app_name}.psgi. That is as far as I have gotten unfortunately... It is based on an MVC structure so it will help to have an understanding of that (coming from ASP I probably have a broken understanding of that, but I digress... :). All of this said, if others are right and you only have a simple program that will become a single Web page that just needs to run and generate some simple HTML output then it might well be easier to just hack up a CGI program and wire it up with whatever Web server is currently available. Your mileage may vary... And of course, there are many ways to do it. Just take care to sanitize user inputs, whatever you do... Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: accessing variables in subroutines
. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: last
On Mon, Jun 24, 2013 at 08:20:57PM -0700, John W. Krahn wrote: The parentheses in this case do nothing, and they certainly don't imply that last is a function. $ perl -le' use warnings; use strict; for (1..2) { (); } In this case I'd guess that the parenthesis would be interpretted as an empty list in void context. Next to a next or last statement though that seems to make zero sense so it seems odd that it would be allowed at all. I didn't say that this implies that last is a function/subroutine. :) Only that it appears to behave like one sometimes. That won't work: $ perl -le' use warnings; use strict; while () { my_last; } sub my_last { last; } ' Bareword my_last not allowed while strict subs in use at -e line 5. Execution of -e aborted due to compilation errors. And even if you fix that bareword you still get a warning: $ perl -le' use warnings; use strict; while () { my_last(); } sub my_last { last; } ' Exiting subroutine via last at -e line 8. Touché. :) Apparently I left out strict and warnings from my one-liners and carelessly translated them into strict programs without verifying... :-[ Thanks for catching my mistakes. Nevertheless, it still works, but it warns you about it if you enable warnings (and the subroutine call causes compilation to fail with strict subs). Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: last
On Sun, Jun 23, 2013 at 12:02:38PM +0300, Shlomi Fish wrote: last is not a function (a.k.a subroutine) - it cannot be. It is a special statement which is handled in a special way by the Perl interpreter. redo and next are not functions either for a similar reason. I think we can all agree that last is not a function/subroutine, but at the same time it does behave like one in several ways. :) For example, you can follow it with parenthesis if you want (albeit, there's no good reason to do that). #!/usr/bin/perl use strict; use warnings; while(1) { last(); } __END__ Similarly, you can even wrap it up in a subroutine and it still works as intended (so obviously it understands just how to find the right enclosing block even through subroutine calls). #!/usr/bin/perl use strict; use warnings; while(1) { my_last; } sub my_last { last; } __END__ Perl is just magic. ;) It's just one of those things. It's good to understand that it's special and meant to be a control-flow statement. The perldoc describes it as a command. Particularly surprising is that a stand-alone block is considered the same beast as loops so last will affect that as well! #!/usr/bin/perl use strict; use warnings; do { { last; } print This is reached!\n; } while(0); __END__ Personally I'm of the opinion too that unnecessary labels don't really benefit the code any. They're certainly helpful to jump several levels in a clean way. though I wouldn't oppose using a goto for that in languages that lack labelled next/last/continue/break statements. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: last
On Mon, Jun 24, 2013 at 10:20:36AM +0200, lee wrote: It's logical and expected because an 'if' statement goes like 'if(condition) { is_true; } else { is_not_true;}'. Put a 'last' in front of the 'if' and the 'if' suddenly goes like 'if(condition_is_true) last;' and no more. Flipping things around and breaking stuff like this is illogical and unexpected. The 'if' should still go like 'if(condition) { is_true; } else { is_not_true;}', the only difference being that the prefixed 'last' is also executed when the condition is true and not when the condition is not true. When you think of a natural language like English, prefixing a word with not doesn't change the meaning of the word that follows it, *and particularly it does not change the way how the word is being used in the language*. Prefixing a not only changes the meaning of the /sentence/, like It is raining now and we need to close the windows. vs. It is not raining now and we do not need to close the windows.. What perl does is like forbidding to use any more words after raining and need for instances where raining or need is prefixed with not. That's just a very silly thing to do because it creates a different word (which is written exactly the same) the usage of which is different from another word (which is written exactly the same). Usually, words that have a different meaning and/or are being used in a different way in a language aren't the same and are written differently. Perl uses different words, but written exactly the same, with different meanings, and they have different meanings because they are used differently within the language (which is what makes them different words). That leads to unexpected behaviour. I think that you are just misunderstanding what is happening here. `perldoc perlsyn' explains it. These are actually two different things. The compound if-statement, which is typical of other languages, and the if-style statement modifier. The syntax of the statement modifier is: STATEMENT if EXPR; Where STATEMENT is a single statement (what that statement is doesn't really matter, though the documentation does say simple so perhaps there is a limit). For example: last if condition(); It is basically just syntactic sugar to get rid of some braces. It is limited to a SINGLE modifer though so you can't chain them (i.e., ... if ... if ... if ...). It's meant to make code easier to read. Using it for complex statements or complex conditions will likely reduce the readability. In that case, using the plain old if-statement makes better sense. The syntax for that is: if(EXPR) BLOCK Where BLOCK is enclosed with braces. So sticking with our previous example you could express that as: if(condition()) { last; } So your attempts to add a last before the if-statement above are futile. You are combining two different language constructs and they are not compatible. And it's a good thing because I find that hypothetical syntax quite difficult to read. Using English terms, what you're attempting to say is more like: Stop here if we've iterated thrice write 'if' tracing message. Obviously that doesn't make sense. It needs more language constructs to separate the ideas. For example, perhaps a word like and. While it might have been possible to support such complexities in a language like Perl, and indeed Perl probably goes further than most languages in this regard, apparently it was decided not to go that far in this case. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Tips or Tricks to print out tab-separated data?
On Fri, Jun 21, 2013 at 02:15:39PM +0100, Gary Stainburn wrote: What your code did was to tag the \n onto the end of the array then pass the whole thing to join. Gary is basically correct, but he worded it wrongly. When Perl calls subroutines it basically flattens arguments into a list. Earlier, Mark Perry wrote: my @array = ( boris, natasha, rocky, bullwinkle); print join \t, @array, \n; In this case, you're calling two subroutines: print and join. The arguments are \t, @array, and \n. The contents of @array are substituted in its place, so the arguments end up being \t, boris, natasha, rocky, bullwinkle, and \n. Nothing special is done to disambiguate which argument is for which subroutine so Perl does the usual thing of handing them all to the nearest subroutine: join. In short, the \n is passed to join, so a tab character is injected into the stream before the newline. The misleading thing that Gary said was that the \n gets tagged onto the end of the array, implying that the array is modified and then passed to the join subroutine. Of course, the array is unmodified, so really the \n gets tagged onto the end of the join argument *list*. @array is unaffected, but the newline is delivered to the wrong subroutine. Using parenthesis as John specified solves the problem; or by alternatively using say() instead of print(), which automatically writes a newline after writing the arguments. say() is only available in non-ancient perl's and only available when enabled (typically by -E or use v5.010 [or whatever the first version is to include it] or some equivalent pragma). When you want to write lines that don't already contain a newline it is an ideal shortcut though. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: make and Makefile.PL
On Mon, May 27, 2013 at 09:21:41PM +0100, Rahim Fakir wrote: I have download Make for windows 7, but I have problems, with make nmake, and Makefile.PL, i don't know how to install them. Strawberry Perl ships with a dmake.exe. Try that. E.g., perl Makefile.PL dmake dmake test dmake install Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: help with some code
On Thu, May 23, 2013 at 08:45:05PM +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. BEGIN failed--compilation aborted at janelahello.pl line 3 In addition to what others have said (you need to install Glib.pm) you are probably going to first have to install the binary dependencies for Glib.pm if you haven't yet since I believe that is a Perl module that provides bindings to a C library related to the cross-platform Gnome desktop environment. You may have to do some guess work to figure out what those dependencies are (i.e., Gnome). First check the documentation, but often you'll just have to infer what's missing from the build errors and figure out how to install those things. Don't be deterred if you have trouble or fail at first. It takes practice and experience and we all go through it. Since you are on Windows you should first make sure that Glib.pm is supported on Windows (I haven't used it before so I couldn't tell you). If it isn't then you can't expect it to Just Work(tm), even if you do get its usual dependencies installed. If it is supported on Windows then it should Just(tm) be a matter of satisfying its dependencies, installing the Glib module from CPAN, and finally running your program with a strong sense of accomplishment. My advice would be to set yourself up with a GNU/Linux distribution installation to experiment with these things. You will generally have a much easier time installing dependences, especially if you have to build them from source, and often your Linux distribution's package manager will have them prebuilt (popular CPAN modules will also be packaged up for you, which won't necessarily give you the latest version, but will at least give you an easy button to jump right in, which is ideal for beginners). If you're new to Linux then you'll have that hurdle to climb first, but in the long run I think it'll balance out... Good luck. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: problem with File::Util::readlimit
On Thu, May 09, 2013 at 08:51:23AM +0200, Luca Ferrari wrote: In fact I was discovering it too reading the code, I've submitted a bug report for this. I also submitted a bug report (so now I feel doubly bad for not letting the list know so he didn't get duplicates; twice the noise). I was informed that the latest code is already fixed. You can apparently get it here: http://cpan.metacpan.org/authors/id/T/TO/TOMMY/File-Util-4.130610-TRIAL.tar.gz. The maintainer informs me that despite it being a TRIAL release it has months of stability and he plans to release it soon as mature. So maybe keep an eye out for that too. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: curiosity about the usage of my
On Thu, May 09, 2013 at 12:15:00PM +0200, Luca Ferrari wrote: Hi, Hello, the usage of my to scope variables is a good habit, and under strict is almost a need. But just today I realized that having to write my in front of each block of variables does not seem to me a perl-ish way of doing things: it requires extra effort to a quite simple task (variable declaration). Is there any ongoing effort to make my the default behavior (for example in Perl 6, that I'm totally unaware of)? I think that it makes sense to make it explicit. my() and our() let you know when a variable comes into existence. They're also quite terse as it is. It's really not a lot of work. I'd rather know when I'm bringing a variable into the world, and have the compiler yell at me if that variable already exists in this scope because it means I'm making a mistake; I'd rather catch that at compile-time then maybe catch it at run-time. # Contrived subroutine... sub foo { $foo = $_[0] * $_[1]; $bar = $foo ** $_[2]; return $foo == 0 ? undef : $bar / $foo; } So assuming this hypothetical Perl where lexical variables just pop into existence, are $foo and $bar lexical variables local to the sub, closures from an outer sub, lexicals at file scope, or are they package variables? What if the code around them changes? Let's say 300 lines up (several screens away) you introduce a new $foo at the file scope. Does that change the meaning of sub foo? With an explicit my() declaration it wouldn't. Without it, I think it would have to. That's a dangerous world to live in. I think my() and our() are awesome the way they work now. (Interestingly the perldoc explains that our doesn't necessarily create a variable, but it associates the name within a lexical scope with the package; for intents of this discussion I think that's just semantics) In any case, for backwards compatability purposes it probably isn't possible to change this for Perl 5. I don't think you'd want to change it either. /nitpick I always find it annoying when people declare laziness as a good trait for programmers. I disagree. Lazy means not doing something because it takes effort, even if it's a good or correct thing. It's a good trait for programmers to avoid waste, but not if it comes at the expense of reliability, security, or robustness. Using my() and our() takes very little effort and is well worth the investment. /rant Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: problem with File::Util::readlimit
On Wed, May 08, 2013 at 06:49:48PM +0200, Luca Ferrari wrote: Hello, Hello, I'm encountering a problem with File::Util that I'm not able to see, and I'm sure it's trivial: my $handle_file = new File::Util; my $files_for = {}; my $max_file_size = int( 1024 * 1024 * 1024 ); $handle_file-readlimit( $max_file_size ); that once run produces the following trace: PROCESS TERMINATED DUE TO ERRORS *snip* Any idea about what I'm missing? Looks like a module bug to me. A freshly installed File/Util.pm said: sub readlimit { my $arg = _myargs( @_ ); if ( defined $arg ) { return File::Util-new()-_throw ( 'bad readlimit' = { bad = $arg } ) if $arg !~ /\D/o; $READLIMIT = $arg; } return $READLIMIT; } Looks like it raises an exception if the argument doesn't match a NON-digit character. Which seems like the opposite of what is wanted. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Lines written to a file are not contiguous
. And for future reference, non-ancient versions of Perl have a say() alternative to print() that appends a newline for you. close(WRITE) || die Unable to close $write_file: $!\n; close(READ) || die Unable to close $file: $!\n; } } 1; If that isn't enough to figure out your problem then I think you're going to have to take another shot at explaining what it is that you're trying to achieve, and what you're getting instead. Many of us will not have the aforementioned book so you should try to explain it to us assuming no external knowledge. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: split
On Fri, May 03, 2013 at 09:59:49PM +1000, Edward and Erica Heim wrote: Hi all, Hello, I'm using LWP::UserAgent to access a website. One of the methods returns HTML data e.g. my $data = $response-content; I.e. $data contains the HTML content. I want to be able to parse it line by line e.g. foreach (split /pattern/, $data) { my $line = $_; .. If I print $data, I can see the individual lines of the HTML data but I'm not clear on the pattern that I should use in split or if there is a better way to do this. I understand that there are packages to parse HTML code but this is also a learning exercise for me. You haven't explained what it is you're trying to do so it's impossible for any of us to help you. There is no correct pattern here. It depends entirely on what the input data can be, and what you're trying to accomplish. What is it that you want to learn? If you want to learn how to process HTML data then you don't want to use split to do it. It's a flawed and error prone way (there is no /pattern/ that will work on all possible HTML inputs). You're far better off learning to use one of the HTML parsing modules because that will give you a reliable mechanism to process HTML data. If you just want to learn how to use split then by all means go for it. Just don't expect to get meaningful results when processing unpredictable HTML data. :) Give us more information if you want help. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Can't close filehandle
On Wed, May 01, 2013 at 04:22:42PM +0200, Manfred Lotz wrote: Hi there, Hello, I have a script where I log stuff to a file and the same time displays it to stdout using Log4perl. Here is a minimal example where I log a command which fails. snip- #! /usr/bin/perl use strict; use warnings; #use autodie; use Log::Log4perl qw(:easy); Log::Log4perl-easy_init( { level = $DEBUG, file = test.log, }, { level = $DEBUG, file = 'STDOUT', } ); my $cmd = 'uname'; my $parms = '-f'; # invalid parm INFO( Issuing [$cmd $parms] ); open my $fh, '-|', $cmd $parms 21 or die open: $!; INFO( $_ ) while $fh; close $fh; snap- This works great. Output is like this: 2013/05/01 16:16:40 Issuing [uname -f] 2013/05/01 16:16:40 uname: invalid option -- 'f' 2013/05/01 16:16:40 Try 'uname --help' for more information. However, if I add autodie then I get: 2013/05/01 16:16:33 Issuing [uname -f] 2013/05/01 16:16:33 uname: invalid option -- 'f' 2013/05/01 16:16:33 Try 'uname --help' for more information. Can't close(GLOB(0x2554c80)) filehandle: '' at ./test02.pl line 20 close does not fail if the command is ok, e.g. 'uname -a'. Can anybody explain to me why close fails in the example above? One thing that I know bit me with the 'or die' pattern is that with a pipe if the child process exits with an non-zero exit status then close returns undef, even though there is really no problem closing the pipe. I had to read the perldoc closely to notice this. So with the 'or die' pattern I had to change it around a little bit, from: close $fh or die close: $!; ...to... close $fh or $? != 0 or die close: $!; I'm *guessing* that the same thing is biting you, in that when the piped process signals failure the close also signals failure, even though there was no actual error with close, and autodie just follows suit and dies. There's no meaningful errno because no error actually occurred within your process. My conclusion appears to be confirmed with the following one-liners: # This works fine. perl -Mautodie -E 'open my $fh, -|, /bin/true; close $fh;' # This fails with the same error you get. perl -Mautodie -E 'open my $fh, -|, /bin/false; close $fh;' So perhaps this is a bug in autodie, or perhaps you should just not use autodie with close and a pipe... I guess a work around should be to disable it for the close: use autodie; open my $fh, -|, /bin/false; { no autodie qw/close/; close $fh or $? != 0 or die close: $!; } Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Can't close filehandle
On Wed, May 01, 2013 at 11:45:00AM -0400, Brandon McCaig wrote: One thing that I know bit me with the 'or die' pattern is that with a pipe if the child process exits with an non-zero exit status then close returns undef, ... Sorry, close returns false to signal failure, not undef. close $fh or $? != 0 or die close: $!; Reading over the perldoc again, the correct check might be $!, not !?. I guess if the only reason close is signalling failure is the exit status of the child process then close will set $! to 0. So perhaps... close $fh or $! == 0 or die close: $!; Hopefully that covers all of my errors.. _ Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Current time with DST
On Thu, Apr 18, 2013 at 07:26:13AM -0300, Brian Fraser wrote: On Wed, Apr 17, 2013 at 10:24 PM, Brandon McCaig bamcc...@gmail.com wrote: Using the warnings pragma is good! You should also use the strict pragma: use strict; This is not needed. Since 5.14, 'use VERSION' automatically enables strict. Thanks for correcting me. :) I keep forgetting that some pragmas are implicitly turned on when features or modules are used... :( Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Current time with DST
On Thu, Apr 18, 2013 at 01:00:38AM +0100, Ricardo Pais Oliveira wrote: Hi! Hello, I'm using DateTime::TimeZone to get my current time. Shouldn't daylight saving time be handled by the DateTime module? In $date I'm obtaining the time with less an hour than it should so I believe DST is not being considered. How can I obtain the current time regardless it's in DST mode or not? Here's my script: use 5.14.2; use warnings; Using the warnings pragma is good! You should also use the strict pragma: use strict; use DateTime::TimeZone; my $tz = DateTime::TimeZone-new( name = 'Europe/Lisbon' ); my $date = DateTime-now(); print $date; Looks to me like the problem is that you're not specifying the time zone for DateTime::now. Try this: my $date = DateTime-now(time_zone = $tz); Seems to work on my end. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: perl unicode utf8 and JSON::XS
On Sun, Apr 07, 2013 at 06:08:52PM +0200, Christophe Martin wrote: Hello , Hello, I'm trying to use JSON::XS. The trouble is that I have French strings with accented chars. The solutions I tried don't work. Also, I'm not sure i understand JSON::XS doc on unicode and utf. I use linux. The shell, terminal, and files all use utf-8. Locales are installed, and I have LANG=fr_FR.UTF-8 and a bunch of LC_THING=fr_FR.UTF-8 env variables. Apart from my script, everything works perfectly well. As you can see below, with use utf8;, json is ok and can be decoded by other progs, but perl strings turn to latin1 encoding. with no utf8 or without use utf8, json strings are «utf8 encoded twice» but simple perl strings are ok. Ubuntu linux 12.04 with perl 5.14.2, and JSON::XS 2.320-1build1 if that matters I must have missed something, hopefully simple. Any Idea ? #! /usr/bin/perl use utf8; use strict; use warnings; use JSON::XS; my %srchash = ( 'éléphant' = 'ça trompe' ); my $json = encode_json \%srchash; my %dsthash = %{decode_json $json}; while ( my ($key, $value) = each %srchash ) { print $key, ' = ', $value, \n; } print $json, \n; while ( my ($key, $value) = each %dsthash ) { print $key, ' = ', $value, \n; } The results without use utf8; (or with no utf8;) : éléphant = ça trompe {éléphant:ça trompe} éléphant = ça trompe with use utf8, here is what I get �l�phant = �a trompe {éléphant:ça trompe} �l�phant = �a trompe The documentation says that the exported decode_json and encode_json subs expect the input and output to be UTF-8 encoded. That is, they expect the keys and values to be binary strings encoded in UTF-8. Then internally JSON::XS will decode and encode as necessary. The utf8 pragma tells Perl that the source code is UTF-8 encoded, and that Perl should automatically decode scalar strings (including hash keys), etc., automatically. So I think that the use cast for decode_json and encode_json is different, though I honestly can't figure out what it would be... This seems to work though. Note that I explicitly set the IO encoding layer for the STDOUT handle and use the JSON::XS OO interface with automatic UTF-8 decoding/encoding disabled (since Perl has already handled that for us). #! /usr/bin/perl use utf8; use strict; use warnings; use Data::Dumper; use Encode; use JSON::XS; binmode \*STDOUT, ':encoding(UTF-8)'; my %src = ( 'éléphant' = 'ça trompe' ); my $jsonizer = JSON::XS-new(); my $json = $jsonizer-encode(\%src); my %dst = %{$jsonizer-decode($json)}; print $_\n for keys %src, values %src; print $json, \n; print $_\n for keys %src, values %dst; __END__ Since we used the utf8 pragma Perl is already decoding the keys and values as UTF-8 and storing them appropriately internally (the details of which shouldn't matter to us). So JSON::XS shouldn't need to do any character encoding/decoding with the data. It is already internally stored properly and all operations on those values in Perl should be character-wise automatically. I'm not really sure then what purpose JSON::XS::utf8 is intended to serve. It sounds to me like when enabled it expects the input data structure to be binary (UTF-8 encoded data). I'm not sure under what circumstances you'd want hash keys to be in binary when you could instead just decode the data from where ever into text strings and happily work with them from there... I wonder if maybe it could be a bug in JSON::XS that it modifies these already Unicode-aware scalars. I am not qualified to assert that it is so. The documentation seems clear that the behavior is by design and I have to assume that the module author(s) know more about Unicode in Perl than I do. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: perl unicode utf8 and JSON::XS
On Mon, Apr 08, 2013 at 01:07:13PM -0400, Brandon McCaig wrote: use Data::Dumper; use Encode; I should note that neither of these packages were used in the example program that I posted and aren't required for it to run. :) They were just remnants from debugging... Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Exposed Email Addresses
Angela: CC'ing the list so you hopefully don't get 300 of these messages. :) On Wed, Mar 27, 2013 at 03:39:45PM -0700, Angela Barone wrote: I just noticed something that's very disconcerting. Our email addresses are being posted online from this mailing list. Does anyone know about this? I was doing a search when I came across this page: http://www.nntp.perl.org/group/perl.beginners/2013/03/msg122680.html As you can see, my email address is there for the world to see. Is there a way to prevent this from happening? It seems it would be so easy for someone to scape that site for our addresses. The list blocks the address of the sender, but if there's an address in the body of the email, it's sent through. Yes, I'm sure most of us are aware. My E-mail address(es) are also in my public GitHub repositories, and I've probably knowingly posted them to public discussion boards on a few occasions. I still barely get any spam, despite this. I only get 1 - 5 spam messages per day, and 95% of them are caught by G-mail's spam filter. It's just part of my daily routine to open that mailbox, scan it for a false positives, and then 'delete all'. Very shortly after I posted to this list I started getting spam. I couldn't figure out what had happened, but this could be why. There's really nothing that you can do about it. It's a public mailing list so I think that anybody can freely archive it (there's no way to control how they do that). On top of that, everybody subscribed to the mailing list also has every poster's E-mail address. So even if you couldn't scrape the archive for them you would just have to subscibe to the mailing list and you would still be handed the addresses. There's no real difference. You just have to live with it, or find/write spam filters. Alternatively, you could post to the list with a personal E-mail address that you don't care about (it's a little late for that now though). BTW, I noticed it's not the same for everyone. Some peoples addresses are shown while others are not. That probably has more to do with the mail client (or person) that wrote the quote header, not the archive software. It appears that my mutt config is currently only attributing the message to your name, not your E-mail address. Which sure, helps to keep your address out of archives, I guess, but it could also cause confusion if somebody else starts posting as the same name. I don't remember if I configured that or if it's default.. Some clients might truncate the address to make it invalid or something while still giving sufficient context to match an address. That sounds reasonable, but still not flawless. Welcome to the wild, wild, Intarnetz. :) There's little point to unsubscribing too. Your E-mail address is already publicly visible on the Web. And the spam could be unrelated to the list anyway. Anybody could have passed your E-mail address along until it found its way to a spammer's bot. Just avoid replying to any spam messages, and avoid fetching any URLs found within them (make sure your mail client doesn't do this automatically) and hopefully the spammers will move on to more vulnerable prey. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: IPC::Open3 Usage
On Thu, Mar 21, 2013 at 11:12:54PM +0100, Dominik Danter wrote: Hi I just don't understand the perlfaq example. All I want is to capture output of an external command's stdout and stderr. Here is what I've tried: sub get_exit() { Careful with those parens. You may not realize, but that means something special to Perl. It's called a prototype, and what you're telling Perl is that get_exit accepts no arguments. This matters because it will determine what things like this do: get_exit $exit_status, $std_out, $std_err; Without a prototype it means what you think: get_exit(-1, $std_out, $std_err); With an empty prototype as you have it, it actually means: get_exit() 1, $std_out, $std_err; # ^ Syntax error. Operator expected. Syntax error. It doesn't make sense. However, if you do happen to have an operator there, such as + or -: get_exit -1, $std_out, $std_err; Now it is syntactically correct, but doesn't do what you want it to. Instead it means: get_exit() - 1, $std_out, $std_err; So get_exit gets no arguments, 1 is subtracted from the return value (which is a hash reference) as a number, and finally the comma operators ultimately result in $std_err in a void context. The warnings pragma will give you a bunch of warnings for this. You shouldn't use a prototype unless you want built-in semantics (i.e., so you can call your sub without parenthesis and still have the right arguments passed to it). See `perldoc perlsub' for all the details. Getting them right can be complex. There's no real need for them though if you call your subs with parens. So this should be: sub get_exit { my ($exit_status, $std_out, $std_err) = @_; my %error_codes = ( 1024 = 'uid already exists', 256 = 'not root', 0= 'success', ); my $message = $error_codes{$exit_status}; return { message = (defined $message ? $message : 'unknown exit status'), exit_status = $exit_status, std_out = (defined $std_out ? $std_out : 'no standard output produced'), std_err = (defined $std_err ? $std_err : 'no error output produced'), }; } sub change_id { my %command = ( uid = 'usermod --uid', gid = 'usermod --gid', ); my ($type, $username, $new_id) = @_; my ($std_in, $std_out, $std_err, $exit_status); eval { my $pid = open3($std_in, $std_out, $std_err, $command{$type} $new_id $username); waitpid( $pid, 1); $exit_status = $? 8; }; return get_exit($exit_status, $std_out, $std_err ); That bit above about prototypes doesn't apply when you call subroutines with , which may well be why you're using it in the first place (otherwise you'd get a syntax error), but you shouldn't be calling a subroutine with anyway unless you intentionally want to ignore the prototype. In this case, the prototype shouldn't be there anyway, so there's no good reason to use . Once you get rid of the prototype you can call it normallly: return get_exit($exit_status, $std_out, $std_err); } print Dumper(user::change_id('uid','bla',997)); Here is my output: Use of uninitialized value $exit_status in hash element at user.pm line 31. $VAR1 = { 'std_err' = 'no error output produced', 'std_out' = \*Symbol::GEN1, 'exit_status' = undef, 'message' = 'unknown exit status' }; The command should fail since it lacks required permission, but I'd like to handle the error messages gracefully. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Installing from CPAN -- YAML
On Wed, Mar 20, 2013 at 04:33:28PM +0100, Klaus Jantzen wrote: Hi, Hello, when installing a module from CPAN I always get the hint that YAML is not installed. To solve the problem I tried to find YAML on CPAN and found dozens of pages with YAML. Scanning the entries I detected CPAN::Meta::YAML. Its documentation seems to indicate that that is what I need. I installed it but still get the message 'YAML' not installed, will not store persistent state. So what should I install to satisfy the 'installer '? Try installing 'YAML'. ;) bash$ cpan YAML HTH. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Problem Installing perlbrew
Angela Barone: On Tue, Mar 19, 2013 at 11:42:56AM -0700, Angela Barone wrote: I tried that, but it didn't work. It turns out that when I used OS X's Terminal, everything installed smoothly. But if I tried iTerm, then I got the -bash: perlbrew: command not found error. This makes no sense to me. I thought both should operate identically, but apparently not. If anyone knows how to get perlbrew to work in iTerm, I'd appreciate hearing from you. I am not familiar with iTerm, but Google reveals that it is an open source terminal emulator for OS X. So what probably matters here is the shell that it is running (I would assume it would be the same shell as your other terminal emulator program, but perhaps not..). Is it possible that it's not running bash, and therefore your .bash_profile is not being sourced, and therefore the Perlbrew environment is not being set up? What if you try sourcing the perlbrew bashrc yourself directly from iTerm: source ~/perl5/perlbrew/etc/bashrc Does that execute without errors? Does perlbrew work then? As an aside, I have found that recent versions of bash (in GNU/Linux) seem to cache the PATH and not notice changes to it right away (albeit, you said you were upgrading Perlbrew so I don't think it would have changed much, but I digress). You can clear that cache with `hash -r' from the affected shell. You can also just start a new shell. I doubt this is applicable to your situation, but you never know... :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Problem Installing perlbrew
On Wed, Mar 20, 2013 at 10:22:04AM -0400, Brandon McCaig wrote: Is it possible that it's not running bash, and therefore your .bash_profile is not being sourced, and therefore the Perlbrew environment is not being set up? Nevermind... On Tue, Mar 19, 2013 at 11:42:56AM -0700, Angela Barone wrote: But if I tried iTerm, then I got the -bash: perlbrew: command not found error. I'm going back to bed. _ Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Dynamically created perl script
On Tue, Mar 19, 2013 at 12:21:10AM -0300, Brian Fraser wrote: You can just pipe a program into perl and it'll DWIM: $ echo 'print Hello World, Perl $^V\n' | perl You can also be more explict about the program being on STDIN: bash$ echo 'print Hello , World!\n' | perl - The - tells it to read the program from STDIN. This allows you to pass arguments to the program if necessary. bash$ echo 'print $_\n for @ARGV' | perl - foo bar baz From within Perl you can open a pipe to perl. For example: #!/usr/bin/perl use strict; use warnings; my $program = 'EOF'; use strict; use warnings; print $_\n for @ARGV; EOF my @args = qw(foo bar baz); open my $fh, '|-', qw(perl -), @args or die open pipe: $!; print $fh $program or die write to pipe: $!; close $fh or $! == 0 or warn close pipe: $!; __END__ See perldoc -f open and perldoc perlrun. Of course, there's always eval() too, but that has certain drawbacks (in particular it executes within the scope and context of your program and can therefore tamper with it). #!/usr/bin/perl use strict; use warnings; hello();# Prints Hello, World\n! my $program = 'EOF'; use strict; use warnings; hello();# Prints Hello, Satan!\n sub hello { print Hello, Satan!\n; } EOF eval $program; die $@ if $@; hello();# Oops, prints Hello, Satan!\n sub hello { print Hello, World!\n; } __END__ Here is an evil example where the evaled program tampered with the outer program in ways that may be undesirable. If you control the inner program then you can do things to make sure that doesn't happen accidentally (and avoid doing it intentionally). Thanks to the warnings pragma we do get a warning about it, but that can be surpressed, and regardless the damage may be done already. See perldoc -f eval and perldoc warnings. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: anonymous array for loop
Protip: you should stay on list for this. :) Cc'ing the list. On Sun, Mar 10, 2013 at 11:16:50AM +, John Delacour wrote: On 09/03/2013 20:57, Brandon McCaig wrote: You cannot have anonymous arrays in Perl except by reference. What about this? for (split //, abc) { print $_\n; } There is no array here. There are only lists. See perldoc perldata. List value constructors List values are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it): (LIST) In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element, as with the C comma operator. For example, @foo = ('cc', '-E', $bar); assigns the entire list value to array @foo, but $foo = ('cc', '-E', $bar); assigns the value of variable $bar to the scalar variable $foo. Note that the value of an actual array in scalar context is the length of the array; the following assigns the value 3 to $foo: @foo = ('cc', '-E', $bar); $foo = @foo;# $foo gets 3 I'm not sure exactly where list values are explained (I think it is explained more directly than this, but this is what I could find at the moment). If you read through the docs you will discover that subroutine arguments and return values are expanded into lists automatically (exact semantics may be altered by prototypes or built-in semantics). Thus, in your example above you passed split /PATTERN/ and /EXPR/ (split is built-in and its semantics are not the same as user-defined functions), and in exchange split returned a list of ('a', 'b', 'c'), which your for-loop then iterated over. At no point was an actual array allocated for this data. For example: @bar = (1,2,3); foo(@bar); It may look like you are passing foo an array, but really you are passing foo a list of values (which are aliased). sub foo { # The list passed to foo is in @_. $_[0]++; # Here we can increment the caller's @bar[0] # because arguments are always aliased in Perl. # (Note: you generally /shouldn't/ do this) unshift @_, '0'; # However, modifying @_ doesn't affect # the caller's @bar in any way. They are # not the same array. } For an actual executable example: #!/usr/bin/perl use strict; # Should always use strict and warnings. use warnings; use Data::Dumper; main(); sub main { my @bar = (1,2,3); print q/Before: main's @bar is: /, Dumper \@bar; foo(@bar); print q/After: main's @bar is: /, Dumper \@bar; } sub foo { print q/Before: foo's @_ is: /, Dumper \@_; $_[0]++; unshift @_, 0; print q/After: foo's @_ is: /, Dumper \@_; } __END__ Output: Before: main's @bar is: $VAR1 = [ 1, 2, 3 ]; Before: foo's @_ is: $VAR1 = [ 1, 2, 3 ]; After: foo's @_ is: $VAR1 = [ 0, 2, 2, 3 ]; After: main's @bar is: $VAR1 = [ 2, 2, 3 ]; Notice that $bar[0] is modified, but @bar itself could not be (i.e., more elements couldn't be added from within foo). The thing for for-loops to iterate is also a list, so if you say: for (@bar) { ... } You are really saying to expand @bar into a list and iterate over that. Which is why you can also directly loop over a hash (though it's generally less meaningful to do so): for (%baz) { ... } The first iteration will be the first key (which isn't necessarily the first key/value pair you stored), the second will be the value stored in the hash by that key, the third will be the second key, the forth its value, etc. And of course, you can just pass a list value instead: for (1,2,3) { ... } Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: anonymous array for loop
On Sun, Mar 10, 2013 at 11:06:14AM -0400, Brandon McCaig wrote: There is no array here. There are only lists. See perldoc perldata. List value constructors List values are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it): (LIST) *snip* Sorry, I meant to properly identify the source of that quote. That was of course perldoc perldata, not John. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: anonymous array for loop
On Sat, Mar 09, 2013 at 07:24:37AM -0600, Chris Stinemetz wrote: Each anonymous array @files and @newFiles has 5 total elements. Just to nitpick, @files and @newFiles are not anonymous arrays. They are just arrays. They have names. You cannot have anonymous arrays in Perl except by reference. That is, [] gives you a reference to an anonymous array. my @named_array = (1,2,3); my $anon_array_ref = [1,2,3]; my @named_array_copy_of_anon_array = @$anon_array_ref; Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Running into dependency hell with Cygwin when trying to install Excel-Writer-XLSX-0.65 and have all tests pass and not skipped
On Fri, Mar 08, 2013 at 05:25:53PM -0800, Jim Gibson wrote: So what's the best Perl module that recursively installs all prerequisites of a package for you, jsut supplying the desired package name for installation? The CPAN module is want you want. It installs the prerequisites of a module. It comes with an executable ('cpan'). See 'perldoc CPAN'. App::cpanminus is often a more convenient module to use. The wrapper command is called 'cpanm' (versus 'cpan' for the CPAN module). CPAN will typically prompt you constantly to install each recursive dependency which gets old after about the 5th prompt, and will show you all of the output by default, which if successful usually doesn't matter to you. If all you want is for the modules to be installed then App::cpanminus does a much prettier, less interactive job of it, hiding the output by default, but storing it in a log file if you want to inspect it after (particularly if the modules you are installing could not be installed). I believe there is also an App::cpanplus that you can look into, which I assume is named that because its author felt it does more i.e., is more featureful. I haven't used the latter myself. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: anonymous array for loop
On Sat, Mar 09, 2013 at 08:19:43PM -0600, Chris Stinemetz wrote: I don't think that is true. Example being: #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $firstVar = One; my $secondVar = Two; my @array; push @array,[$firstVar, $secondVar]; print Dumper \@array; for my $item ( @array ) { print join(\t, @$item[0], @$item[1]), \n; } ## output ## $VAR1 = [ [ 'One', 'Two' ] ]; One Two I do not see the point you are trying to make. You created a named array, stuffed an anonymous array ref into it, and then printed its elements out (in a somewhat odd way). What does this demonstrate? Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Return values more than 256?
On Thu, Mar 07, 2013 at 10:21:40AM +0100, WFB wrote: Hi, List, Hello, To test our software I use perl to start it several times. My perl script gather some information and start then the program with different parameters. It works very well, but I have a problem with the return values of our program. This return codes are all in an area from 55000 to 6. I use open to invoke our program and print the output. Finally I use the $? variable and print the error code in case of an error. sub start_test_runner { my ($tr = shift, $tr_params) = @_; I don't see a need for shift here. You can just assign the arguments array to your list of parameters. my ($tr, $tr_params) = @_; my $pid = open(my $trexe, $tr \$tr_params\ |) or die Could not start TestRunner. $!\n; Instead of escaping the double-quotes consider using the qq// operator instead. You should also prefer the 3-argument open. my $pid = open(my $trexe, '-|', qq($tr $tr_params) or die Could not start TestRunner. $!; while($trexe) { print $_ if $verbose; No need to reference the default variable with print. That is implied with no arguments. print if $verbose; } waitpid($pid, 0); close($trexe); my $tr_ret = ($?8); return $tr_ret; } now I read the perldoc perlvar section about $? and realised that only the above 8 bits are used for the return value. The first 8 bit are used for the system codes. As far as I that understand fit just 256 values in those 8 bits. Obviously, the error codes given back from my scripts are always wrong. For example, if the error code is 55028 I get 62464. With right shift the script prints 244. I think on most platforms 0-255 is the range of possible exit codes. What platform are you on? Apparently Windows supports 32-bit integers... The Web suggests that if you want to get full 32-bit integers on Windows then you should use the Win32::Process module instead of open. It's not portable, but at least it will do what you need. Note: 55028 255 = 244. So what is happening is the original exit code is being truncated down to one byte. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Return values more than 256?
On Thu, Mar 07, 2013 at 10:05:56AM -0500, Brandon McCaig wrote: Apparently Windows supports 32-bit integers... The Web suggests that if you want to get full 32-bit integers on Windows then you should use the Win32::Process module instead of open. It's not portable, but at least it will do what you need. I suppose I should include a link[1][2] to the SO thread I referenced since this advice is coming from there. Especially because it offers an alternative option (using Win32::API to make a system call with the PID). At a glance I'm not sure how to access the standard streams with Win32::Process so perhaps using the GetExitCodeProcess system call as mentioned in the SO thread would be a convenient alternative to do everything you want. I cannot say. :) [1] http://stackoverflow.com/questions/6786812/is-it-possible-to-process-exit-codes-255-with-perl [2] The thread is titled Is it possible to process exit codes 255 with perl, just in case I typed the URL wrong. :) Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Return values more than 256?
On Thu, Mar 07, 2013 at 02:24:58PM -0800, John W. Krahn wrote: Or, instead of the three argument open, use the list option and you won't need quotes: my $pid = open my $trexe, '-|', $tr, $tr_params or die Could not start TestRunner. $!; Good catch. :) I didn't think that open worked with a pipe and LIST, but apparently it does now. It must have been an older Perl that screamed at me, I guess. Thanks for pointing it out. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Line Endings
On Sun, Feb 24, 2013 at 11:17:40PM -0300, Brian Fraser wrote: my $over = ''; while ( sysread( $fh, $over, 8192, length($over) ) ) { while ( $over =~ /\R/ ) { my $line = encode('UTF-8', substr($over, 0, $+[0], '')); I think you meant decode. :) Decode input, encode output. The internal storage that Perl uses isn't relevant. Apologies if I'm wrong. I am tired. :) I would probably suggest moving the substr call onto a separate line because it's already obscure that it has side effects without explicitly assigning or referencing. I had to look it over several times and have faith in you before I figured out how it is meant to work. do_something_with_line($line); } } Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: Email::Mime not able to send array in mesg body
On Wed, Feb 20, 2013 at 10:41:51AM -0800, 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. I tried sending reference to @email_body, but It simply printed array_ref in body. open(my $tmp_fh,,html_file) or die error: $!; I will assume that html_file is supposed to be a bareword. :-/ @email_body = $tmp_fh; close $tmp_fh; See also File::Slurp, which would eliminate this problem with less code than open, , close, and join: use File::Slurp qw/slurp/; my $email_body = slurp(html_file); my $message = Email::MIME-create( header_str = [ From = 'jo...@apple.com', To = 'jac...@banana.com', Subject = Please see my test email, ], attributes = { encoding = 'quoted-printable', charset = 'ISO-8859-1', content_type = 'text/html' }, body_str = @email_body, body_str = $email_body, ); sendmail($message); Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature
Re: .profile not being read
On Tue, Feb 19, 2013 at 07:29:49PM -0600, Gerard Walschap wrote: I see. There is a .bashrc file in my home directory with the line - source /Users/gerard/perl5/perlbrew/etc/bashrc - So I guess installing perl did override my .profile, although I'm not sure how. The source man page redirects to built in. I'm not quite happy with the way perl installs itself, but at least now I know how to remedy things. Thanks again, I don't know about on Mac OS X, but in Linux I always have to add that line myself. Perlbrew tells me to, but it's up to me to add that to bashrc. source is a built-in shell command. That is, it's built into bash itself, and is not a system command. man bash You should be able to jump to the documentation with this if your man page is anything like mine: / {7}source (AKA search for 7 spaces followed by the text 'source') A built-in alias for source is . (dot), so you will sometimes see that instead: . ~/path/to/file Perlbrew works by installing a local Perl for you and setting up your environment to use it by default by overriding PATH and a few Perl specific vars. It's actually a very beautiful system. It allows your system perl to be left untouched so all system managed software should be compatible and work, but it allows you to run any number of other perl versions side-by-side reliably and with little effort on your part. Checking the man page for my version of bash I am told that $HOME/.profile is never sourced. That may be why it isn't working for you. Just use $HOME/.bash_profile and be done with it. :) Or, if you insist on using .profile for some reason, you could source that from .bash_profile yourself: # .bash_profile: source .profile Results may vary from UNIX-like to UNIX-like, but not by much. Regards, -- Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org Castopulence Software https://www.castopulence.org/ Blog http://www.bamccaig.com/ perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' signature.asc Description: Digital signature