Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Printing to a File from Sub

by suaveant (Parson)
on Aug 27, 2010 at 14:56 UTC ( #857700=note: print w/replies, xml ) Need Help??

in reply to Printing to a File from Sub

Of course, if your code is OO you could also store the filehandle in the object to prevent the need for passing it around, even better create a method to retrieve the fh that created it on the first request. Perl filehandles when create as Corion exhibited can be passed around like any scalar, pretty much. Or you could even create a closure to handle all prints:
{ my $fh; open($fh,'>',$file); sub print_to_fh { print $fh @_; } }
As always, TIMTOWTDI :)

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re^2: Printing to a File from Sub
by PyrexKidd (Monk) on Aug 27, 2010 at 22:56 UTC

    here is code I am trying to use:

    #!/usr/bin/perl use strict; use warnings; open my $fhout, '>', "./test_out.file" || die "$!"; &test_print($fhout); sub test_print{ my $fhout = @_; print {$fhout} "this is a test\n"; } close $fhout;

    and here is the error message I keep getting:

    ~$ perl Can't use string ("1") as a symbol ref while "strict refs" in use at t line 11.
    What is going on here? am I just missing something? Thanks for the assist.
      You should either use
      open my $fhout, '>', "./test_out.file" or die "$!";
      open (my $fhout, '>', "./test_out.file") || die "$!";
      . The code you posted is parsed as
      open (my $fhout, '>', ("./test_out.file" || die "$!"));
      which is definitely not what you want.

      To avoid the error you reported, though, use

      my ($fhout) = @_;
      my $fhout = shift;
      Also note that the ampersand sign & is not needed when calling a sub (see perlsub).

      Putting the filehandle in parens told Perl that it is the argument to print, and so you wanted to print it to STDOUT - which is not what you wanted. You wanted to say
      print $fhout "this is a test\n";
      It should also be noted that the variable $fhout is actually still in scope in your subroutine, so you technically could just use it. It is, however, better practice to pass it in.

      Edit: choroba is correct as s/he notes below. I did mistake the curlies for parens.

        Yes, those aren't parens, they are squiggle brackets, easy mistake to make with the right font and font size.

                        - Ant
                        - Some of my best work - (1 2 3)

        Not true. The problem is $fhout contains the number of elements in @_, not the filehandle. The syntax print {$fh} "out" is even recommended in PBP.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://857700]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2023-12-04 23:42 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (25 votes). Check out past polls.