{"thread":{"id":"7841","subject":"[PATCH] git-svn: Don't rely on $_ after making a function call","startedAt":"2007-04-25T18:50:32Z","lastAt":"2007-04-26T02:11:48Z","messageCount":5,"participants":["Adam Roben","Eric Wong","Junio C Hamano","Randal L. Schwartz"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"40418","messageId":"11775270321427-git-send-email-aroben@apple.com","threadId":"7841","inReplyTo":null,"subject":"[PATCH] git-svn: Don't rely on $_ after making a function call","fromName":"Adam Roben","fromEmail":"aroben@apple.com","sentAt":"2007-04-25T18:50:32Z","receivedAt":"2007-04-25T18:50:32Z","isPatch":true,"sender":{"key":"aroben@apple.com","avatar":"https://gravatar.com/avatar/9d3697e1de53890adf241331f4b970bdd2b18962b2ff0b8028ebb00e085807f8?d=mp&s=160"},"body":"Many functions and operators in perl set $_, so its value cannot be relied upon\nafter calling arbitrary functions. The solution is simply to copy the value of\n$_ into a local variable that will not get overwritten.\n\nSigned-off-by: Adam Roben <aroben@apple.com>\n---\n git-svn.perl |   10 +++++-----\n 1 files changed, 5 insertions(+), 5 deletions(-)\n\ndiff --git a/git-svn.perl b/git-svn.perl\nindex 077d6b3..90f3bc1 100755\n--- a/git-svn.perl\n+++ b/git-svn.perl\n@@ -771,19 +771,19 @@ sub cmt_metadata {\n sub working_head_info {\n \tmy ($head, $refs) = @_;\n \tmy ($fh, $ctx) = command_output_pipe('rev-list', $head);\n-\twhile (<$fh>) {\n-\t\tchomp;\n-\t\tmy ($url, $rev, $uuid) = cmt_metadata($_);\n+\twhile (my $hash = <$fh>) {\n+\t\tchomp($hash);\n+\t\tmy ($url, $rev, $uuid) = cmt_metadata($hash);\n \t\tif (defined $url && defined $rev) {\n \t\t\tif (my $gs = Git::SVN->find_by_url($url)) {\n \t\t\t\tmy $c = $gs->rev_db_get($rev);\n-\t\t\t\tif ($c && $c eq $_) {\n+\t\t\t\tif ($c && $c eq $hash) {\n \t\t\t\t\tclose $fh; # break the pipe\n \t\t\t\t\treturn ($url, $rev, $uuid, $gs);\n \t\t\t\t}\n \t\t\t}\n \t\t}\n-\t\tunshift @$refs, $_ if $refs;\n+\t\tunshift @$refs, $hash if $refs;\n \t}\n \tcommand_close_pipe($fh, $ctx);\n \t(undef, undef, undef, undef);\n-- \n1.5.2.rc0.14.g520d-dirty\n"},{"id":"40451","messageId":"20070425205948.GA12375@untitled","threadId":"7841","inReplyTo":"11775270321427-git-send-email-aroben@apple.com","subject":"Re: [PATCH] git-svn: Don't rely on $_ after making a function call","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2007-04-25T20:59:48Z","receivedAt":"2007-04-25T20:59:48Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"Adam Roben <aroben@apple.com> wrote:\n> Many functions and operators in perl set $_, so its value cannot be relied upon\n> after calling arbitrary functions. The solution is simply to copy the value of\n> $_ into a local variable that will not get overwritten.\n\nDoes this fix any particular bug?  It looks right to me\nand makes the code easier to follow, so;\n\nAcked-by: Eric Wong <normalperson@yhbt.net>\n\n> Signed-off-by: Adam Roben <aroben@apple.com>\n> ---\n>  git-svn.perl |   10 +++++-----\n>  1 files changed, 5 insertions(+), 5 deletions(-)\n> \n> diff --git a/git-svn.perl b/git-svn.perl\n> index 077d6b3..90f3bc1 100755\n> --- a/git-svn.perl\n> +++ b/git-svn.perl\n> @@ -771,19 +771,19 @@ sub cmt_metadata {\n>  sub working_head_info {\n>  \tmy ($head, $refs) = @_;\n>  \tmy ($fh, $ctx) = command_output_pipe('rev-list', $head);\n> -\twhile (<$fh>) {\n> -\t\tchomp;\n> -\t\tmy ($url, $rev, $uuid) = cmt_metadata($_);\n> +\twhile (my $hash = <$fh>) {\n> +\t\tchomp($hash);\n> +\t\tmy ($url, $rev, $uuid) = cmt_metadata($hash);\n>  \t\tif (defined $url && defined $rev) {\n>  \t\t\tif (my $gs = Git::SVN->find_by_url($url)) {\n>  \t\t\t\tmy $c = $gs->rev_db_get($rev);\n> -\t\t\t\tif ($c && $c eq $_) {\n> +\t\t\t\tif ($c && $c eq $hash) {\n>  \t\t\t\t\tclose $fh; # break the pipe\n>  \t\t\t\t\treturn ($url, $rev, $uuid, $gs);\n>  \t\t\t\t}\n>  \t\t\t}\n>  \t\t}\n> -\t\tunshift @$refs, $_ if $refs;\n> +\t\tunshift @$refs, $hash if $refs;\n>  \t}\n>  \tcommand_close_pipe($fh, $ctx);\n>  \t(undef, undef, undef, undef);\n> -- \n> 1.5.2.rc0.14.g520d-dirty\n> \n\n-- \nEric Wong\n"},{"id":"40455","messageId":"3601FA1C-CDB1-4BFE-93D7-8FF1A79DCF45@apple.com","threadId":"7841","inReplyTo":"20070425205948.GA12375@untitled","subject":"Re: [PATCH] git-svn: Don't rely on $_ after making a function call","fromName":"Adam Roben","fromEmail":"aroben@apple.com","sentAt":"2007-04-25T21:03:22Z","receivedAt":"2007-04-25T21:03:22Z","isPatch":true,"sender":{"key":"aroben@apple.com","avatar":"https://gravatar.com/avatar/9d3697e1de53890adf241331f4b970bdd2b18962b2ff0b8028ebb00e085807f8?d=mp&s=160"},"body":"On Apr 25, 2007, at 1:59 PM, Eric Wong wrote:\n\n> Adam Roben <aroben@apple.com> wrote:\n>> Many functions and operators in perl set $_, so its value cannot  \n>> be relied upon\n>> after calling arbitrary functions. The solution is simply to copy  \n>> the value of\n>> $_ into a local variable that will not get overwritten.\n>\n> Does this fix any particular bug?  It looks right to me\n> and makes the code easier to follow, so;\n\n    Yes, I was getting warnings that $_ was uninitialized in the eq.  \nSorry for not mentioning that.\n\n-Adam\n"},{"id":"40456","messageId":"7v4pn4w41k.fsf@assigned-by-dhcp.cox.net","threadId":"7841","inReplyTo":"20070425205948.GA12375@untitled","subject":"Re: [PATCH] git-svn: Don't rely on $_ after making a function call","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-04-25T21:13:43Z","receivedAt":"2007-04-25T21:13:43Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Wong <normalperson@yhbt.net> writes:\n\n> Adam Roben <aroben@apple.com> wrote:\n>> Many functions and operators in perl set $_, so its value cannot be relied upon\n>> after calling arbitrary functions. The solution is simply to copy the value of\n>> $_ into a local variable that will not get overwritten.\n>\n> Does this fix any particular bug?  It looks right to me\n> and makes the code easier to follow, so;\n\nI suspect Merlyn has better explanation, but...\n\n     for (list) {\n\t...\t\n     }\n\nloop implicitly localizes $_ and does not share this problem,\nwhich I really appreciate whenever I am writing Perl code, but\noften enough I was bitten by scripts that use \"while (<$fh>)\",\nwhich does not localizes $_, and made hard to spot bugs by\nclobbering $_.\n\nI find the patch a good safety measure for any future breakages.\n"},{"id":"40473","messageId":"864pn3hokb.fsf@blue.stonehenge.com","threadId":"7841","inReplyTo":"7v4pn4w41k.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] git-svn: Don't rely on $_ after making a function call","fromName":"Randal L. Schwartz","fromEmail":"merlyn@stonehenge.com","sentAt":"2007-04-26T02:11:48Z","receivedAt":"2007-04-26T02:11:48Z","isPatch":true,"sender":{"key":"merlyn@stonehenge.com","avatar":"https://gravatar.com/avatar/dc528d210743ff0333e6213f9ee7b33b23f1b7bc1f3c5a8c2d819074ecd7ab19?d=mp&s=160"},"body":">>>>> \"Junio\" == Junio C Hamano <junkio@cox.net> writes:\n\nJunio> I suspect Merlyn has better explanation, but...\n\nJunio>      for (list) {\nJunio> \t...\t\nJunio>      }\n\nJunio> loop implicitly localizes $_ and does not share this problem,\nJunio> which I really appreciate whenever I am writing Perl code, but\nJunio> often enough I was bitten by scripts that use \"while (<$fh>)\",\nJunio> which does not localizes $_, and made hard to spot bugs by\nJunio> clobbering $_.\n\nJunio> I find the patch a good safety measure for any future breakages.\n\nI'm happy with the patch.\n\n-- \nRandal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095\n<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>\nPerl/Unix/security consulting, Technical writing, Comedy, etc. etc.\nSee PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!\n"}]}