Public Scratchpad | Download, Select Code To D/L |
Last update: Feb 06, 2012 at 05:00 UTC
Comments on the tutorial itself:
If we are going to convert this meditation directly into a tutorial, rather than having you repost:
All that being said - I'm not sure this is Tutorials-quality, in the sense that you're really just showing a very specific technique for accomplishing a very arcane task. I'd say it's a better fit for the Cool Uses for Perl section, frankly. It's certainly very cool; I don't mean to take that away from it at all.
As for your other -- RFC Tutorial - Deleting Excel Rows, Columns and Sheets -- It looks really good to me. I'd post it in Cool Uses for Perl ASAP! (Or in Meditations. If you post it in CUFP, modify the title appropriately.)
my $total_population = $generations{$_}; $total_population += $generations{$_ - 1} if $generations{$_ - 1}; $total_population += $generations{$_ - 2} if $generations{$_ - 2};
dim sh, f, i, v set sh = CreateObject("Shell.Application") set f = sh.NameSpace(17) 'My Computer' for each i in f.Items ' MsgBox i.Name & "=" & i.Type if (i.Type = "CD Drive") then for each v in i.Verbs if (v.Name = "E&ject") then v.DoIt() end if next end if next
use List::Util qw( shuffle ); use strict; use warnings; my @upper = 'A'..'Z'; my @lower = 'a'..'z'; $_ = "What That April With His Shoures Soote The Drought Of Marche Hath Perced To The Roote "; $" = ''; eval "y/@upper@lower/@{[shuffle @upper]}@{[shuffle @lower]}/"; print
It alleges properly to parse quoted CSV fields with embedded commas and double-quotes, but my test of this utterly fails.
Test:sub parse_csv1 { my $text = shift; # record containing comma-separated values my @fields = ( ); while ($text =~ m{ # Either some non-quote/non-comma text: ( [^"',] + ) # ...or... | # ...a double-quoted field: (with "" allowed inside) " # field's opening quote; don't save this ( now a field is either (?: [^"] # non-quotes or | "" # adjacent quote pairs ) * # any number ) " # field's closing quote; unsaved }gx) { if (defined $1) { $field = $1; } else { ($field = $2) =~ s/""/"/g; } push @fields, $field; } return @fields; }
Output:my @f = parse_csv1('first,"second","thi,rd","fou""rth"'); print "$_\n" for @f;
WTF?first second thi rd fou rth
Solution: The problem is on line 15:
Because the /x switch is in effect, the programmer can, and did, embed comments within the regex. But there is no way for the compiler to check when a # has been accidentally omitted!( now a field is either
Change the line to:
IIRC, credit for finding this bug goes to ikegami, and possibly ambrus as well. Thanks, guys!( # now a field is either
/* a basic one: */ div.header { background-color: #EEE; padding: 3px; border-bottom: 1px +solid blue; } ul.replies:before { content: url(http://perlmonks.org/images/bubbles.p +ng) } ul.replies { list-style-type: none; padding-left: 40px; } li.reply { border: 1px solid blue; } /* a fun one: */ div.header { background-color: #DFB; padding: 3px; } ul.replies:before { content: url(http://perlmonks.org/images/bubbles.p +ng) } ul.replies { border-left: 2px solid red; padding: 3px; list-style-type +: none; padding-left: 20px; } li.reply { border-top: 2px solid blue; padding: 3px; } /* both will need something like this: */ div.comment-on { text-align: center; font-size: larger; background-col +or: #DFB; }
<jcw> Jebus, I can't type. <jcw> Perhaps a typing exercise would help. *<* zdog has been kicked off channel #perlmonks by jcw (The quick brow +n fox kicked over the whining dog) <jcw> Yea, that worked. *<* Signoff: castaway (Ping timeout) *<* Signoff: theorbtwo (Ping timeout) <jcw> 3 for the price of 1!
{ package Foo; use overload '""' => \&as_string; sub as_string { "Foo!" } } $_ = bless {}, 'Foo'; /(.*)/ and print "$1\n"; # does this print "Foo!" or "Foo=HASH(0xdeaded)"?
Source: Mapping of Unicode characters
Character Name | Code point (hex) | Character between two Zeroes |
---|---|---|
Word Joiner | ⁠ | 00 |
Zero-width joiner | ‍ | 00 |
Zero-width non-joiner | ‌ | 00 |
Zero-width space | ​ | 00 |
Zero-width no-break space |  | 00 |
Combining Grapheme Joiner | ͏ | 0͏0 |
Invisible Separator | ⁣ | 00 |
Invisible Times | ⁢ | 00 |
Function Application | ⁡ | 00 |
Space |   | 0 0 |
En Quad |   | 0 0 |
Em Quad |   | 0 0 |
En Space |   | 0 0 |
Em Space |   | 0 0 |
Three-Per-Em Space |   | 0 0 |
Four-Per-Em Space |   | 0 0 |
Six-Per-Em Space |   | 0 0 |
Figure Space |   | 0 0 |
Punctuation Space |   | 0 0 |
Thin Space |   | 0 0 |
Hair Space |   | 0 0 |
Left-to-Right Mark | ‎ | 00 |
Mathematical Space |   | 0 0 |
Soft Hyphen | ­ | 00 |
Non-breaking Hyphen | ‑ | 0‑0 |
No-break Space |   | 0 0 |
Narrow No-break Space |   | 0 0 |
Zero-width space | ​ | 00 |
Line separator | 
 | 0 0 |
Paragraph separator | 
 | 0 0 |
horizontal tab | 	 | 0 0 |
linefeed | 
 | 0 0 |
carriage return | 
 | 0 0 |
newline | … | 0 0 |
:set shiftwidth=2 :set foldcolumn=8 (not important) :set foldmethod=indent :1,$foldopen! (to open all folds)
use Tk::FileSelect; use Data::Dumper; use strict; use warnings; my $mw = new MainWindow; my $fs = $mw->FileSelect( -directory => '.' ); $fs->configure(-verify => ['-d'] ); print "$_: ",$fs->Subwidget($_),"\n" for 'dir_entry', # LabEntry 'file_entry', # LabEntry 'dir_list', # ScrlListbox 'file_list', # ScrlListbox 'dialog' # Dialog ; my %ch; $ch{$_} = $_ for $fs->children; delete $ch{$_} for $fs->Subwidget; my( $fr ) = @ch{ grep /Frame/, keys %ch }; my $b = $fr->Button( -text => "New Dir", -command => \&new_dir, ); $b->pack( -side => 'top', -fill => 'x', -expand => 1, ); my $file = $fs->Show; die "> $file\n"; sub new_dir { $fs->Subwidget('dir_entry')->validate; # note that this is the current "accepted" directory name from the # dir entry. It may not be the name of an existing directory! # but it will NOT have a trailing /* even if displayed. # it's possible to wangle the direntry into a state where it # has two slashes before the * and/or letters between # the / and * for example: /x//* /x/y* /x//y* # In all cases, the slash (both slashes, if present) up # through the star are excluded from the value of -directory. my $dir = $fs->cget('-directory'); print "Make a new dir under $dir !\n"; my $new_dirname = Prompt("Enter new folder name:"); # wave hands my $new_dir = "$dir/$new_dirname"; mkdir $new_dir; $fs->Accept_dir($new_dir); }
The sad, unfortunate fact is that not all of the registered users of this site are playing with a full deck, scruples-wise. In particular, two monks — jesuashok and madtoperl — have routinely posted material by other authors stolen from other sites; in each case, proper attribution of authorship was studiously avoided; and in many cases the plagiarist explicitly claimed the work was original.
I'm not trying to single out these folks. They are simply a couple known, currently active plagiarists. If you discover any other recent or new plagiarisms, please notify me, liverpole, planetscape, chargrill, or shmem (the Plagiarism Posse).
Keep in mind that usernames are not important; people can always create new user accounts to hide behind. Rather, we want to educate any offenders — the people behind the usernames — that plagiarism is not cool and not acceptable.
Advice to moderators:
Advice to monks:
Relevant PMD: How should Perlmonks deal with Plagiarism?
post | Plagiarized from |
---|---|
B_egi_N oF Wzdm | |
If my boss ..... | |
Propose to a Girl | web |
Prayer for new joinees in perl monks | |
I love perl | |
Confess |
post | Plagiarized from |
---|---|
Size varies using TK in different versions? | clp.tk |
why Crashing windows API using perl?? | perl.beginners |
My Friend and Mine! | Sharon Hopkins (quoted in a reply) |
Reaped: order a machine to have sex, love and death? | Sharon Hopkins (quoted in a reply) |
Options in Life! | Spidering Hacks |
The day it was dark!!! | Peter Eckenrod |
Student in Home!!! | Yanick |
In user code:package SNMPfu; # pass an object which is either a Net::SNMP object # (as returned by Net::SNMP->session) or an SNMP::Session # object (as in the SNMP module/dist). sub wrap { my $obj = shift; bless $obj, __PACKAGE__ . '::' . ref $obj; # rebless } package SNMPfu::Net::SNMP; # wrapper for Net::SNMP use base 'Net::SNMP'; sub get # wraps Net::SNMP::get_request { my( $self, $var_id ) = @_; my $r = $self->get_request( -varbindlist => [$var_id] ); defined $r or die $self->error; $r->{$var_id} } package SNMPfu::SNMP::Session; # wrapper for SNMP::Session use base 'SNMP::Session'; sub get # wraps SNMP::Session::get { my( $self, $var_id ) = @_; $self->get($var_id) # but I'm not sure this correct. Illustration +only. } 1;
By reblessing, we divert all method calls on the object into one of the wrapper classes we've written. By making those wrapper classes inherit from the original class of the object, the user can call "native" methods of the object if she chooses. If we wish to disallow that, just take out the 'use base' lines.my $sess = Net::SNMP->session( ... ); # or: my $sess = SNMP::Session->new( ... ); SNMPfu::wrap( $sess ); my $val = $sess->get('sysDescr.0');
my $sh = new Win32::OLE 'Shell.Application' or die; my $folder = $sh->NameSpace( $directory_path ) or die; my $item = $folder->ParseName( $unqualified_filename ) or die; $item->InvokeVerb('Edit');
cscript /nologo cdrom_drives.js
/* * cdrom_drives.js * by jdporter */ var sh = WScript.CreateObject('Shell.Application'); var a = new Array(); var i; for ( i = 65; i < 75; i++ ) { // test the first 10 drive letters var p = String.fromCharCode(i,58,92); // letter + colon + backslas +h try { var fi = sh.NameSpace(p).Self; // will throw if bad path if ( fi.IsFileSystem && fi.Type == 'Compact Disc' ) a.push(p); } catch(e) { } } WScript.Echo(a.join("\n"));
use LWP::Simple; use XML::Simple; use strict; my( $username, $password ); print "Username: "; chomp( $username = <> ); print "Password: "; chomp( $password = <> ); $username && $password or die "Abort.\n"; my $t = XMLin get "http://perlmonks.org/?node_id=32704;op=login;user=$ +username;passwd=$password;ticker=yes"; my $total; my $n; while ( my( $id, $hr ) = each %{ $t->{'NODE'} } ) { $total += $hr->{'reputation'}; $n++; } print "$total rep in $n nodes\n";
use LWP::Simple; use strict; *a = \"http://perlmonks.org/?node_id"; printf qq m%2d %7d %s\nm,m leve.="(\d+)"l,m exp="(\d+)"e,m foruser="([^"]+)"ffor map get qq m$a=16046;for_userid=$_m,map m user_id="(\d+)"ug,get qq m$a=15851m;
This snippet can be put anyplace on PerlMonks where HTML forms are allowed, including your homenode or in your Free Nodelet.
You must replace "jdporter" with your username, and 170442 with your user id (which is your homenode's node_id).
<form method="post" action="?" enctype="application/x-www-form-urlenco +ded"> <input type="hidden" name="node_id" value="170442"> <input type="hidden" name="op" value="message"> <input type="hidden" name="replyto" value="user"> <input type="hidden" name="sendto" value="jdporter"> <input type="text" name="replytotext" size=60 maxlength=255> <input type="hidden" name="sexisgood" value="submit"> </form>
One of the patterns I see frequently goes like this:
Now, of course, you may -- and I fully expect you to -- say I'm mistaken about any or all of the above. But you have to ask yourself: Why do people so routinely misunderstand me? If you truly believe people aren't understanding your true opinion on something, I'd suggest (1) stating clearly and unambiguously -- and that means (among other things) without sarcasm, since that is so easily missed in this medium; and (2) maintaining your position with as much consistency as possible.
Help for User Settings
Aristotle
For Beginners: Basic Information Resources (or, Where to Look Things Up)
On finding the Perl Wisdom
ybiC
Nodelet types, uses, and (relative) usefulness
Welcome to the Monastery! Make yourself at home.
Spirit of the Monastery
Louis_Wu
Being a monk: why you'll love it, why you'll hate it
Site Comments.
PerlMonks for the Absolute Beginner
Forum or Knowledge-Base ?
A reflection or two on PM life
Perl $^O values for various operating systems
The Threading Dilemma
Are "PM Discussions" only to be technical?
About Perlmonks: How would you describe PM?
Why does PerlMonks work?
Where and how to start learning Perl
fast, flexible, stable sort
Keyword Nodelet / Tagging documentation
Re: Humorous Module Ideas
Real DJs Code Live (from Wired)
turnstep
PerlMonks as Ambassadors
The next step
How to get the most of your question from the monks
Help for new monks
New Monks
New Monks Info Page
broquaint
Add Entries to vroom's Book List
Re: Rethrowing with die $@ considered harmful (local $_ buggy)
Re: Name Space
Pumpkins are best used for...
Re^2: Unused accounts zombified
Re: How can I create an UTF8 encoded txt file contains strings like "aaaa"?
Conversion from UTF-8 to windows-1256 encoding
Re: Restrictions of the Anonymous Monk
Free Nodelet Settings
Making Perl Monks a better place for newbies (and others)
Ignored Users
Wow, stuff got inserted above, not below!
Replies are listed 'Best First'. | |
---|---|
jdporter's treehouse
by jdporter (Paladin) on Jul 20, 2005 at 22:01 UTC | |
by jdporter (Paladin) on Mar 24, 2008 at 18:34 UTC | |
by jdporter (Paladin) on Oct 11, 2005 at 15:33 UTC | |
by jdporter (Paladin) on Jun 20, 2006 at 14:41 UTC | |
by jdporter (Paladin) on Sep 01, 2005 at 02:28 UTC | |
by demerphq (Chancellor) on Sep 01, 2005 at 17:05 UTC | |
by jdporter (Paladin) on Mar 16, 2006 at 13:56 UTC | |
by jdporter (Paladin) on Jun 20, 2006 at 15:06 UTC | |
by jdporter (Paladin) on Dec 30, 2005 at 12:56 UTC | |
by jdporter (Paladin) on Jun 20, 2006 at 15:08 UTC | |
by jdporter (Paladin) on May 10, 2021 at 14:31 UTC | |
by jdporter (Paladin) on Jul 23, 2009 at 15:08 UTC | |
by jdporter (Paladin) on Jun 03, 2021 at 02:21 UTC | |
by jdporter (Paladin) on Aug 05, 2009 at 16:44 UTC |