1

From the below code I want to store the regular expression result variable contents (i.e $fh result into new file name ). How can I do it?

Is there any possible way to do get regex variable output (i.e $fh output) written in new file? Here is my code :

use strict;
use warnings;
open my $fh,'<',"open.txt" or die "$!";
while(<$fh>)
  {
    $fh=s/("\d+|\d+|")\s*/$1/g;
    print $fh;
  }
close $fh;

Error:

readline() on unopened filehandle 1 at fr.pl line 7.
close() on unopened filehandle 1 at fr.pl line 9.

Open.txt

1
2
3
radio
vendor
version114
version115

Output:

Note: $fh has regular expression values.Now those $fh values should be stored in new file and print the same contents as like below.

radio
vendor
version
version
15
  • 1
    Please edit your question and include example input and output. What do you want to do? Your regex is strange. You are capturing "\d+ or \d+ or " (one double quote). Then there is optional whitespace, and you replace all of that with the capture. Do you want $word to be the altered line, or the thing inside the capture group? Commented Jun 21, 2017 at 11:33
  • 2
    Please edit your question to fix the obvious syntax error (that the syntax highlighting is making very clear!) While you're there, you might consider fixing the indentation too. If you want a large number of strangers to read and understand your code, you can help them by making it as easy to follow as possible. Commented Jun 21, 2017 at 11:49
  • 1
    added my input and output part along with the error @DaveCross Commented Jun 21, 2017 at 12:11
  • 2
    You've asked more or less the same question as last time, and the problem is the same - you cannot 'overwrite' a file handle like that. Commented Jun 21, 2017 at 12:43
  • 1
    Imagine you have a box full of toys. What you want to do is replace one toy at a time. What you are doing is taking the first toy, changing it to some new toy, and then throwing away the whole box and only keeping the new toy. Commented Jun 21, 2017 at 13:07

1 Answer 1

2

I'm not entirely sure what you're trying to do. But I can see what is generating your errors. The problem is this line:

$fh=s/("\d+|\d+|")\s*/$1/g;

Before you execute this line of code, $fh contains the filehandle that you are reading data from. But with this line of code, you overwrite that value with the results from your substitution. That's either going to be the number of substitutions made (which is an integer) or an empty string (if the substitution fails). In either of those two cases, $fh will no longer contain your filehandle.

So at the end of the first iteration of the loop, $fh no longer contains a filehandle. So when you execute while (<$fh>) at the start of the second iteration, that will fail (because, as the error says, $fh is not an open filehandle). That read fails and the loop is skipped. The next line executed is the call the close(). And that also fails for exactly the same reason.

I'm not at all sure what you think that line of code is doing. But what it's actually doing is breaking your program :-)

Update: I suspect that reading (and, more importantly, understanding) the Perl FAQ How do I change, delete, or insert a line in a file, or append to the beginning of a file? will be a very profitable use of your time at this point. It looks like you don't understand how basic file manipulation works.

Sign up to request clarification or add additional context in comments.

5 Comments

The think is i want to remove all the digits from the file using that regular expression.After removing digits the output should be written in new file name. @Dave Cross
@salar33: Ok. And why do you think that overwriting your filehandle is a good way to achieve that?
Because i want to use same file handle and perform some other operations in such case i feel it will be better if it overwrites the same file handle .@Dave Cross
@salar33: But, you're overwriting a filehandle with an integer. That's never going to work.
Then how can i do by overwriting the same filehandle and use the same file handle to save its contents in new file name. @DaveCross

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.