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

  • SEARCH
  • Home
  • 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 3322766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:13:19+00:00 2026-05-17T23:13:19+00:00

use strict; my @array=(‘f1′,’f2′,’f3′); my $dir =’\tmp’; foreach (@array) { my $FH = $_;

  • 0
use strict;
my @array=('f1','f2','f3');
my $dir ='\tmp';
foreach (@array) {
  my $FH = $_;
  open ("$FH", ">$dir/${FH}.txt") or die $!;
}

foreach (@array) {
  my $FH = $_;
  close($FH);
}

i got "Can't use string ("f1") as a symbol ref while "strict refs" in use at bbb.pl line 6." error . What is the isuse ?

  • 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-05-17T23:13:19+00:00Added an answer on May 17, 2026 at 11:13 pm

    First: 2 arg open is bad, 3 arg open is better.

    open( .. , ">", "$dir/${FN}.txt")   
    

    second, what on earth are you doing with open(“$FH” ..

    argument 1 to open is supposed to be an actual filehandle of sorts which can be connected to a datastream. passing it a string will not work.

    INSANE:  open( "Hello world", .... )  # how can we open hello world, its not a file handle
    WORKS:   open( *FH,.... )  # but don't do this, globs are package-globals and pesky
    BEST:    open( my $fh, .... ) # and they close themself when $fh goes out of scope! 
    

    third

    foreach my $filename ( @ARRAY ){ 
    }
    

    Forth:

    dir = \tmp ? are you sure? I think you meant /tmp , \tmp is something different altogether.

    Fifth:

    use warnings;
    

    using strict is good, but you should use warnings too.

    Sixth: Use names for variables that are explanatory, we know @ is an array @array is not more helpful.

    ALL TOGETHER

    use strict;
    use warnings;
    
    my @filenames=('f1','f2','f3');
    my @filehandles = ();
    my $dir ='/tmp';
    foreach my $filename (@filenames) {
       open (my $fh,'>', "${dir}/${filename}.txt") or die $!;
       push @filehandles, $fh;
    }
    # some code here, ie: 
    foreach my $filehandle ( @filehandles ) { 
       print {$filehandle}  "Hello world!";
    }
    # and then were done, cleanup time
    foreach my $filehandle ( @filehandles ){ 
       close $filehandle or warn "Closing a filehandle didn't work, $!";
    }
    

    Alternatively, depending on what you were trying to do, this may have been better code:

    use strict;
    use warnings;
    
    my @filenames=('f1','f2','f3');
    my $dir ='/tmp';
    foreach my $filename (@filenames) {
       open (my $fh,'>', "${dir}/${filename}.txt") or die $!;
       print {$fh}  "Hello world!";
    }
    

    I don’t explicitly close $fh, because its not needed, as soon as $fh goes out of scope ( at the end of the block in this case ) it is closed automatically.

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

Sidebar

Related Questions

#!/usr/bin/perl use strict; use warnings; my @array = qw[a b c]; foreach my($a,$b,$c) (@array)
I'm puzzled with this test script: #!perl use strict; use warnings; use encoding 'utf8';
Consider the following code and its output: Code #!/usr/bin/perl -w use strict; use Data::Dumper;
In XHTML Strict, it seems that you're not allowed to use the <u> tag
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Given this code: #!/usr/bin/perl -w use strict; use warnings; sub foo { return wantarray
activePerl 5.8 based #!C:\Perl\bin\perl.exe use strict; use warnings; # declare a new hash my
I ran this test script: use strict; use warnings; use Test::More tests => 3;
I wrote an small script to use split() as this, use strict; use warnings;
In this script #!/usr/bin/perl -w use strict; my @ar = (1,2,10,3,5); @ar = sort

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.