in reply to Re: foreach loop and creating files with "$_"
in thread foreach loop and creating files with "$_"

a lot of people find $_ ugly and even obfuscatory.

Possibly true, but then they are people who don’t know and use Perl! Perl is, by design, an atypical language, so its idioms and byways are often unintuitive at first, but unfamiliar should never be mistaken for ugly. As apotheon notes, people who find Perlish idioms ugly sometimes avoid Perl itself for that reason. IMHO that is not necessarily a bad thing.

I concur. And I'll submit that $_ is actually quite intuitive -- it's just other programming languages that are unintuitive, unfamiliar and (arguably) unnatural in this regard.

For me, $_ is simply the current topic that's being talked about. Like in natural languages, sometimes you leave it out entirely, since it's clear from the context what's being meant; sometimes you merely say "this" or "it" (which is how I read $_). Perl doesn't go quite as far as natural languages where you can have several of these at once, but on the other hand, unlike natural languages, Perl cannot afford to be ambiguous ("Bill met Bob. They talked, and he gave him back the book" is fine; the same thing in Perl wouldn't be).

Still, having one "topic" is better than having none, as most programming languages do. Natural languages are intuitive, after all, and we're all familiar with them.

  • Comment on Re^2: foreach loop and creating files with "$_"

Replies are listed 'Best First'.
Re^3: foreach loop and creating files with "$_"
by xiaoyafeng (Deacon) on Jul 22, 2014 at 09:26 UTC

    For me, $_ is simply the current topic that's being talked about.

    well, I think it is the problem. how about 2 topics happen at the same time? actually it's the biggest problem about $_ . think a topic you want to talk is the another topic (but happen at the same time).




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

      That is simply one of Perl's restrictions. You can talk about several things at once, but you will have to name them explicity, rather than referring to all of as "this" and "that". At least you get one such pronoun, though; that's one more than in pretty much every other language out there.

      Given that programs aren't actually conversations, I don't think it's a terribly bothersome restriction either. In practice, programs more closely resemble instructions, recipes, that sort of thing. There's usually one thing you're working on at any given time; often it's referred to as "it", and (in recipes in particular), it's also often not explicitely referred to at all ("cook until tender" etc).

      I think the parallel to $_ is obvious.