mikejones has asked for the wisdom of the Perl Monks concerning the following question:

Is there a better way to write:
chomp ( my $dginput = lc(<>) ) ; chomp ( my $volinput = lc(<>) ) ; ## chomp ( my ( $dginput ,$volinput ) = lc(<>) ) ; ## the above chomp with the 2 vars does not work
thank you

Replies are listed 'Best First'.
Re: diamond operator multiple input
by chromatic (Archbishop) on Jan 21, 2009 at 22:06 UTC

    You should be more specific about what "does not work" means. What happens? What did you expect to happen?

    My guess (without running code) is that lc forces scalar context on its single argument. You want a list, so this will work better:

    my ($dginput, $volinput) = map { chomp; lc } <>;

    Consider that because Perl is not a lazy language, it'll read everything from STDIN and throw away all but the first two elements. That could be a lot of work.

      Doing map { chomp; lc } <>; generates a "Useless use of lc in void context ..." error, lc $_ seems to solve that. You could avoid reading the whole file by explicitly doing a scalar <> twice.

      $ cat zzzz AAAA BBBB CCCC DDDD EEEE $ perl -e ' > ( $dg, $vo ) = map { map { chomp; lc $_ } scalar <> } 1 .. 2; > print qq{$dg, $vo\n}; > print q{-} x 15, qq{\n}; > print for <>;' zzzz aaaa, bbbb --------------- CCCC DDDD EEEE $

      I hope this is of interest.

      Cheers,

      JohnGG

      Update: As ikegami points out, the warning I saw seems to be a figment of my imagination. Please ignore that part of the post.

        I cancan't replicate your warning.
        >perl -we"my ($dginput, $volinput) = map { chomp; lc } <>;" a b c ^Z

        Not even if I remove the assignment.

        >perl -we"map { chomp; lc } <>;" a b c ^Z

        Tested with 5.6, 5.8 and 5.10

      That will read until EOF, won't it?

        Exactly, just like every readline in list context except for a while loop.

Re: diamond operator multiple input
by GrandFather (Saint) on Jan 21, 2009 at 22:11 UTC

    Define 'better'. You may like:

    ($_ = lc <>), chomp for my ($dginput, $volinput);

    Perl's payment curve coincides with its learning curve.

      As there are two "things" I'd consider statements I'd rather use the usual form of for().

      for my ($dginput, $volinput) { $_ = lc <>; chomp; }
      I find it less confusing. This is not golf.

      Update: Whoops. That's what I get for not testing the code. No it would not work in 5.10 either. Forget everything I said, you'd have to write it like this:

      my ($dginput, $volinput); for ($dginput, $volinput) { $_ = lc <>; chomp; }
      Even if I fix the syntax error (by adding one more pair of braces) the variables to not stay declared outside the loop. So it might have been less confusing, but it was plain wrong. Sorry.

        Is that "for" loop syntax something that was introduced with 5.10? I tried that script with 5.8.8 and had two problems with it. First, the snippet as you posted it gave me this sort of error:
        $ perl -e 'use strict; for my ($d,$v){ $_=lc<>; chomp; } print "got $d and $v\n"' Missing $ on loop variable at -e line 1.
        I could get rid of that with an extra set of parens, but then there were scoping issues, because the two variables were limited to the scope of the "for" loop:
        $ perl -e 'use strict; for (my($d,$v)){ $_=lc<>; chomp; } print "got $d and $v\n"' Global symbol "$d" requires explicit package name at -e line 2. Global symbol "$v" requires explicit package name at -e line 2. Execution of -e aborted due to compilation errors.
        Interestingly, neither of those problems seems to affect GrandFather's snippet -- the following not only compiles, but when you feed it data, it does what it's supposed to do:
        $ perl -e 'use strict; ($_=lc <>), chomp for my ($d,$v); print "got $d and $v\n"'
        Again, I'm only looking at 5.8.8 -- it would be good to know whether 5.10 has different behavior in this regard.
Re: diamond operator multiple input
by jdporter (Paladin) on Jan 22, 2009 at 01:24 UTC

    You need two scalars in a list on the RHS:

    chomp( my( $dginput, $volinput ) = ( lc(<>), lc(<>) ) );
    Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.