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 6981323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:08:32+00:00 2026-05-27T18:08:32+00:00

I’m having trouble modifying a script that processes files passed as command line arguments,

  • 0

I’m having trouble modifying a script that processes files passed as command line arguments, merely for copying those files, to additionally modifying those files. The following perl script worked just fine for copying files:

use strict;
use warnings;

use File::Copy;

foreach $_ (@ARGV) {
   my $orig = $_;
   (my $copy = $orig) =~ s/\.js$/_extjs4\.js/;
   copy($orig, $copy) or die(qq{failed to copy $orig -> $copy});
}

Now that I have files named “*_extjs4.js”, I would like to pass those into a script that similarly takes file names from the command line, and further processes the lines within those files. So far I am able get a file handle successfully as the following script and it’s output shows:

use strict;
use warnings;

foreach $_ (@ARGV) {
    print "$_\n";
    open(my $fh, "+>", $_) or die $!;
    print $fh;
    #while (my $line = <$fh>) {
    #    print $line;
    #}
    close $fh;
}

Which outputs (in part):

./filetree_extjs4.js
GLOB(0x1a457de8)
./async_submit_extjs4.js
GLOB(0x1a457de8)

What I really want to do though rather than printing a representation of the file handle, is to work with the contents of the files themselves. A start would be to print the files lines, which I’ve tried to do with the commented out code above.

But that code has no effect, the files’ lines do not get printed. What am I doing wrong? Is there a conflict between the $_ used to process command line arguments, and the one used to process file contents?

  • 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-27T18:08:33+00:00Added an answer on May 27, 2026 at 6:08 pm

    It looks like there are a couple of questions here.


    What I really want to do though rather than printing a representation of the file handle, is to work with the contents of the files themselves.

    The reason why print $fh is returning GLOB(0x1a457de8) is because the scalar $fh is a filehandle and not the contents of the file itself. To access the contents of the file itself, use <$fh>. For example:

    while (my $line = <$fh>) {
        print $line;
    }
    
    # or simply print while <$fh>;
    

    will print the contents of the entire file.

    This is documented in pelrdoc perlop:

    If what the angle brackets contain is a simple scalar variable (e.g.,
    <$foo>), then that variable contains the name of the filehandle to
    input from, or its typeglob, or a reference to the same.


    But it has already been tried!

    I can see that. Try it after changing the open mode to +<.

    According to perldoc perlfaq5:

    How come when I open a file read-write it wipes it out?

    Because you’re using something like this, which truncates the file
    then gives you read-write access:

      open my $fh, '+>', '/path/name'; # WRONG (almost always)
    

    Whoops. You should instead use this, which will fail if the file
    doesn’t exist:

      open my $fh, '+<', '/path/name'; # open for update
    

    Using ">" always clobbers or creates. Using "<" never does either. The
    "+" doesn’t change this.

    It goes without saying that the or die $! after the open is highly recommended.


    But take a step back.

    There is a more Perlish way to back up the original file and subsequently manipulate it. In fact, it is doable via the command line itself (!) using the -i flag:

    $ perl -p -i._extjs4 -e 's/foo/bar/g' *.js
    

    See perldoc perlrun for more details.


    I can’t fit my needs into the command-line.

    If the manipulation is too much for the command-line to handle, the Tie::File module is worth a try.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.