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

my $fh; my %x; open($fh, ...) $x{handle} = $fh print $x{handle} "Wow!\n";

I get the error "String found where operator expected" at the print. I can use a temporary variable as a workaround, but that should be unnecessary:

my $z = $x{handle} print $z "Wow!\n";

Perl hashes work just about everywhere else I've used them, so why don't they work with print?

Replies are listed 'Best First'.
Re: Nonsense error from Perl
by davido (Cardinal) on Oct 15, 2011 at 07:41 UTC

    The answer is as far away as the documentation for print:

    If you're storing handles in an array or hash, or in general whenever you're using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable to retrieve it, you will have to use a block returning the filehandle value instead, in which case the LIST may not be omitted:

    print { $files[$i] } "stuff\n"; print { $OK ? STDOUT : STDERR } "stuff\n";

    So having read that one could deduce that your statement needs to look like this:

    print { $x{handle} } "Wow!\n";

    Dave

Re: Nonsense error from Perl
by ikegami (Patriarch) on Oct 15, 2011 at 08:12 UTC
    Only one of these can work:
    print $handles{foo};
    print $strings{foo};

    The latter was picked to work. As such,

    print $x{y} "Wow!\n";
    is as much an error as
    $s = $x{y} "Wow!\n";

    If the file handle expression is non-trivial, you can't omit the curlies around the expression.

    print $fh "..."; print { $fh } "..."; print { $handles{foo} } "..."; print( $fh "..." ); print( { $fh } "..." ); print( { $handles{foo} } "..." );

    print

Re: Nonsense error from Perl
by jwkrahn (Abbot) on Oct 15, 2011 at 07:44 UTC

    If you read the documentation for print you will see that you have to put {} around a filehandle like that:

    print { $x{ handle } } "Wow!\n";
Re: Nonsense error from Perl
by Anonymous Monk on Oct 15, 2011 at 07:28 UTC

    You're missing a lot of semicolons. I get

    $ perl junk Scalar found where operator expected at junk line 5, near ") $x" (Missing semicolon on previous line?) syntax error at junk line 4, near ", ..." Execution of junk aborted due to compilation errors.

    But I'm using a very modern perl, 5.14.1

Re: Nonsense error from Perl
by Marshall (Canon) on Oct 15, 2011 at 12:20 UTC
    The simple answer is that some exceptions had to
    be made to print, in particular some exceptions had
    to be made to the syntax of HANDLE in the interest of simplicity over consistentcy.

    print $x{handle},"\n"; is valid to STDOUT
    print {$x{handle}} $x{handle},"\n"; FILEHANDLE $x{handle} with value $x{handle}

    I think the idea was to make the "normal", "usual" syntax as easy as possible, even if this caused some trouble in less common cases.

Re: Nonsense error from Perl
by Khen1950fx (Canon) on Oct 15, 2011 at 23:04 UTC
    This does what you want.
    #!/usr/bin/perl -l use strictures 1; use Data::Dumper::Concise; my $fh; my %x; $x{'handle'} = $fh; open $fh, '>-'; print Dumper($x{'handle'}), "Wow!"; close $fh;