fouinix has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm new in perl. I just want to read a file on svn repository.

The doc (SVN::Client) says :

$ctx->cat(\*FILEHANDLE, $target, $revision, $pool); Outputs the content of the file identified by $target and $revision to the FILEHANDLE. FILEHANDLE is a reference to a filehandle.

When I put \*STDOUT it works. But I want to put the content of the $target to an array. I searched few hours on internet to find any example without success :(. With this code I have this error message : Bareword "FILE" not allowed while "strict subs" in use at script.pl line 16. Execution of script.pl aborted due to compilation errors.

#!/usr/bin/perl use 5.014; use strict; use warnings; use SVN::Client; my $ctx = new SVN::Client( auth => [SVN::Client::get_simple_provider(), SVN::Client::get_simple_prompt_provider(\&simple_prompt,2), SVN::Client::get_username_provider()] ); $ctx->cat(FILE, 'http://127.0.0.1/file.txt','HEAD'); my @text = <FILE>; sub simple_prompt { my $cred = shift; my $realm = shift; my $default_username = shift; my $may_save = shift; my $pool = shift; print "Enter authentication info for realm: $realm\n"; print "Username: "; my $username = <>; chomp($username); $cred->username($username); print "Password: "; my $password = <>; chomp($password); $cred->password($password); }
Thanks a lot!

Replies are listed 'Best First'.
Re: Use SVN::Client to "cat" a file on svn
by choroba (Cardinal) on Feb 04, 2014 at 15:44 UTC
    The characters \* are not part of the STDOUT name, and they are not there just for fun. They turn a bareword (no sigil) filehandle into a filehandle reference.
    $ctx->cat(\*FILE, 'http://127.0.0.1/file.txt', 'HEAD');

    You have to open the filehandle first, though:

    open FILE, '>', 'file.txt.HEAD' or die $!;

    Or, use lexical filehandles:

    open my $FH, '>', 'file.txt.HEAD' or die $!; $ctx->cat($FH, 'http://127.0.0.1/file.txt', 'HEAD');
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks ! So I have to put in a file, I can't put in an array?
        You can open a filehandle that points to a variable:
        open my $FH, '>', \$output;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Use SVN::Client to "cat" a file on svn
by kcott (Archbishop) on Feb 05, 2014 at 07:16 UTC

    G'day fouinix,

    Welcome to the monastery.

    I see you have your answer to the FILE vs. \*FILE issue.

    You seem to want to use an array. You don't say why or how you intend to use that array. I'm guessing that you want to avoid creating a disk file (for \*FILE). If you have follow-up questions, clarification of those points may be useful in providing a response.

    In the following code, I've created a subroutine to (roughly) emulate what $ctx->cat(...) is doing: emulate_ctx_cat(\*FILEHANDLE, $target). There's two blocks of code: the first creates a disk file from $target (compare with choroba's example) and prints its contents; the second creates an array from $target, manipulates the data in that array, and then shows that $target has not changed.

    #!/usr/bin/env perl use strict; use warnings; use autodie qw{:all}; use Tie::File; my $target_in = './pm_1073422_test.txt'; my $target_out = './pm_1073422_test.out'; print "*** Using Disk File ***\n"; { open my $fh_out, '>', $target_out; emulate_ctx_cat($fh_out, $target_in); close $fh_out; system cat => $target_out; unlink $target_out; } print "*** Using Array ***\n"; { open my $fh_out, '+>', undef; emulate_ctx_cat($fh_out, $target_in); tie my @text, 'Tie::File', $fh_out; print "* Original Text *\n"; print "$_\n" for @text; $text[2] = 'Modified line'; print "* Modified Text *\n"; print "$_\n" for @text; untie @text; close $fh_out; print "* Original File *\n"; system cat => $target_in; } sub emulate_ctx_cat { my ($fh_out, $target) = @_; open my $fh_in, '<', $target; while (<$fh_in>) { print $fh_out $_; } close $fh_in; }

    Output:

    *** Using Disk File *** Line 1 Line 2 Line 3 Line 4 *** Using Array *** * Original Text * Line 1 Line 2 Line 3 Line 4 * Modified Text * Line 1 Line 2 Modified line Line 4 * Original File * Line 1 Line 2 Line 3 Line 4

    [If it's unfamiliar to you, see the documentation for open and Tie::File for an explanation of the syntax I've used.]

    -- Ken

Re: Use SVN::Client to "cat" a file on svn
by GotToBTru (Prior) on Feb 04, 2014 at 15:49 UTC

    The compilation error is telling you perl found something it couldn't identify, "FILE", when it was parsing the statement. It needs that \* sigil to tell it what type of variable FILE is. Check the documentation again.