Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:13:26+00:00 2026-06-18T06:13:26+00:00

I would like to be able to have a function that prints to a

  • 0

I would like to be able to have a function that prints to a file but that does not open the file — instead an already open file handle should be passed to it. That way the file opening and closing only happen once in the calling block of code.

I tried:

sub teeOutput
{
    my $str = $_[0];
    my $hndl = $_[1];

    #print to file
    print $hndl $str;
    #print to STDOUT
    print $str;
}

and then when calling that

open(RPTHANDLE, ">", $rptFilePath) || die("Could not open file ".$rptFilePath);

&teeOutput('blahblah', RPTHANDLE);
&teeOutput('xyz', RPTHANDLE);

close(RPTHANDLE);

but that didn’t work.

Any idea how to accomplish this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:13:27+00:00Added an answer on June 18, 2026 at 6:13 am

    First, stop using global variables for file handles.

    open(my $RPTHANDLE, ">", $rptFilePath)
       or die("Could not open file $rptFilePath: $!\n");
    

    Then… Well, there is no “then”.

    teeOutput($RPTHANDLE, 'blahblah');
    teeOutput($RPTHANDLE, 'xyz');
    close($RPTHANDLE);
    

    Notes:

    • I reversed the argument to teeOutput to something saner.
    • I removed the directive (&) to override teeOutput‘s prototype. teeOutput doesn’t even have one.

    (But if you have to deal with globs, use teeOutput(\*STDERR, ...);.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string property that I would like to be able to force
I have an application A that I would like to be able to invoke
I have five activities/screens that I would like to be able to swipe between,
I have two arrays of values that I would like to combine - but
I would like to be able to have hovering over the image invoke the
I would like to be able to go: sed s/^\(\w+\)$/leftside\1rightside/ and have the group
I have a table and I would like to be able to set up
I have a TcxScheduler where I would like to be able to get the
I have an NSArray of NSDictionary objects which I would like to be able
I have several ASP.NET sites in IIS7 and would like to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.