Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: unwanted ARGV in subroutine

by choroba (Canon)
on Oct 29, 2012 at 21:48 UTC ( #1001428=note: print w/ replies, xml ) Need Help??

in reply to unwanted ARGV in subroutine

my $text = do { local( @ARGV, $/ ) = $infile ; <> } ;
This should locally change @ARGV to contain $infile and load the whole file into $text. Maybe the file is huge and you have just to wait a bit?
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Comment on Re: unwanted ARGV in subroutine
Download Code
Re^2: unwanted ARGV in subroutine
by runrig (Abbot) on Oct 29, 2012 at 22:19 UTC
    And if the file is huge, the OP shouldn't be loading it into memory, and should write to a temp file, and then rename it at the end.

      Hi rizzler

      Try this

      sub addheader{ my($infile,$header)=@_; open(FILE, "$infile"); my $text=do {local $/; <FILE>}; close FILE; open(FILE,">$infile"); print FILE $header; print FILE $text; close FILE; }
        Please don't try that :) it doesn't use error checking ... If you want to copy a file use File::Copy, for moving File::Move

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2015-03-28 19:13 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (625 votes), past polls