Hash refs and printing to FD

Discussion in 'Perl Misc' started by January Weiner, Apr 16, 2009.

  1. Hi,

    I want to print to a file descriptor that sits in a hash ref:

    my %hash ;
    my $hash_ref = \%hash ;
    open( $hash{fd}, ">test" ) or die "Can't write to file test: $!\n" ;

    print $hash_ref->{fd} "Test\n" ;

    This throws the following error:
    syntax error at test.pl line 12, near "} "Test\n""

    Why is that so? And what should I do, except for the obvious code below?

    my $fd = $hash_ref->{fd} ;
    print $fd "Test\n" ;

    j.
     
    January Weiner, Apr 16, 2009
    #1
    1. Advertising

  2. January Weiner <> wrote in news:gs71l8$kum$1
    @sagnix.uni-muenster.de:

    > Hi,
    >
    > I want to print to a file descriptor that sits in a hash ref:
    >
    > my %hash ;
    > my $hash_ref = \%hash ;
    > open( $hash{fd}, ">test" ) or die "Can't write to file test: $!\n" ;
    >
    > print $hash_ref->{fd} "Test\n" ;
    >
    > This throws the following error:
    > syntax error at test.pl line 12, near "} "Test\n""
    >
    > Why is that so?


    perldoc -f print

    Note that if you're storing FILEHANDLEs in an array, or if
    you're using any other expression more complex than a scalar
    variable to retrieve it, you will have to use a block returning
    the filehandle value instead:

    > And what should I do


    Follow the solution given in the documentation.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Apr 16, 2009
    #2
    1. Advertising

  3. On 2009-04-16, A. Sinan Unur <> wrote:
    > Follow the solution given in the documentation.


    Thanks, Sinan!

    I was looking for something in the perlre manual...

    j.
     
    January Weiner, Apr 16, 2009
    #3
  4. January Weiner

    Tim McDaniel Guest

    In article <gs7657$m39$-muenster.de>,
    January Weiner <> wrote:
    >On 2009-04-16, A. Sinan Unur <> wrote:
    >> Follow the solution given in the documentation.

    >
    >Thanks, Sinan!
    >
    >I was looking for something in the perlre manual...


    For anyone else who was puzzled about what the regular expressions
    manual (perlre) had to do with it, he meant "perlref".

    --
    Tim McDaniel,
     
    Tim McDaniel, Apr 16, 2009
    #4
  5. January Weiner <> wrote:
    > On 2009-04-16, A. Sinan Unur <> wrote:
    >> Follow the solution given in the documentation.

    >
    > Thanks, Sinan!
    >
    > I was looking for something in the perlre manual...



    s/perlre/perlref/

    ??


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Apr 16, 2009
    #5
  6. On 2009-04-16, Tim McDaniel <> wrote:
    > For anyone else who was puzzled about what the regular expressions
    > manual (perlre) had to do with it, he meant "perlref".


    :))))

    j.
     
    January Weiner, Apr 21, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Nagle
    Replies:
    3
    Views:
    312
    John Nagle
    Feb 25, 2007
  2. rp
    Replies:
    1
    Views:
    594
    red floyd
    Nov 10, 2011
  3. rocknmetal20

    Question on refs inside hash

    rocknmetal20, Apr 8, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    110
    Brian McCauley
    Apr 8, 2004
  4. Tony N.

    Printing an array of hash refs

    Tony N., Sep 22, 2004, in forum: Perl Misc
    Replies:
    11
    Views:
    224
    Tony N.
    Sep 24, 2004
  5. Alfred Z. Newmane
    Replies:
    6
    Views:
    128
    Mark Clements
    May 2, 2005
Loading...

Share This Page