in reply to Get file make backup

sub get_file { my $name = shift; my $no_backup_flag = shift; unless ($no_backup_flag) { my $suffix = "aaa"; $suffix++ while -e "$name.$suffix"; local $^I = ".$suffix"; local $/ = \4096; local *ARGV; @ARGV = $name; print while <>; } local *ARGV; @ARGV = $name; local $/ = wantarray ? $/ : undef; # act like `` in scalar vs list c +ontext <>; }
I don't return the backup filename, because I hate globals. {grin}

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
(tye)Re: Get file make backup
by tye (Sage) on May 22, 2001 at 21:21 UTC

    I like this idiom but I recently learned that it doesn't work in at least a few versions of Perl prior to Perl 5.6.0.

    So keep that in mind if you plan to use the above.

            - tye (but my friends call me "Tye")

        Sorry, I was busy and so unacceptably terse. do { local *ARGV; @ARGV= $filename; <> } returns nothing in Perl 5.004 and keeps reading the "current" file in Perl 5.005. I'd be interested in more testing on this as I've just got the two examples at the moment so I'm not sure if the behavior is consistant across all of 5.004 and 5.005.

                - tye (but my friends call me "Tye")

      So, I've heard. I'm a perl 5.6.0er and a new camel booker so I took the open(FOO, "<", "foo.txt") idoim to heart as it scored a 'righterer' in the veritable tome. open(FOO, "<foo.txt") is more retro!

      cheers

      tachyon