Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Can perl do...? by nashdj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-19 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found