I was sitting around with nothign to do today. So I figured I would just play around with perl, and play around I did.
But I came across a few things, that I'm not sure if you can do. Well, sure there are other ways of doing the same, but the way I wanted to do some things, seemed to make a lot of sense and was simpler.
Firstly,
$foo = ("this $foo" =~ s/a/b/);
Well that obviously wont work because for one thing it is trying to modify a static string, and secondly because what we are actually placing into $foo at the end is the number of substitutions.
But why cant we modify a static string, sure it is "static" but if during execution perl creates a (non existant) variable to store it in. Do I make any sense, probably not. :)
The second step, assigning the actual result back to $foo, is there a way to do that (no I mean, is there a perlvar to set which causes it to do what I want)?
Next, I often find myself assigning a value to $_. Simple to do "$_ = $foo;" but I see that too much. What I was thinking would be nice was simply...
$foo;
Which by itself seems to serve no purpose, other than being usefull in a sub as a return. Again, am I missing something, is there a shorthand "$_ =" already?
Ok and for my last wish :) (no no, two actually)
Occasionally I find myself in a for (well foreach rather) loop, and want to find the previous value of $_
for(@_)
{
$in .=' '.($t = $_ - $last);
$last = $_;
}
Which can be done like that, but.. What I had in mind, was treating $_ as a stack. Each time it takes on a new value the stack is pushed up. The previous value could be given the name $__ then $___ and so on, but that could get very messy :) (does perl have something similar already?)...
Finally, as you see above I was using a for(@_) loop. Pretty short to do, but again... :) Does a standalone "for" do anything? I thought it would be nice if for{} == for(@_){}.
So there you have it, am I crazy, is all this already here. If not are there reasons why, or do you think they should be.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.