in reply to Can perl do...?

1) Like you say, the substitution operator doesn't do what you want; it's only for real variables. This is because it is an operator that needs an lvalue to modify. You can't use s/// with static strings for the same reason you can't do "'x'++;" or "'x'=3;". If you don't want to create a new variable, the fastest way is to assign your static or interpolated string to $_ and then use the substitution operator; then you can even drop the binding operator =~.

2) Sorry, you have to type the five extra characters "$_ = " :-P. The insanity resulting from putting the result of every statement into $_ would far outweigh the small convenience. You'd always have to be on the lookout for what blew away $_ (even more so than now); I suspect it would make the variable much less useful. Also, explicitly putting in the assignment makes the code significantly clearer; even if you could take it out, you probably shouldn't.

3)This is what numerical indices are for. Sorry, but you can't throw out all your C habits ;-). Just have an integer variable (say, $i) and then when you need to reference the previous value use $_[$i-1]. For (which is a synonym for foreach) does require at least one parameter; if you want an infinite loop use for(;;){...};. On another note, @_ doesn't have the same "temporary" role that $_ does. @_ is for passing arguments to subs, so once again it's much more clear and barely more difficult to just type it. On an even less related note, while loops DO have funky semantics when you use "<>" as the sole argument. Namely, it will take elements from @ARGV, assume they are filenames, get data from those files, then read from STDIN.

Update: foobah's comments below are all correct; I fixed the first but check the others for clarifications.

Replies are listed 'Best First'.
RE: Re: Can perl do...?
by nashdj (Friar) on Jun 04, 2000 at 13:56 UTC
    Don't know if I could have asked for a better answer. Everything seems so clear the way you have explained it.
RE: Re: Can perl do...?
by foobah (Novice) on Jun 04, 2000 at 23:42 UTC
    Just a few notes; instead of @_$[i-1] I think you meant $_[$i - 1] Also, although it's deprecated, split in void context splits into @_, so @_ can sometimes have a "temporary" role. Lastly, while (<>) { ... } doesn't assume that @ARGV contains only filenames; @ARGV may also contain pipe commands.
RE: Re: Can perl do...?
by nashdj (Friar) on Jun 05, 2000 at 15:56 UTC
    There is still one thing beyond me. What about.. $foo; What purpose does it serve being able to do that. As I said before all I can think it would be good for is as a return for a sub. (I really need to get some books dont I)
    I'm just curious :)
      That doesn't do anything at all; you're introducing a value into a null context. That is, your statement has no effect whatsoever since there are no side effects. For instance, try the one-liner perl -we "$_;"Using the -w flag will let you know when your statements aren't doing anything.
        Ok, so if it does nothing, then why not in a future perl release allow that to assign the value to $_. Sure it might sound completely useless, but if it already is useless. I would find something like that very handy. As for making sense, it seems to do that too..
        It does one thing nashdj alluded to. As the last statement in a block, the resultant value of the block is the value in the variable.

        For example, in the Everything source they'll occasionally have a subroutine that does something like:

        sub do_thisorthat { my ($text, $op) = shift; my $code = get_op($op); $code->$text; # manipulate text $text; }