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

I have a question. Is it okay to use "undef" as a placeholder to skip a variable when you're capturing return elements from a list?
use strict; use warnings; $| = 1; my (undef, $min, $hr, undef, $mon) = localtime; # Okay # my (undef, $min, $hr, , $mon) = localtime; # Okay but unexpecte +d/weird result # my ( , $min, $hr, , $mon) = localtime; # Syntax error print "\nMinutes = $min\nHours = $hr\nMonths = $mon\n\n";

I have noticed that it's also okay to just write blank, but when I try to substitute blank in place of the first undef, I get a syntax error. What would be the official or best practice to skip a variable in a list? I am asking this question, because what if they change Perl and in version 5.79 they introduce some new feature which means that "undef" as a placeholder will raise an error or warning in the code?

Replies are listed 'Best First'.
Re: skipping unwanted list items
by ikegami (Patriarch) on May 10, 2026 at 11:49 UTC

    Yes, and LanX provided the docs. You can also use a list slice (( LIST )[ LIST ]).

    my ( $y, $m, $d ) = ( localtime )[ 5, 4, 3 ]; ++$m; $year += 1900;
      This is really neat. I didn't know you could do this. Thank you!
Re: skipping unwanted list items
by LanX (Saint) on May 10, 2026 at 07:39 UTC

    > What would be the official or best practice to skip a variable in a list?

    $ perldoc -f undef

    ...

    my ($x, $y, undef, $z) = foo(); # Ignore third value returned

    $ perldoc perldata

    ...

    The list 1,,3 is a concatenation of two lists, 1, and 3, the first of which ends with that optional comma. 1,,3 is (1,),(3) is 1,3 (And similarly for 1,,,3 is (1,),(,),3 is 1,3 and so on.) Not that we'd advise you to use this obfuscation.

    ...

    you may assign to undef in a list. This is useful for throwing away some of the return values of a function:

    ($dev, $ino, undef, undef, $uid, $gid) = stat($file);

    As of Perl 5.22, you can also use (undef)x2 instead of undef, undef

    Update
    In short

    undef assignment is official, commas are mostly ignored inside a list.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      How to find documentation

      To check this yourself you can either use perldoc ° on the commandline like demonstrated, or use the excellent full text search in perldoc.perl.org (expand menu)

      NB: you can restrict your search to your beloved 5.8.X miniperl

      https://perldoc.perl.org/5.8.0 (adjust subversion)

      Last but not least you can use my keyword search for X<tags>

      Perldoc Keyword Search (update3)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      °) see also monastery markup [doc://perldoc], [doc://undef], [doc://perldata]

      Oh, okay. Thank you!