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

Hello james28909,

Your use of $_ to generate the filenames “temp1”, “temp2”, ... “temp5” looks fine to me. There is, in general, nothing wrong with using $_. On the contrary, $_ is one of Perl’s strengths, because it facilitates the writing of concise, elegant code.

In the article you reference, apotheon recommends this rule of thumb for $_: If you have to use it explicitly, use something else instead. So far as I can see (I’ve only skimmed the article), the only justification given for this opinion is that:

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.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: foreach loop and creating files with "$_"
by GrandFather (Saint) on Jul 22, 2014 at 05:24 UTC

    When using $_ keep in mind that it may change when you're not expecting it to. For example, if a sub gets called between setting $_ and using it, there's a fair chance that $_ will have changed. apotheon's rule of thumb is a good defence against that. Using a well named variable also has the huge advantage that it conveys intent which makes code more robust, easier to understand and easier to maintain.

    In the OP's code there is pretty much no chance that $_ is going to change unexpectedly as it stands, but for clarity a loop variable helps:

    for my $fileIdx (1 .. 5) { my $fileName = "$temp$fileIdx"; open $file, '>', fileName or die "Failed to create fileName: $!\n +"; ... }
    Perl is the programming world's equivalent of English
Re^2: foreach loop and creating files with "$_"
by AppleFritter (Vicar) on Jul 22, 2014 at 07:45 UTC

    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.

      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.

Re^2: foreach loop and creating files with "$_"
by Anonymous Monk on Jul 22, 2014 at 05:07 UTC
    a lot of people find $_ ugly and even obfuscatory... Possibly true
    It's about as true as the statement "Chinese language is ugly and obfuscatory". I mean, a lot of people find things like "舍阿奈比山是突尼西亞最高峰" completely unreadable... right?
Re^2: foreach loop and creating files with "$_"
by james28909 (Deacon) on Jul 22, 2014 at 03:50 UTC
    actually perl's wild looking code is what brought me to perl, i like its hacky script look lol