in reply to Re: Can't find any documentation on < a b c > syntax for lists
in thread Can't find any documentation on < a b c > syntax for lists

<a b c> doesn't look for files. But <[abc]> and <*> would.

Replies are listed 'Best First'.
Re^3: Can't find any documentation on < a b c > syntax for lists
by Anonymous Monk on Feb 14, 2023 at 06:33 UTC
    Why are these different?
    perl -MO=Deparse,-p -e 'print "$_\n" for < a >;'
    use File::Glob ();
    print("$_\n") foreach (glob(' a '));
    

    perl -MO=Deparse,-p -e 'print "$_\n" for <a>;'
    print("$_\n") foreach (readline(a));
    

      From perlop (boldface added for emphasis):

      If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context. This distinction is determined on syntactic grounds alone. That means <$x> is always a readline() from an indirect handle, but <$hash{key}> is always a glob(). That's because $x is a simple scalar variable, but $hash{key} is not--it's a hash element. Even <$x > (note the extra space) is treated as glob("$x "), not readline($x).

      Hope that helps,

      Athanasius <°(((><contra mundum סתם עוד האקר של פרל,

      > Why are these different?

      > perl -MO=Deparse,-p -e 'print "$_\n" for < a >;'

      > perl -MO=Deparse,-p -e 'print "$_\n" for <a>;'

      a could be an old-style bare-word file-handle, but " a " can't

      think

      • open a, "<", "filename"
      instead of the common FH

      one of the reasons why ppl are encouraged to use lexical filehandles:

      • open my $fh, "<", "filename"

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

    • update: added code tags in citations, thanks hippo++

      From this StackOverflow answer,

      • <> means readline(ARGV)
      • <IDENTIFIER> means readline(IDENTIFIER)
      • <$IDENTIFIER> means readline($IDENTIFIER)
      • <...> (anything else) means glob(qq<...>)
          <...> (anything else) means glob(qq<...>)
        Anything? Except this:
        perl -MO=Deparse,-p -e 'print "$_\n" for <<>>'
        print("$_\n") foreach (<<>>);

        Perl waits for input:

        echo fubar | perl -e 'print "$_\n" for <<>>'
        fubar